Created
August 22, 2018 16:40
-
-
Save podhmo/db5711ce63d2a37b354f30c793e01caa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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