This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author sobolevn
Recipients gvanrossum, kj, med2277, sobolevn
Date 2021-12-30.10:38:56
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1640860737.17.0.142442499658.issue46195@roundup.psfhosted.org>
In-reply-to
Content
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:
- correct `value` is passed to `_eval_type`, correct result `typing.Annotated[typing.Optional[str], 'd']` is returned at this point
- then `if name in defaults and defaults[name] is None:` adds extra `Optional` annotation on top of `Annotated`

> 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:
- https://bugs.python.org/issue42921
- https://bugs.python.org/issue42288
History
Date User Action Args
2021-12-30 10:38:57sobolevnsetrecipients: + sobolevn, gvanrossum, kj, med2277
2021-12-30 10:38:57sobolevnsetmessageid: <1640860737.17.0.142442499658.issue46195@roundup.psfhosted.org>
2021-12-30 10:38:57sobolevnlinkissue46195 messages
2021-12-30 10:38:56sobolevncreate