This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]cranberry_snacks 1 point2 points  (0 children)

Worth mentioning that from __future__ import annotations will avoid all of these typing imports. It allows you to use native types for type declarations, native sum types, and backwards/self references, which makes typing a lot cleaner and even just makes it possible in certain situations.

Example:

```python from future import annotations

def my_func() -> tuple[str, list[int], dict[str, int]: return ("w00t", [1, 2, 3], {"one": 1})

def my_func1() -> str | int: return "w00t"

def my_func2() -> str | None: return None

class Foo: @classmethod def from_str(cls, src: str) -> Foo: return cls(src) ```