Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created Aug 22, 2018
Embed
What would you like to do?
import typing as t
import typing_extensions as tx
class A:
def add(self, x: int, y: int, *, verbose: bool = False) -> int:
return x + y
class B:
def add(self, x: int, y: int, verbose: bool = False) -> int:
return x + y
class C:
def add(self, x: int, y: int) -> t.Any:
return x + y
class Adder(tx.Protocol):
def add(self, x: int, y: int) -> int:
...
def use(adder: Adder, x: int, y: int) -> int:
return adder.add(x, y)
def main() -> None:
a = A()
print(use(a, 10, 20))
b = B()
print(use(b, 10, 20))
c = C()
print(use(c, 10, 20))
import typing as t
F = t.Callable[[int, int], int]
def add(x: int, y: int, *, verbose: bool = False) -> int:
return x + y
def add2(x: int, y: int, verbose: bool = True) -> int:
return x + y
def use(f: F, x: int, y: int) -> int:
return f(x, y)
def main() -> None:
print(use(add, 10, 20))
print(use(add2, 10, 20))
import typing as t
import mypy_extensions as mx
F = t.Callable[[int, int, mx.DefaultNamedArg(bool, "verbose")], int]
def add(x: int, y: int, *, verbose: bool = False) -> int:
return x + y
def add2(x: int, y: int, verbose: bool = False) -> int:
return x + y
def use(f: F, x: int, y: int) -> int:
return f(x, y)
def main() -> None:
print(use(add, 10, 20))
print(use(add2, 10, 20))
import typing_extensions as tx
class F(tx.Protocol):
def __call__(self, x: int, y: int, *, verbose: bool = False) -> int:
...
def add(x: int, y: int, *, verbose: bool = False) -> int:
return x + y
def use(f: F, x: int, y: int) -> int:
return f(x, y)
def main() -> None:
print(use(add, 10, 20))
import typing as t
import typing_extensions as tx
class P(tx.Protocol):
def f(self, **kwargs: t.Any) -> None:
...
class A:
def f(self, verbose: bool = False) -> None:
pass
def use(p: P) -> None:
p.f()
def main():
a = A()
use(a)
import typing as t
import mypy_extensions as mx
# (x: int, y:int, verbose: bool=...) -> int みたいな型
F = t.Callable[[int, int, mx.DefaultArg(bool, "verbose")], int]
# error: Argument 1 to "use" has incompatible type "Callable[[int, int, DefaultNamedArg(bool, 'verbose')], int]"; expected "Callable[[int, int, bool], int]"
def add(x: int, y: int, *, verbose: bool = False) -> int:
return x + y
def add2(x: int, y: int, verbose: bool = False) -> int:
return x + y
def use(f: F, x: int, y: int) -> int:
return f(x, y)
def main() -> None:
print(use(add, 10, 20))
print(use(add2, 10, 20))
import typing as t
import argparse
if t.TYPE_CHECKING:
import myprotocol # noqa
class FakeParser:
def parse_args(
self,
argv: t.Optional[t.Sequence[str]] = None,
namespace: t.Optional[argparse.Namespace] = None
) -> t.Any:
pass
def add_argument(
self,
*name_or_flags: str,
**kwarg: t.Any,
) -> t.Any:
pass
def use(parser: "myprotocol.ArgumentParserSubset") -> None:
parser.add_argument("--verbose", action="store_true")
print(parser.parse_args())
def main() -> None:
use(argparse.ArgumentParser())
use(FakeParser())
00:
mypy --strict 00*.py
01:
mypy --strict 01*.py
02:
mypy --strict 02*.py
03:
mypy --strict 03*.py
04:
mypy --strict 04*.py
05:
mypy --strict 05*.py
06:
mypy --strict 06*.py
import typing as t
import typing_extensions as tx
import argparse
T = t.TypeVar("T", contravariant=True)
class ArgumentParserSubset(tx.Protocol[T]):
def parse_args(
self,
argv: t.Optional[t.Sequence[str]] = ...,
namespace: t.Optional[argparse.Namespace] = ...
) -> t.Any:
...
# これはダメ。。
# def add_argument(self, *name_or_flags: str, **kwargs: t.Any) -> argparse.Action:
# ...
def add_argument(
self,
*name_or_flags: str,
action: t.Union[str, t.Type[argparse.Action]] = ...,
nargs: t.Union[int, str] = ...,
const: t.Any = ...,
default: t.Any = ...,
type: t.Union[t.Callable[[str], T], argparse.FileType] = ...,
choices: t.Iterable[T] = ...,
required: bool = ...,
help: t.Optional[str] = ...,
metavar: t.Union[str, t.Tuple[str, ...]] = ...,
dest: t.Optional[str] = ...,
version: str = ...
) -> t.Any:
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment