mypy

Generate TypedDict from function's keyword arguments

半城伤御伤魂 提交于 2021-02-19 02:26:29
问题 foo.py : kwargs = {"a": 1, "b": "c"} def consume(*, a: int, b: str) -> None: pass consume(**kwargs) mypy foo.py : error: Argument 1 to "consume" has incompatible type "**Dict[str, object]"; expected "int" error: Argument 1 to "consume" has incompatible type "**Dict[str, object]"; expected "str" This is because object is a supertype of int and str , and is therefore inferred. If I declare: from typing import TypedDict class KWArgs(TypedDict): a: int b: str and then annotate kwargs as KWArgs ,

Can mypy check docstrings?

╄→гoц情女王★ 提交于 2021-02-18 12:12:38
问题 I have numpydoc-style docstrings: def foobar(filename, copy, dtype, iterable, shape, files): """ foobar is 42. Parameters ---------- filename : str copy : bool dtype : data-type iterable : iterable object shape : int or tuple of int files : list of str Returns ------- foobarfoo : int """ pass Is it possible to check if the docstring-types can possibly be correct? (side question: Can numpy return/print the function signatures it discovered?) For example, I would expect the following to fail:

Can mypy check docstrings?

元气小坏坏 提交于 2021-02-18 12:10:42
问题 I have numpydoc-style docstrings: def foobar(filename, copy, dtype, iterable, shape, files): """ foobar is 42. Parameters ---------- filename : str copy : bool dtype : data-type iterable : iterable object shape : int or tuple of int files : list of str Returns ------- foobarfoo : int """ pass Is it possible to check if the docstring-types can possibly be correct? (side question: Can numpy return/print the function signatures it discovered?) For example, I would expect the following to fail:

Can mypy check docstrings?

女生的网名这么多〃 提交于 2021-02-18 12:10:01
问题 I have numpydoc-style docstrings: def foobar(filename, copy, dtype, iterable, shape, files): """ foobar is 42. Parameters ---------- filename : str copy : bool dtype : data-type iterable : iterable object shape : int or tuple of int files : list of str Returns ------- foobarfoo : int """ pass Is it possible to check if the docstring-types can possibly be correct? (side question: Can numpy return/print the function signatures it discovered?) For example, I would expect the following to fail:

Callable is invalid base class?

落花浮王杯 提交于 2021-02-18 06:44:06
问题 Can someone explain why inheriting from unparameterized and parameterized Callable : from typing import Callable from typing import NoReturn from typing import TypeVar T = TypeVar('T', str, int) C = Callable[[T], NoReturn] class Foo(Callable): def __call__(self, t: T): pass class Bar(C): def __call__(self, t: T): pass when passed to mypy raises errors for both Foo and Bar : tmp.py:13: error: Invalid base class tmp.py:19: error: Invalid base class 回答1: This is in part because classes at

How can I keep imports lightweight and still properly type annotate?

∥☆過路亽.° 提交于 2021-02-11 06:14:38
问题 Tensorflow is a super heavy import. I want to import it only when it's needed. However, I have a model loading function like this: from typing import Dict, Any from keras.models import Model # Heavy import! Takes 2 seconds or so! # Model loading is a heavy task. Only do it once and keep it in memory model = None # type: Optional[Model] def load_model(config: Dict[str, Any], shape) -> Model: """Load a model.""" if globals()['model'] is None: globals()['model'] = create_model(wili.n_classes,

How can I keep imports lightweight and still properly type annotate?

家住魔仙堡 提交于 2021-02-11 06:13:12
问题 Tensorflow is a super heavy import. I want to import it only when it's needed. However, I have a model loading function like this: from typing import Dict, Any from keras.models import Model # Heavy import! Takes 2 seconds or so! # Model loading is a heavy task. Only do it once and keep it in memory model = None # type: Optional[Model] def load_model(config: Dict[str, Any], shape) -> Model: """Load a model.""" if globals()['model'] is None: globals()['model'] = create_model(wili.n_classes,

Python Dictionary with generic keys and Callable[T] values

独自空忆成欢 提交于 2021-02-10 12:42:54
问题 I have some named tuples: JOIN = NamedTuple("JOIN", []) EXIT = NamedTuple("EXIT", []) I also have functions to handle each type of tuple with: def handleJoin(t: JOIN) -> bool: pass def handleExit(t: EXIT) -> bool: pass What I want to do is create a dictionary handleTuple so I can call it like so: t: Union[JOIN, EXIT] = #an instance of JOIN or EXIT result: bool result = handleTuple[type(t)](t) What I cannot figure out is how to define said dictionary. I tried defining a generic T : T = TypeVar

Python dynamic properties and mypy

跟風遠走 提交于 2021-02-08 15:17:36
问题 I'm trying to mask some functions as properties (through a wrapper which is not important here) and add them to the object dynamically, however, I need code completion and mypy to work. I figured out how to add a property dynamically (either through a metaclass or simply in constructor), but the problem I have is mypy doesn't pick it up (and neither does the IDE). One workaround is to define an attribute with the same name/type, but I really don't like this approach (too much code, static set

Python dynamic properties and mypy

自闭症网瘾萝莉.ら 提交于 2021-02-08 15:17:05
问题 I'm trying to mask some functions as properties (through a wrapper which is not important here) and add them to the object dynamically, however, I need code completion and mypy to work. I figured out how to add a property dynamically (either through a metaclass or simply in constructor), but the problem I have is mypy doesn't pick it up (and neither does the IDE). One workaround is to define an attribute with the same name/type, but I really don't like this approach (too much code, static set