-
Notifications
You must be signed in to change notification settings - Fork 27
fix: upload_forwardref #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import collections.abc | ||
import functools | ||
import operator | ||
import sys | ||
import types | ||
import typing | ||
from typing import ForwardRef, List | ||
import typing_extensions | ||
from typing_extensions import ParamSpec | ||
|
||
_GenericAlias = type(List[int]) | ||
SpecialType = (_GenericAlias, types.GenericAlias) | ||
|
||
if sys.version_info >= (3, 10): | ||
SpecialType += (types.UnionType,) | ||
|
||
if sys.version_info >= (3, 11): | ||
eval_type = typing._eval_type # type: ignore | ||
else: | ||
def _is_param_expr(arg): | ||
return arg is ... or isinstance(arg, (tuple, list, ParamSpec, typing_extensions._ConcatenateGenericAlias)) # type: ignore | ||
|
||
|
||
def _should_unflatten_callable_args(typ, args): | ||
"""Internal helper for munging collections.abc.Callable's __args__. | ||
|
||
The canonical representation for a Callable's __args__ flattens the | ||
argument types, see https://github.com/python/cpython/issues/86361. | ||
|
||
For example:: | ||
|
||
>>> import collections.abc | ||
>>> P = ParamSpec('P') | ||
>>> collections.abc.Callable[[int, int], str].__args__ == (int, int, str) | ||
True | ||
>>> collections.abc.Callable[P, str].__args__ == (P, str) | ||
True | ||
|
||
As a result, if we need to reconstruct the Callable from its __args__, | ||
we need to unflatten it. | ||
""" | ||
return ( | ||
typ.__origin__ is collections.abc.Callable | ||
and not (len(args) == 2 and _is_param_expr(args[0])) | ||
) | ||
|
||
|
||
def eval_type(t, globalns, localns, recursive_guard=frozenset()): | ||
"""Evaluate all forward references in the given type t. | ||
|
||
For use of globalns and localns see the docstring for get_type_hints(). | ||
recursive_guard is used to prevent infinite recursion with a recursive | ||
ForwardRef. | ||
""" | ||
if isinstance(t, ForwardRef): | ||
return t._evaluate(globalns, localns, recursive_guard) | ||
if isinstance(t, SpecialType): | ||
if isinstance(t, types.GenericAlias): | ||
args = tuple( | ||
ForwardRef(arg) if isinstance(arg, str) else arg | ||
for arg in t.__args__ | ||
) | ||
is_unpacked = getattr(t, "__unpacked__", False) | ||
if _should_unflatten_callable_args(t, args): | ||
t = t.__origin__[(args[:-1], args[-1])] # type: ignore | ||
else: | ||
t = t.__origin__[args] # type: ignore | ||
if is_unpacked: | ||
t = typing_extensions.Unpack[t] | ||
ev_args = tuple(eval_type(a, globalns, localns, recursive_guard) for a in t.__args__) # type: ignore | ||
if ev_args == t.__args__: # type: ignore | ||
return t | ||
if isinstance(t, types.GenericAlias): | ||
return types.GenericAlias(t.__origin__, ev_args) | ||
if hasattr(types, "UnionType") and isinstance(t, types.UnionType): # type: ignore | ||
return functools.reduce(operator.or_, ev_args) | ||
return t.copy_with(ev_args) # type: ignore | ||
return t |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.