-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Support new union syntax in stubs always in runtime context #10771
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
5 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
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
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 |
---|---|---|
|
@@ -69,7 +69,7 @@ def analyze_type_alias(node: Expression, | |
plugin: Plugin, | ||
options: Options, | ||
is_typeshed_stub: bool, | ||
allow_unnormalized: bool = False, | ||
allow_new_syntax: bool = False, | ||
allow_placeholder: bool = False, | ||
in_dynamic_func: bool = False, | ||
global_scope: bool = True) -> Optional[Tuple[Type, Set[str]]]: | ||
|
@@ -80,12 +80,12 @@ def analyze_type_alias(node: Expression, | |
Return None otherwise. 'node' must have been semantically analyzed. | ||
""" | ||
try: | ||
type = expr_to_unanalyzed_type(node, options) | ||
type = expr_to_unanalyzed_type(node, options, allow_new_syntax) | ||
except TypeTranslationError: | ||
api.fail('Invalid type alias: expression is not a valid type', node) | ||
return None | ||
analyzer = TypeAnalyser(api, tvar_scope, plugin, options, is_typeshed_stub, | ||
allow_unnormalized=allow_unnormalized, defining_alias=True, | ||
allow_new_syntax=allow_new_syntax, defining_alias=True, | ||
allow_placeholder=allow_placeholder) | ||
analyzer.in_dynamic_func = in_dynamic_func | ||
analyzer.global_scope = global_scope | ||
|
@@ -126,7 +126,7 @@ def __init__(self, | |
is_typeshed_stub: bool, *, | ||
defining_alias: bool = False, | ||
allow_tuple_literal: bool = False, | ||
allow_unnormalized: bool = False, | ||
allow_new_syntax: bool = False, | ||
allow_unbound_tvars: bool = False, | ||
allow_placeholder: bool = False, | ||
report_invalid_types: bool = True) -> None: | ||
|
@@ -141,9 +141,9 @@ def __init__(self, | |
self.allow_tuple_literal = allow_tuple_literal | ||
# Positive if we are analyzing arguments of another (outer) type | ||
self.nesting_level = 0 | ||
# Should we allow unnormalized types like `list[int]` | ||
# (currently allowed in stubs)? | ||
self.allow_unnormalized = allow_unnormalized | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update the comment above to reflect the new name? |
||
# Should we allow new type syntax when targeting older Python versions | ||
# like 'list[int]' or 'X | Y' (allowed in stubs)? | ||
self.allow_new_syntax = allow_new_syntax | ||
# Should we accept unbound type variables (always OK in aliases)? | ||
self.allow_unbound_tvars = allow_unbound_tvars or defining_alias | ||
# If false, record incomplete ref if we generate PlaceholderType. | ||
|
@@ -199,7 +199,7 @@ def visit_unbound_type_nonoptional(self, t: UnboundType, defining_literal: bool) | |
return hook(AnalyzeTypeContext(t, t, self)) | ||
if (fullname in get_nongen_builtins(self.options.python_version) | ||
and t.args and | ||
not self.allow_unnormalized and | ||
not self.allow_new_syntax and | ||
not self.api.is_future_flag_set("annotations")): | ||
self.fail(no_subscript_builtin_alias(fullname, | ||
propose_alt=not self.defining_alias), t) | ||
|
@@ -282,7 +282,7 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Opt | |
elif (fullname == 'typing.Tuple' or | ||
(fullname == 'builtins.tuple' and (self.options.python_version >= (3, 9) or | ||
self.api.is_future_flag_set('annotations') or | ||
self.allow_unnormalized))): | ||
self.allow_new_syntax))): | ||
# Tuple is special because it is involved in builtin import cycle | ||
# and may be not ready when used. | ||
sym = self.api.lookup_fully_qualified_or_none('builtins.tuple') | ||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TBH I am not sure how this logic work correctly for
A = list[int | None]
, maybe add a short comment?