mypy

Typechecking dynamically added attributes

◇◆丶佛笑我妖孽 提交于 2020-12-03 07:43:07
问题 When writing project-specific pytest plugins, I often find the Config object useful to attach my own properties. Example: from _pytest.config import Config def pytest_configure(config: Config) -> None: config.fizz = "buzz" def pytest_unconfigure(config: Config) -> None: print(config.fizz) Obviously, there's no fizz attribute in _pytest.config.Config class, so running mypy over the above snippet yields conftest.py:5: error: "Config" has no attribute "fizz" conftest.py:8: error: "Config" has no

Does mypy only type check a function if it declares a return type?

让人想犯罪 __ 提交于 2020-11-29 19:25:42
问题 The following file: from typing import List class A: def __init__(self, myStr): self.chars: List[int] = list(myStr) def toString(self): return "".join(self.chars) typechecks (note chars should be List[str] not List[int]): ➜ Python python3 -m mypy temp.py => Success: no issues found in 1 source file , but the following one, where I declare the return type of toString, does: from typing import List class A: def __init__(self, myStr): self.chars: List[int] = list(myStr) def toString(self) -> str

Does mypy only type check a function if it declares a return type?

主宰稳场 提交于 2020-11-29 19:25:38
问题 The following file: from typing import List class A: def __init__(self, myStr): self.chars: List[int] = list(myStr) def toString(self): return "".join(self.chars) typechecks (note chars should be List[str] not List[int]): ➜ Python python3 -m mypy temp.py => Success: no issues found in 1 source file , but the following one, where I declare the return type of toString, does: from typing import List class A: def __init__(self, myStr): self.chars: List[int] = list(myStr) def toString(self) -> str