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

Reading the PEP and its rejected ideas with hindsight, maybe a different case could be made where a: ~X = b stands for a: X | Literal[b] = b [1]:

  • The tilde could stand for “approximately”, which is fairly common use (e.g. in pip requirements).
  • The generalisation from = None to any right-hand side, still in the sense of an is relation, might delineate more clearly the difference between the new, short syntax and the existing X | type(b).

  1. Requiring extension to non-literals? ↩︎

This is just an æsthetic preference, but, for what it’s worth: same here. I don’t want to see any hasty deprecations on Optional because I still need compatibility with older Pythons for quite a while, and I don’t want tools yelling at me until the yelling is actually actionable.

Beyond the æsthetics, though, objectively speaking, Optional is specialized jargon borrwed from other languages, whereas | None is a (shorter!) statement about the type based on generalized rules that you have to learn for lots of other reasons in Python typing. Once the functional issue (i.e.: legacy compatibility) is no longer a concern, I think (as pyupgrade apparently does) that we should consider | None to be the better way to do this since it is friendlier to those newer to typing.

5 Likes

Not sure it’s worth the effort, but I do love this as an idea :wink: