python-typing

How do I annotate a callable with *args and **kwargs?

走远了吗. 提交于 2021-01-19 06:21:46
问题 I have a function which returns a function. I would like to find a proper type annotation. However, the returned function has *args and *kwargs . How is that annotated within Callable[[Parameters???], ReturnType] ? Example: from typing import Callable import io import pandas as pd def get_conversion_function(file_type: str) -> Callable[[io.BytesIO, TODO], pd.DataFrame]: def to_csv(bytes_, *args, **kwargs): return pd.read_csv(bytes_, **kwargs) if file_type == "csv": return to_csv 回答1: As I

Expected type 'List[A]' (matched generic type 'List[_T]'), got 'List[B]' instead on correctly typed lists

隐身守侯 提交于 2021-01-03 07:07:52
问题 from typing import List class Base(object): pass class A(Base): pass class B(Base): pass a: List[A] = [] b: List[B] = [] c: List[Base] = a + b I am getting Expected type 'List[A]' (matched generic type 'List[_T]'), got 'List[B]' instead on b . How do I get correct warnings, as obviously the types are fine. 回答1: These types are not fine. List is invariant, meaning that a List[X] is not a substitute for List[Y] unless X and Y are exactly equal. Similarly, A <: Base does not imply List[A] <:

Expected type 'List[A]' (matched generic type 'List[_T]'), got 'List[B]' instead on correctly typed lists

◇◆丶佛笑我妖孽 提交于 2021-01-03 07:03:23
问题 from typing import List class Base(object): pass class A(Base): pass class B(Base): pass a: List[A] = [] b: List[B] = [] c: List[Base] = a + b I am getting Expected type 'List[A]' (matched generic type 'List[_T]'), got 'List[B]' instead on b . How do I get correct warnings, as obviously the types are fine. 回答1: These types are not fine. List is invariant, meaning that a List[X] is not a substitute for List[Y] unless X and Y are exactly equal. Similarly, A <: Base does not imply List[A] <:

Expected type 'List[A]' (matched generic type 'List[_T]'), got 'List[B]' instead on correctly typed lists

点点圈 提交于 2021-01-03 07:01:31
问题 from typing import List class Base(object): pass class A(Base): pass class B(Base): pass a: List[A] = [] b: List[B] = [] c: List[Base] = a + b I am getting Expected type 'List[A]' (matched generic type 'List[_T]'), got 'List[B]' instead on b . How do I get correct warnings, as obviously the types are fine. 回答1: These types are not fine. List is invariant, meaning that a List[X] is not a substitute for List[Y] unless X and Y are exactly equal. Similarly, A <: Base does not imply List[A] <:

Python typing: Dynamically Create Literal Alias from List of Valid Values

北城以北 提交于 2020-12-21 04:16:00
问题 I have a function which validates its argument to accept only values from a given list of valid options. Typing-wise, I reflect this behavior using a Literal type alias, like so: from typing import Literal VALID_ARGUMENTS = ['foo', 'bar'] Argument = Literal['foo', 'bar'] def func(argument: 'Argument') -> None: if argument not in VALID_ARGUMENTS: raise ValueError( f'argument must be one of {VALID_ARGUMENTS}' ) # ... This is a violation of the DRY principle, because I have to rewrite the list

Python typing: Dynamically Create Literal Alias from List of Valid Values

萝らか妹 提交于 2020-12-21 04:14:24
问题 I have a function which validates its argument to accept only values from a given list of valid options. Typing-wise, I reflect this behavior using a Literal type alias, like so: from typing import Literal VALID_ARGUMENTS = ['foo', 'bar'] Argument = Literal['foo', 'bar'] def func(argument: 'Argument') -> None: if argument not in VALID_ARGUMENTS: raise ValueError( f'argument must be one of {VALID_ARGUMENTS}' ) # ... This is a violation of the DRY principle, because I have to rewrite the list