Typing and mypy usability

Allow using lowercase list[int]

  • Previously this was hard to enable because it required adding __getitem__() to type
  • Now PEP 560 introduced  __class_getitem__()
  • Let’s add it to the generic builtins
  • Same story applies to collections.abc.Mapping etc.
  • Also IO, Pattern, Match (latter will be importable from re)
  • Comments:
  • Many people want this
  • Jukka and Sully volunteered to write/implement a PEP

Type application on functions

  • We support type application for classes: Node[int]()
  • There is a common user request to allow overriding failed type inference for functions
  • Current workaround is use a dummy class with a single class method (ugly and non-intuitive)
  • Can now be also fixed with a __getitem__()
  • Comments:
  • Not so popular as the previous one
  • May also use bind(func, [int, str])

Forward references

  • Forward references require string escapes: List["Vector"]
  • Possible solution: add runtime hook for name errors — very bad
  • Another solution: add an AST hook (PEP 511, rejected) — less bad, but still bad
  • PEP 563 fixed most common situations

Forward references: what PEP 563 didn’t solve

  • Base classes (nested in [...])
  • Type aliases
  • Cast targets
  • Type applications DefaultDict[Vector]()
  • Superclasses in NewType()
  • Type variable definitions: value constraints and upper bound

Forward references: what are the “worst” cases

  • Frequency analysis at Dropbox (absolute numbers for 3M+ annotated lines)
  • Type variables: 87
  • Type aliases: 40
  • Cast targets: 34
  • Base classes: 10
  • Are they actually bad? Python is interpreted language, compiled languages don’t have them, also mypyc 🙂 
  • Comments:
  • Łukasz will continue working on PEP 585, maybe split it
  • Also we can use from typing import F; F.NotYetDefined as nicer and less ambiguous syntax for cases that can’t be fixed (like base classes)

Avoid imports of special forms from typing

  • It may make sense to allow using Union, Any, Optional without importing them from typing
  • One possible solution: make them builtins (Any may be hard)
  • Another option: allow un-imported names only in annotations with PEP 563
  • Some of these may become irrelevant if we propose a special syntax (next slide)

Better syntax for special forms

  • Usage frequency at Dropbox: 
  • Optional 71.0K
  • Tuple 8.7K
  • Callable 6.5K