mypy

How to make Mypy deal with subclasses in functions as expected

泄露秘密 提交于 2020-03-16 06:14:30
问题 I have the following code: from typing import Callable MyCallable = Callable[[object], int] MyCallableSubclass = Callable[['MyObject'], int] def get_id(obj: object) -> int: return id(obj) def get_id_subclass(obj: 'MyObject') -> int: return id(obj) def run_mycallable_function_on_object(obj: object, func: MyCallable) -> int: return func(obj) class MyObject(object): '''Object that is a direct subclass of `object`''' pass my_object = MyObject() # works just fine run_mycallable_function_on_object

How to make Mypy deal with subclasses in functions as expected

点点圈 提交于 2020-03-16 06:12:50
问题 I have the following code: from typing import Callable MyCallable = Callable[[object], int] MyCallableSubclass = Callable[['MyObject'], int] def get_id(obj: object) -> int: return id(obj) def get_id_subclass(obj: 'MyObject') -> int: return id(obj) def run_mycallable_function_on_object(obj: object, func: MyCallable) -> int: return func(obj) class MyObject(object): '''Object that is a direct subclass of `object`''' pass my_object = MyObject() # works just fine run_mycallable_function_on_object

How to make Mypy deal with subclasses in functions as expected

谁说我不能喝 提交于 2020-03-16 06:12:04
问题 I have the following code: from typing import Callable MyCallable = Callable[[object], int] MyCallableSubclass = Callable[['MyObject'], int] def get_id(obj: object) -> int: return id(obj) def get_id_subclass(obj: 'MyObject') -> int: return id(obj) def run_mycallable_function_on_object(obj: object, func: MyCallable) -> int: return func(obj) class MyObject(object): '''Object that is a direct subclass of `object`''' pass my_object = MyObject() # works just fine run_mycallable_function_on_object

Can mypy handle list comprehensions?

穿精又带淫゛_ 提交于 2020-02-03 16:18:33
问题 from typing import Tuple def test_1(inp1: Tuple[int, int, int]) -> None: pass def test_2(inp2: Tuple[int, int, int]) -> None: test_tuple = tuple(e for e in inp2) reveal_type(test_tuple) test_1(test_tuple) While running mypy on the above code, I get: error: Argument 1 to "test_1" has incompatible type "Tuple[int, ...]"; expected "Tuple[int, int, int]" Is test_tuple not guaranteed to have 3 int elements? Does mypy not handle such list comprehensions or is there another way of defining the type

Exclude type in Python typing annotation

旧时模样 提交于 2020-01-30 05:44:18
问题 I wrote the following function: def _clean_dict(d): return {k: v for k, v in d.items() if v is not None} I want to add type annotations to the function: def _clean_dict(d: Dict[Any, Any]) -> Dict[Any, Any]: return {k: v for k, v in d.items() if v is not None} However, I want to explicitly define that the values inside the returned dictionary cannot be None. Is there a way to say " Any type, except NoneType " or "Every possible value but None "? 回答1: Python type hinting can't exclude types.

Type hinting sqlalchemy query result

余生长醉 提交于 2020-01-24 02:46:06
问题 I can't figure out what kind of object a sqlalchemy query returns. entries = session.query(Foo.id, Foo.date).all() The type of each object in entries seems to be sqlalchemy.util._collections.result , but a quick from sqlalchemy.util._collections import result in a python interpreter raises an ImportError. What I'm ultimately trying to do is to type hint this function: def my_super_function(session: Session) -> ???: entries = session.query(Foo.id, Foo.date).all() return entries What should I

How to use static type checking using Dict with different value types in Python 3.6?

ぃ、小莉子 提交于 2020-01-14 14:48:30
问题 Trying to use static types in Python code, so mypy can help me with some hidden errors. It's quite simple to use with single variables real_hour: int = lower_hour + hour_iterator Harder to use it with lists and dictionaries, need to import additional typing library: from typing import Dict, List hour_dict: Dict[str, str] = {"test_key": "test_value"} But the main problem - how to use it with Dicts with different value types, like: hour_dict = {"test_key": "test_value", "test_keywords": ["test

What is the correct way to type hint a homogenous Queue in Python3.6 (especially for PyCharm)?

大憨熊 提交于 2020-01-14 08:27:38
问题 I'm writing a fractal generator in Python 3.6, and I use multiprocessing.Queue s to pass messages from the main thread to the workers. This is what I've tried so far, but PyCharm doesn't seem to be able to infer attribute types for items taken from the queues: from typing import NamedTuple, Any, Generic, TypeVar, Tuple from multiprocessing import Process, Queue T = TypeVar() class Message(NamedTuple): method: str id: str data: Any = None class TypedQueue(Generic[T]): def get(self) -> T: ...

mypy: creating a type that accepts list of instances of subclasses

牧云@^-^@ 提交于 2020-01-14 02:29:15
问题 Suppose I have a Child class that is a subclass of Parent class, and a function that accepts a list of instances of Parent subclasses: from typing import List class Parent: pass class Child(Parent): pass def func(objects: List[Parent]) -> None: print(objects) children = [Child()] func(children) running mypy on this produces an error: error: Argument 1 to "func" has incompatible type "List[Child]"; expected "List[Parent]" How do I create a type for this? P.S. There's a way to fix this

How should I type-hint an integer variable that can also be infinite?

淺唱寂寞╮ 提交于 2020-01-04 08:23:08
问题 Searching for this topic I came across the following: How to represent integer infinity? I agree with Martijn Peeters that adding a separate special infinity value for int may not be the best of ideas. However, this makes type hinting difficult. Assume the following code: myvar = 10 # type: int myvar = math.inf # <-- raises a typing error because math.inf is a float However, the code behaves everywhere just the way as it should. And my type hinting is correct everywhere else. If I write the