typehints

What is the type hint for a (any) python module?

≡放荡痞女 提交于 2020-03-13 07:08:29
问题 I would like to add the (Python3) type hint for a module (class 'module'). The typing package doesn't provide one, and types.ModuleType() is a constructor that returns a module object for a specific name. Example: import types def foo(module: types.ModuleType): pass at least in PyCharm results in "Cannot find reference ModuleType in types.pyi". 回答1: and types.ModuleType() is a constructor. That doesn't matter. types.ModuleType is still a reference to a type, just like str and int are. There

What is the type hint for a (any) python module?

让人想犯罪 __ 提交于 2020-03-13 07:08:08
问题 I would like to add the (Python3) type hint for a module (class 'module'). The typing package doesn't provide one, and types.ModuleType() is a constructor that returns a module object for a specific name. Example: import types def foo(module: types.ModuleType): pass at least in PyCharm results in "Cannot find reference ModuleType in types.pyi". 回答1: and types.ModuleType() is a constructor. That doesn't matter. types.ModuleType is still a reference to a type, just like str and int are. There

Python: All type hints errors in subclass constructure seems ignored

↘锁芯ラ 提交于 2019-12-25 01:33:14
问题 I have the following code with python type hints It has a bunch of errors. All erros in code are found by mypy but not the errors in constructor of S. Why? I cannot find out what is happening thanks code: import typing class T(object): def __init__(self, a: int, b: str = None) -> None: self.a = a self.b: typing.Union[str, None] = b self._callback_map: typing.Dict[str, str] = {} class S(T): def __init__(self): super().__init__(self, 1, 2) self._callback_map[1] = "TOTO" s = T(1, 1) t = T(1, b=2

pylint giving not-callable error for object property that is callable

泄露秘密 提交于 2019-12-11 03:15:48
问题 Not sure if I am doing something wrong or if this is a problem with pylint . In the code below I get a linting error that self.type is not callable E1102 . Although I could just ignore it and keep working, seems like this kind of thing should be easy to fix... just can't figure out how to fix it. from typing import Callable class Thing: def __init__(self, thing_type: Callable): self._type = thing_type self._value = None @property def type(self) -> Callable: return self._type @property def

Is it possible to test a function that uses get_type_hints with a doctest?

时光毁灭记忆、已成空白 提交于 2019-12-08 12:37:22
问题 I have a function that uses typing.get_type_hints. I want to add a documentation test to it. However, it looks like get_type_hints fails to resolve types that are defined in a doctest. Here is a simplified example: import typing def f(clazz): """ >>> class MyClass: ... my_field: 'MyClass' >>> f(MyClass) """ typing.get_type_hints(clazz) When running it with python3 -m doctest test.py it throws NameError: name 'MyClass' is not defined . 回答1: from __future__ import annotations import typing def