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.
So, what happens there:
- correct
value
is passed to_eval_type
, correct resulttyping.Annotated[typing.Optional[str], 'd']
is returned at this point - then
if name in defaults and defaults[name] is None:
adds extraOptional
annotation on top ofAnnotated
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:
- Remove
Optional
inference withNone
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). - 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: