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 commented on 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 commented on 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 commented on Dec 30, 2021
I can verify that this happens on 3.10 and main branches:
from typing import Annotated, Optional, get_type_hints class Foo: def __init__(self, x: Annotated[Optional[str], "d"] = None): ... class Foo2: x: Annotated[Optional[str], "d"] = None print(get_type_hints(Foo.__init__, include_extras=False)) # ok # {'x': typing.Optional[str]} print(get_type_hints(Foo2, include_extras=False)) # ok # {'x': typing.Optional[str]} print(get_type_hints(Foo.__init__, include_extras=True)) # not ok? # {'x': typing.Optional[typing.Annotated[typing.Optional[str], 'd']]} print(get_type_hints(Foo2, include_extras=True)) # ok # {'x': typing.Annotated[typing.Optional[str], 'd']}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 commented on Dec 30, 2021
And on 3.9 as well.
25 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.