typehints

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

How should a python type hint require that a value has a given attribute?

社会主义新天地 提交于 2020-12-25 01:27:06
问题 Let's say I have a simple function like this: def foo(a: Any): return a.bar + a.baz I would like to change the type hint from Any to one that requires (well, suggests, given that it is a type hint ) that a provides the bar and baz attributes. What should it be changed to? 回答1: This is exactly what Protocols are for. In short, Protocols let you use structural instead of nominal subtyping. With nominal subtyping, type A is a subtype of B if A explicitly inherits or extends B. With structural

How should a python type hint require that a value has a given attribute?

一笑奈何 提交于 2020-12-25 01:25:25
问题 Let's say I have a simple function like this: def foo(a: Any): return a.bar + a.baz I would like to change the type hint from Any to one that requires (well, suggests, given that it is a type hint ) that a provides the bar and baz attributes. What should it be changed to? 回答1: This is exactly what Protocols are for. In short, Protocols let you use structural instead of nominal subtyping. With nominal subtyping, type A is a subtype of B if A explicitly inherits or extends B. With structural

Why do I get a warning when concatenating lists of mixed types in Pycharm?

风流意气都作罢 提交于 2020-12-23 04:17:19
问题 In Pycharm, the following code produces a warning: from typing import List list1: List[int] = [1, 2, 3] list2: List[str] = ["1", "2", "3"] list3: List[object] = list1 + list2 # ↳ Expected type List[int] (matched generic type List[_T]), # got List[str] instead. Why? Should I not be concatenating two lists of mixed, hinted types? 回答1: As requested in the comments, here are some reasons why type checkers don't allow this. The first reason is somewhat prosaic: the type signature of list.__add__

Python type hints for function returning multiple return values

大兔子大兔子 提交于 2020-06-14 06:03:30
问题 How do I write the function declaration using Python type hints for function returning multiple return values? Is the below syntax allowed? def greeting(name: str) -> str, List[float], int : // do something return a,b,c 回答1: You can use a typing.Tuple type hint (to specify the type of the content of the tuple, if it is not necessary, the built-in class tuple can be used instead): from typing import Tuple def greeting(name: str) -> Tuple[str, List[float], int]: # do something return a, b, c

Python type hints for function returning multiple return values

我们两清 提交于 2020-06-14 06:02:08
问题 How do I write the function declaration using Python type hints for function returning multiple return values? Is the below syntax allowed? def greeting(name: str) -> str, List[float], int : // do something return a,b,c 回答1: You can use a typing.Tuple type hint (to specify the type of the content of the tuple, if it is not necessary, the built-in class tuple can be used instead): from typing import Tuple def greeting(name: str) -> Tuple[str, List[float], int]: # do something return a, b, c

Why can't List contain multiple types?

房东的猫 提交于 2020-06-13 19:06:40
问题 You can mix types inside tuples or lists. Why can't you specify that in typing hints? >>> from typing import Tuple, List >>> t = ('a', 1) >>> l = ['a', 1] >>> t2: Tuple[str, int] = ('a', 1) >>> l2: List[str, int] = ['a', 1] TypeError: Too many parameters for typing.List; actual 2, expected 1 回答1: In type theory, a list is a homogenous structure containing values of one type. As such, List only takes a single type, and every element of that list has to have that type. However, type theory also

Why can't List contain multiple types?

眉间皱痕 提交于 2020-06-13 19:06:27
问题 You can mix types inside tuples or lists. Why can't you specify that in typing hints? >>> from typing import Tuple, List >>> t = ('a', 1) >>> l = ['a', 1] >>> t2: Tuple[str, int] = ('a', 1) >>> l2: List[str, int] = ['a', 1] TypeError: Too many parameters for typing.List; actual 2, expected 1 回答1: In type theory, a list is a homogenous structure containing values of one type. As such, List only takes a single type, and every element of that list has to have that type. However, type theory also

typehints -> None or leave blank

拟墨画扇 提交于 2020-06-11 18:18:19
问题 Using python 3, one has the option to use typehints. My question is, if a function returns None, should one add this, or leave it blank. i.e. def hint(p:str) -> None: pass def no_hint(p:str): pass And which PEP addresses this? 回答1: Be explicit and always include -> None for functions that return None That's because otherwise, for functions that don't take arguments , the type checker will assume that you did not use type hints at all. For example, is def foo(): going to return None , or was

typehints -> None or leave blank

馋奶兔 提交于 2020-06-11 18:17:31
问题 Using python 3, one has the option to use typehints. My question is, if a function returns None, should one add this, or leave it blank. i.e. def hint(p:str) -> None: pass def no_hint(p:str): pass And which PEP addresses this? 回答1: Be explicit and always include -> None for functions that return None That's because otherwise, for functions that don't take arguments , the type checker will assume that you did not use type hints at all. For example, is def foo(): going to return None , or was