You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Annotated and Optional get_type_hints buggy interaction #90353
This is two closely related issues with get_type_hints on an optional annotated member. I'm testing with Annotated/get_type_hints from typing extensions on 3.8 and assuming they're backport equivalent to current behavior.
The first issue is get_type_hints has inconsistent behavior depending on whether annotation comes from a function with a None default or an attribute with a None default.
Attributes with a None default are not wrapped by get_type_hints, but function parameters. Which of the two behaviors is correct I don't know, but I'd expect the two to be equivalent annotations.
The second issue is for function arguments with a None default the optional wrapper happens even if the type inside annotated already has optional. Example,
For Annotated types I would expect any type rules like wrapping to apply only to the first argument and not the entire annotation. I mainly ran into this for a runtime type introspection library (similar in spirit to pydantic).
As a note include_extras being True or False while it changes type is an issue in either case. With include_extras as False the Annotated goes away, but the type still gets double wrapped as an Optional.
Could you try with 3.10 and the stdlib typing.Annotated please? There might
be changes (in the past a default of None automatically caused an Optional
to be added, but we changed our minds.--
--Guido (mobile)
Activity
med2277 commentedon Dec 29, 2021
This is two closely related issues with get_type_hints on an optional annotated member. I'm testing with Annotated/get_type_hints from typing extensions on 3.8 and assuming they're backport equivalent to current behavior.
The first issue is get_type_hints has inconsistent behavior depending on whether annotation comes from a function with a None default or an attribute with a None default.
Attributes with a None default are not wrapped by get_type_hints, but function parameters. Which of the two behaviors is correct I don't know, but I'd expect the two to be equivalent annotations.
The second issue is for function arguments with a None default the optional wrapper happens even if the type inside annotated already has optional. Example,
For Annotated types I would expect any type rules like wrapping to apply only to the first argument and not the entire annotation. I mainly ran into this for a runtime type introspection library (similar in spirit to pydantic).
As a note include_extras being True or False while it changes type is an issue in either case. With include_extras as False the Annotated goes away, but the type still gets double wrapped as an Optional.
gvanrossum commentedon Dec 29, 2021
Could you try with 3.10 and the stdlib typing.Annotated please? There might
be changes (in the past a default of None automatically caused an Optional
to be added, but we changed our minds.--
--Guido (mobile)
sobolevn commentedon Dec 30, 2021
I can verify that this happens on
3.10andmainbranches:Notice that 3rd case does not look correct:
{'x': typing.Optional[typing.Annotated[typing.Optional[str], 'd']]}In my opinion it should be
{'x': typing.Annotated[typing.Optional[str], 'd']}I will look into it! :)
sobolevn commentedon Dec 30, 2021
And on 3.9 as well.
26 remaining items
api: Avoid programming errors due to nested Annotated types.
api: Avoid programming errors due to nested Annotated types.
api: Avoid programming errors due to nested Annotated types.
api: Avoid programming errors due to nested Annotated types.
api: Avoid programming errors due to nested Annotated types.
api: Avoid programming errors due to nested Annotated types.
api: Avoid programming errors due to nested Annotated types.
api: Avoid programming errors due to nested Annotated types.
api: Avoid programming errors due to nested Annotated types.
api: Avoid programming errors due to nested Annotated types.
api: Avoid programming errors due to nested Annotated types.
api: Avoid programming errors due to nested Annotated types.
api: Avoid programming errors due to nested Annotated types.
api: Avoid programming errors due to nested Annotated types.
api: Avoid programming errors due to nested Annotated types.