Message 409357 - Python tracker (original) (raw)

As Guido said, the root cause of this problem is because None default automatically adds Optional to the resulting type.

Source: https://github.com/python/cpython/blob/8d7644fa64213207b8dc6f555cb8a02bfabeced2/Lib/typing.py#L1854-L1856

So, what happens there:

in the past a default of None automatically caused an Optional to be added, but we changed our minds

Guido, are you talking about https://github.com/python/typing/issues/275 ?

Now all type-checkers (AFAIK) support something similar to --no-implicit-optional mode.

Having this in mind, I see different solutions to the current problem:

  1. Remove Optional inference with None default. This is going to be a some-what breaking change. The only positive side of this is that we can really simplify our code (mainly because the other solution is to complicate our code even more).
  2. Or we can change this place to explicitly check for Annotated type and its internal type. This should be the easiest to write and backport. But, it still has some complexity to it. I think that this is a better solution: we don't break existing behavior, change is local and pretty trivial.

Also caused by this: