type-hinting

How do I correctly add type-hints to Mixin classes?

泪湿孤枕 提交于 2020-08-22 04:43:08
问题 Consider the following example. The example is contrived but illustrates the point in a runnable example: class MultiplicatorMixin: def multiply(self, m: int) -> int: return self.value * m class AdditionMixin: def add(self, b: int) -> int: return self.value + b class MyClass(MultiplicatorMixin, AdditionMixin): def __init__(self, value: int) -> None: self.value = value instance = MyClass(10) print(instance.add(2)) print(instance.multiply(2)) When executed this will give the following output:

PEP 484: exclusive type for type hint

拜拜、爱过 提交于 2020-08-06 05:02:16
问题 Could I specify exclusive type? Something like this: def foo(bar: Not[str]) -> None: assert not isinstance(bar, str) print(type(bar)) 回答1: PEP 484 does not include a built-in way of specifying a "blacklisted" type. Doing that kind of thing typically doesn't really make sense -- the only reason I can think of why you might want to do something like this is if you wanted to distinguish between str and Iterable[str] or Sequence[str] in some fashion. If that's the case, PEP 484 unfortunately

PEP 484: exclusive type for type hint

╄→尐↘猪︶ㄣ 提交于 2020-08-06 05:02:11
问题 Could I specify exclusive type? Something like this: def foo(bar: Not[str]) -> None: assert not isinstance(bar, str) print(type(bar)) 回答1: PEP 484 does not include a built-in way of specifying a "blacklisted" type. Doing that kind of thing typically doesn't really make sense -- the only reason I can think of why you might want to do something like this is if you wanted to distinguish between str and Iterable[str] or Sequence[str] in some fashion. If that's the case, PEP 484 unfortunately

How can I type hint an attribute in Python 3.5?

ⅰ亾dé卋堺 提交于 2020-08-04 02:52:38
问题 I have a class where I want the initial value of an attribute to be None : class SomeClass: def __init__(self): self.some_attribute = None How can I add type hinting, so that the IDE understands that some_attribute is usually of the type AnotherClass ? 回答1: In Python 3.5, you have to write self.some_attribute = None # type: AnotherClass Since Python 3.6, new type hinting syntax was added for variables (PEP 526): self.some_attribute: AnotherClass = None This will probably make every type

Mypy/typeshed stubs for Pandas

冷暖自知 提交于 2020-07-28 12:15:27
问题 Just checking to see if anybody listening has already generated a sort-of-working set of mypy/typeshed stubs for Pandas. I naively ran stubgen over the local Pandas install which generated some errors. I can go with what I have to start with, but was hoping someone else had pushed the ball further. (Nothing obvious turned up on GitHub, though there is an old ticket for stubs.) 回答1: I have not yet found stubs for pandas, however someone has created some for NumPy: https://github.com/machinalis

mypy, type hint: Union[float, int] -> is there a Number type?

一个人想着一个人 提交于 2020-07-17 03:07:11
问题 mypy is really handy and catches a lot of bugs, but when I write "scientific" applications, I often end up doing: def my_func(number: Union[float, int]): # Do something number is either a float or int, depending on the user's input. Is there an official way to do that? 回答1: Use float only , as int is implied in that type: def my_func(number: float): PEP 484 Type Hints specifically states that: Rather than requiring that users write import numbers and then use numbers.Float etc., this PEP

How to get class variables and type hints?

醉酒当歌 提交于 2020-07-06 10:27:08
问题 Assume I define a class with class level variables with type hints (e.g. something like the new python 3.7 dataclasses ) class Person: name: str age: int def parse_me(self): "what do I do here??" How can I get the pairs of (variable name, variable type) ? 回答1: typing.get_type_hints is another method that doesn't involve accessing magic properties directly: from typing import get_type_hints class Person: name: str age: int get_type_hints(Person) # returns {'name': <class 'str'>, 'age': <class

Type hint for a file or file-like object?

我怕爱的太早我们不能终老 提交于 2020-07-04 06:53:39
问题 Is there any correct type hint to use for a file or file-like object in Python? For example, how would I type-hint the return value of this function? def foo(): return open('bar') 回答1: Use either the typing.TextIO or typing.BinaryIO types, for files opened in text mode or binary mode respectively. From the docs: class typing.IO Wrapper namespace for I/O stream types. This defines the generic type IO[AnyStr] and aliases TextIO and BinaryIO for respectively IO[str] and IO[bytes] . These

Python void return type annotation

寵の児 提交于 2020-06-24 07:01:37
问题 In python 3.x, it is common to use return type annotation of a function, such as: def foo() -> str: return "bar" What is the correct annotation for the "void" type? I'm considering 3 options: def foo() -> None: not logical IMO, because None is not a type, def foo() -> type(None): using the best syntax I know for obtaining NoneType , def foo(): omit explicit return type information. Option 2. seems the most logical to me, but I've already seen some instances of 1. 回答1: This is straight from

Python void return type annotation

我怕爱的太早我们不能终老 提交于 2020-06-24 07:01:27
问题 In python 3.x, it is common to use return type annotation of a function, such as: def foo() -> str: return "bar" What is the correct annotation for the "void" type? I'm considering 3 options: def foo() -> None: not logical IMO, because None is not a type, def foo() -> type(None): using the best syntax I know for obtaining NoneType , def foo(): omit explicit return type information. Option 2. seems the most logical to me, but I've already seen some instances of 1. 回答1: This is straight from