type-hinting

Pycharm type checker - expected Series, got int

≡放荡痞女 提交于 2021-02-19 16:35:38
问题 I have very simple Python function that subtracts scalar value from pandas Series: def sub(x: pd.Series, a: int) -> pd.Series: return x - a It works as expected. I'm using type hints to enable type checker directly in my PyCharm IDE. The issue here is I get this warning message: Expected type 'Series', got 'int' instead As you can see in the image below: I understand that Python is dynamically typed language so in some cases type checking with type hints has its own limitations. But this

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 ,

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

“TypeError: 'type' object is not subscriptable” in a function signature

陌路散爱 提交于 2021-02-16 14:00:48
问题 Why am I receiving this error when I run this code? Traceback (most recent call last): File "main.py", line 13, in <module> def twoSum(self, nums: list[int], target: int) -> list[int]: TypeError: 'type' object is not subscriptable nums = [4,5,6,7,8,9] target = 13 def twoSum(self, nums: list[int], target: int) -> list[int]: dictionary = {} answer = [] for i in range(len(nums)): secondNumber = target-nums[i] if(secondNumber in dictionary.keys()): secondIndex = nums.index(secondNumber) if(i !=

Why Python doesn't throws type exceptions with '-> type' functions definitions? [duplicate]

半世苍凉 提交于 2021-02-08 06:03:33
问题 This question already has answers here : What are type hints in Python 3.5? (5 answers) Closed 4 years ago . In other languages anything like the example throws a type error. Why not in Python? >>> def foo(a:int) -> str: return a+1 >>> foo(5) 6 回答1: Type hinting in Python is an optional addition to aid static code analysis and editors. From the PEP 484 -- Type Hints specification: Note that this PEP still explicitly does NOT prevent other uses of annotations, nor does it require (or forbid)

Why Python doesn't throws type exceptions with '-> type' functions definitions? [duplicate]

和自甴很熟 提交于 2021-02-08 06:02:27
问题 This question already has answers here : What are type hints in Python 3.5? (5 answers) Closed 4 years ago . In other languages anything like the example throws a type error. Why not in Python? >>> def foo(a:int) -> str: return a+1 >>> foo(5) 6 回答1: Type hinting in Python is an optional addition to aid static code analysis and editors. From the PEP 484 -- Type Hints specification: Note that this PEP still explicitly does NOT prevent other uses of annotations, nor does it require (or forbid)

How to combine a custom protocol with the Callable protocol?

感情迁移 提交于 2021-02-07 12:06:17
问题 I have a decorator that takes a function and returns the same function with some added attributes: import functools from typing import * def decorator(func: Callable) -> Callable: func.attr1 = "spam" func.attr2 = "eggs" return func How do I type hint the return value of decorator ? I want the type hint to convey two pieces of information: the return value is a Callable the return value has attributes attr1 and attr2 If I write a protocol, class CallableWithAttrs(Protocol): attr1: str attr2:

How to combine a custom protocol with the Callable protocol?

穿精又带淫゛_ 提交于 2021-02-07 12:04:42
问题 I have a decorator that takes a function and returns the same function with some added attributes: import functools from typing import * def decorator(func: Callable) -> Callable: func.attr1 = "spam" func.attr2 = "eggs" return func How do I type hint the return value of decorator ? I want the type hint to convey two pieces of information: the return value is a Callable the return value has attributes attr1 and attr2 If I write a protocol, class CallableWithAttrs(Protocol): attr1: str attr2:

What is the performance overhead of type-hinting in PHP?

蹲街弑〆低调 提交于 2021-02-07 06:44:25
问题 How significant is the performance overhead of type-hinting in PHP - is it significant enough to be a consideration in the decision to use it or not? 回答1: No, it's not significant. If you need to do something algorithmically intensive, like sound-processing or 3D-programming, you should use another programming language. if you want hard data, make a benchmark... <?php function with_typehint(array $bla) { if(count($bla) > 55) { die("Array to big"); } } function dont_typehint($bla) { if(count(

What type-hint contains both list and tuple?

我与影子孤独终老i 提交于 2021-02-07 04:48:14
问题 I have a function that can accept as input any variable that can be indexed, such as a list of a tuple. How do I indicate this in the type-hint of the function? 回答1: Your method is accepting a sequence, so use typing.Sequence. That's a generic, so you can specify what type of object(s) the sequence must contain: from typing import Sequence def foo(bar: Sequence[int]): # bar is a sequence of integers Quoting the Python glossary: An iterable which supports efficient element access using integer