typed python: using the classes’ own type inside class definition [duplicate]

When Foo.__eq__ is being defined the name Foo is still unbound, because the class itself hasn’t yet been created. Remember: function arguments are evaluated at function definition time, not at function call time. From Python 3.7+ you can postpone evaluation of annotations by adding this import at the top of the module: from __future__ import … Read more

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

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 proposes a straightforward shortcut that is almost as effective: when an argument is annotated as having type float, an argument … Read more

Iterable objects and array type hinting?

I think you mean instanceof Iterator, PHP doesn’t have an Iterable interface. It does have a Traversable interface though. Iterator and IteratorAggregate both extend Traversable (and AFAIK they are the only ones to do so). But no, objects implementing Traversable won’t pass the is_array() check, nor there is a built-in is_iterable() function. A check you … Read more

Difference between defining typing.Dict and dict?

There is no real difference between using a plain typing.Dict and dict, no. However, typing.Dict is a Generic type * that lets you specify the type of the keys and values too, making it more flexible: def change_bandwidths(new_bandwidths: typing.Dict[str, str], user_id: int, user_name: str) -> bool: As such, it could well be that at some … Read more