Typing and mypy usability: focus

PEP 585 (draft)

  • Allow list[int] an other builtins
  • Allow subscripting ABCs collections.abc.Iterable[int] and some concrete collections
  • Implementation: PEP 560 __class_getitem__() and __mro_items__()
  • Still need some validation to prohibit list[-1] and dict[str][int]
  • Extra: allow type application for functions make_node[int]() similar to Node[int]()
  • Make it a separate PEP

PEP 604 (early draft)

  • Allow X | Y as a shorthand for Union[X, Y]
  • Allow ~X as a shorthand for Optional[X]
  • Should we push for X? or ?X syntax for latter? (probably yes)
  • Yes, but make it a separate PEP (likely post-PEP 505)
  • Should we push for (X, Y) -> Z as a shorthand for Callable[[X, Y], Z]? (probably no)
  • We don’t do this
  • Also allow isinstance(obj, int | str) ??? This was previously removed from typing
  • It is probably OK (useful as aliases)

Nicer forward references? (no)

  • Allow from typing import f; f.NotYetDefined as a nicer form of "NotYetDefined" (credits to Mark Shannon)
  • Avoids ambiguity in some cases and looks nicer
  • Implementation is very simple both at runtime and in type checkers
  • Deprecate string literals?
  • Add a hook to allow objects to be valid types at runtime
  • Make a s 3rd party library?