Repository navigation
Support GenericAlias in typing #84576
Description
Activity
Currently typing functions get_origin(), get_args() and get_type_hints() do not support GenericAlias.
>>> from typing import * >>> get_origin(List[int]) <class 'list'> >>> get_origin(list[int]) >>> get_args(List[int]) (<class 'int'>,) >>> get_args(list[int]) () >>> def foo(x: List[ForwardRef('X')], y: list[ForwardRef('X')]) -> None: ... ... >>> class X: ... ... >>> get_type_hints(foo) {'x': typing.List[__main__.X], 'y': list[ForwardRef('X')], 'return': <class 'NoneType'>}
The proposed PR fixes this.
- added3.9 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
on Apr 26, 2020 @serhiy-storchaka
why doesget_origin(list)returnsNonewhileget_origin(List)returns<class 'list'>shouldn't list behaves exactly the same as List? (the same question applies to all generics)
@danielkatzan Looks like an omission in the code for
get_origin(). Would you like to submit a PR to fix it? I can't promise that it will make it into 3.11 but it will be in 3.11.1.@gvanrossum Sure, I would be happy to, but could use a little pointer here
Not sure how this can be accomplishednote while
type(list[str])is<class 'types.GenericAlias'>and has the__origin__attribute,type(list)is actually<class 'type'>and doesn't have an__origin__attributeI can add an explicit if, checking if the tp is actually dict\list etc, and return that instead of None
will that be a valid approach? or just masking a deeper issue that might pop up later in a different location?
This issue was closed 2 years ago, please open a new issue.
The fact that
get_origin(List)returnslistis not documented. It looks to me like an implementation artifact.The fact that
get_origin(List)returnslistis not documented. It looks to me like an implementation artifact.Changing that would be a breaking change at this point (at least... it would break code that I, personally, have written).
opened this #95539
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: