Skip to content

Instantly share code, notes, and snippets.

@neutrinoceros
Last active September 19, 2021 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neutrinoceros/0eaa046a00689254dee0738c65c9bdfc to your computer and use it in GitHub Desktop.
Save neutrinoceros/0eaa046a00689254dee0738c65c9bdfc to your computer and use it in GitHub Desktop.
Simple Python typing changes from Python 3.7
# in Python 3.6
from typing import Dict, List, Tuple
def eggs(a: int, b: List[int]) -> Dict[str, Tuple[int, ...]]:
...
# in Python 3.7+, we can use builtin types instead of their typing equivalents
# this becomes the default in Python 3.9
# see https://www.python.org/dev/peps/pep-0585/
from __future__ import annotations
def eggs(a: int, b: list[int]) -> dict[str, tuple[int, ...]]:
...
# in Python 3.6
from typing import Optional, Union
def bacon(a: Union[int, str], b: Optional[int]):
...
# in Python 3.7+, the pipe operator "|" (a.k.a "logical and") can be used to replace `Union` and `Optional`
# this becomes the default in Python 3.10
# see https://www.python.org/dev/peps/pep-0604/
from __future__ import annotations
def bacon(a: int | str, b: int | None):
...
# in Python 3.6
class Spam:
@classmethod
def spamify(cls, a) -> "Spam": # return type must be quoted to avoid a NameError
...
# in Python 3.7+
# annotations are evaluated as strings, so they never need to be quoted
# this is currently set to become the default in Python 3.11 (2022)
# see https://mail.python.org/archives/list/python-dev@python.org/thread/CLVXXPQ2T2LQ5MP2Y53VVQFCXYWQJHKZ/
from __future__ import annotations
class Spam:
@classmethod
def spamify(cls, a) -> Spam:
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment