Clarification for PEP 604: is `foo: int | None` to replace all use of `foo: Optional[int]`

Some quick notes:

  • Philosophy of whether default values should be part of documented interface reminds me of:
    Should `None` defaults for optional arguments be discouraged? and Signatures, a call to action

  • It is true that there isn’t a particularly ergonomic way to type a function in two different ways, for its documented interface and for its internal use. Some options here include: using @overload to describe your interface, using two separate functions with two type signatures, using # type: ignore. Like Jelle says, I’d welcome more discussion on whether it’s worth finding better typing ergonomics here.

  • On Oscar’s comment about making it easier to type Callable with defaulted arguments. PEP 677 extended syntax is what we had in mind here: PEP 677 – Callable Type Syntax | peps.python.org However, PEP 677 was rejected.

  • On Alex’s comment about a potential deprecation of Union or Optional. I’d go further than “near future” — it would be very disruptive to deprecate and remove these symbols, there would be little benefit, and so I’d vote to not pursue this. However, I strongly recommend the new syntax, since I find it’s less likely to confuse new users and I find it more readable.

6 Likes