type-hinting

Using type hints to translate Python to Cython

痴心易碎 提交于 2021-02-06 10:00:27
问题 Type Hints now are available in Python 3.5 version. In the specification (PEP 484) the goals (and the non-goals) are exposed clearly: Rationale and Goals This PEP aims to provide a standard syntax for type annotations, opening up Python code to easier static analysis and refactoring, potential runtime type checking, and (perhaps, in some contexts) code generation utilizing type information. [...] Of these goals, static analysis is the most important. Non-goals Using type hints for performance

NumPy type hint that something is both an array and float32? [duplicate]

大兔子大兔子 提交于 2021-01-29 12:55:54
问题 This question already has answers here : Type hint for NumPy ndarray dtype? (4 answers) Closed 1 year ago . How can I type hint that a value being returned by a function is both an NumPy array and holds NumPy float32 data? I can specify that the returned value is an array using: def func() -> np.ndarray: ... However, this does not enforce the knowledge that it is a float32 array. I can specify that the returned value is of type float32 using: def func() -> np.float32: ... However, this does

Is it possible to “hint” dictionary keys?

时光毁灭记忆、已成空白 提交于 2021-01-28 11:47:43
问题 In PyCharm Code completion > "Basic Completion" > "Invoke Basic Completion" > "Dictionaries" I see that, if you hard-code a dictionary to some values, you can use code completion when writing code about that dictionary. But obviously, in many cases, you will work with a dict and you have some idea in advance what the structure of that dict will be, and you don't want to hard-code the dict into your code. For example, maybe you're parsing some YAML or JSON that has an expected structure. It

Python metaclass - make class property accessible via class and class instance

大兔子大兔子 提交于 2021-01-27 06:58:51
问题 Using python 3.7, I have created a class property in a metaclass. I would like to be able to access the property via the class itself or an instantiated object of the class. I can emulate it by creating a class property AND an instance property, but it screws with PyCharm's type hinting. Here's what I consider the ideal set up: class Meta(type): @property def cls_prop(cls) -> str: return 'foo' class A(metaclass=Meta): pass But unfortunately, here are the results: >>> A.cls_prop 'foo' >>> a =

Python metaclass - make class property accessible via class and class instance

余生颓废 提交于 2021-01-27 06:55:35
问题 Using python 3.7, I have created a class property in a metaclass. I would like to be able to access the property via the class itself or an instantiated object of the class. I can emulate it by creating a class property AND an instance property, but it screws with PyCharm's type hinting. Here's what I consider the ideal set up: class Meta(type): @property def cls_prop(cls) -> str: return 'foo' class A(metaclass=Meta): pass But unfortunately, here are the results: >>> A.cls_prop 'foo' >>> a =

How can request.param be annotated in indirect parametrization?

痞子三分冷 提交于 2021-01-07 06:57:05
问题 In the Indirect parametrization example I want to type hint request.param indicating a specific type, a str for example. The problem is since the argument to fixt must be the request fixture there seems to be no way to indicate what type the parameters passed through the "optional param attribute" should be (quoting the documentation). What are the alternatives? Perhaps documenting the type hint in the fixt docstring, or in the test_indirect docstring? @pytest.fixture def fixt(request):

How to use reveal_type in mypy

限于喜欢 提交于 2020-12-25 01:36:43
问题 I have read that I can reveal the type of variables by using a function called reveal_type , but I can't find how to use it or from where to import it. 回答1: I found out in the end how to use it: You should just put and use the reveal_type in the code, and run it with the mypy program. Then, it will log a message that look like this: Revealed type is 'builtins.str*' From the mypy documentation: reveal_type is only understood by mypy and doesn’t exist in Python, if you try to run your program.

How to cast a typing.Union to one of its subtypes in Python?

为君一笑 提交于 2020-12-05 06:00:51
问题 I’m using Python 3.6.1, mypy, and the typing module. I created two custom types, Foo and Bar , and then used them in a dict I return from a function. The dict is described as mapping str to a Union of Foo and Bar . Then I want to use values from this dict in a function that names only one argument each: from typing import Dict, Union, NewType Foo = NewType("Foo", str) Bar = NewType("Bar", int) def get_data() -> Dict[str, Union[Foo, Bar]]: return {"foo": Foo("one"), "bar": Bar(2)} def process