Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix issues with type aliases and new style unions#14181

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JukkaL merged 16 commits intomasterfromfix-type-union
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
16 commits
Select commitHold shift + click to select a range
8d90d97
Fix issues with type aliases and new style unions
JukkaLNov 4, 2022
acd93db
Black
JukkaLNov 7, 2022
51660b0
Fix test case
JukkaLNov 7, 2022
f73ee51
WIP add failing cases
JukkaLNov 7, 2022
096d8bd
Fix aliases in stubs
JukkaLNov 24, 2022
3e4e809
Update test case
JukkaLNov 24, 2022
a732e7a
Add test case covering #14158
JukkaLNov 24, 2022
41cd383
Minor tweaks
JukkaLNov 24, 2022
afc8754
Fix TreeTransform
JukkaLNov 24, 2022
ceee87b
Fix aststrip
JukkaLNov 24, 2022
8666b55
A few additional fixes + add test case
JukkaLNov 24, 2022
7a7284b
Fix type check
JukkaLNov 24, 2022
0716501
Black
JukkaLNov 24, 2022
d178f7f
Merge remote-tracking branch 'origin/master' into fix-type-union
JukkaLNov 24, 2022
ac2df6c
Remove accidentally added test stub
JukkaLNov 24, 2022
3980050
Merge branch 'master' into fix-type-union
JukkaLNov 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
Fix issues with type aliases and new style unions
Fix aliases like this and other aliases involving new-style unions:```A = type[int] | str```Fixes#12392.Fixes#14158.
  • Loading branch information
@JukkaL
JukkaL committedNov 24, 2022
commit8d90d97081906e0af00d3019ef97abe50c6767d0
3 changes: 3 additions & 0 deletionsmypy/checkexpr.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2847,6 +2847,9 @@ def visit_ellipsis(self, e: EllipsisExpr) -> Type:

def visit_op_expr(self, e: OpExpr) -> Type:
"""Type check a binary operator expression."""
if e.analyzed:
# It's actually a type expression X | Y.
return self.accept(e.analyzed)
if e.op == "and" or e.op == "or":
return self.check_boolean_op(e, e)
if e.op == "*" and isinstance(e.left, ListExpr):
Expand Down
19 changes: 16 additions & 3 deletionsmypy/nodes.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1968,10 +1968,20 @@ def accept(self, visitor: ExpressionVisitor[T]) -> T:


class OpExpr(Expression):
"""Binary operation (other than . or [] or comparison operators,
which have specific nodes)."""
"""Binary operation.

__slots__ = ("op", "left", "right", "method_type", "right_always", "right_unreachable")
The dot (.), [] and comparison operators have more specific nodes.
"""

__slots__ = (
"op",
"left",
"right",
"method_type",
"right_always",
"right_unreachable",
"analyzed",
)

__match_args__ = ("left", "op", "right")

Expand All@@ -1984,6 +1994,8 @@ class OpExpr(Expression):
right_always: bool
# Per static analysis only: Is the right side unreachable?
right_unreachable: bool
# Used for expressions that represent type X | Y in some contexts
analyzed: TypeAliasExpr | None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This should now be handled in various visitors. Three cases come to my mind: First,treetransform.py this is needed so that this error will not re-appear in a test case like this

T=TypeVar("T",int,str)deffoo(x:T)->T:A=type[int]|strreturnx

Second,aststrip.py, be sure that when you switch imported names from types to variables, you do get an error about missing__or__ on update. Third,semanal_typeargs.py (usesMixedTraverserVisitor), since we should not carry malformed instances around (with number of type args), they may cause crashes, add a test just in case with a malformed instance in| alias.

And in general adding in to the basicTraverserVisitor is a good idea. Maybe just grep fordef visit_index_expr( and see where we useanalyzed.

hauntsaninja reacted with heart emoji
Copy link
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Very good points! I'll fix these.


def __init__(self, op: str, left: Expression, right: Expression) -> None:
super().__init__()
Expand All@@ -1993,6 +2005,7 @@ def __init__(self, op: str, left: Expression, right: Expression) -> None:
self.method_type = None
self.right_always = False
self.right_unreachable = False
self.analyzed = None

def accept(self, visitor: ExpressionVisitor[T]) -> T:
return visitor.visit_op_expr(self)
Expand Down
2 changes: 1 addition & 1 deletionmypy/semanal.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3440,7 +3440,7 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> bool:
no_args=no_args,
eager=eager,
)
if isinstance(s.rvalue, (IndexExpr, CallExpr)): # CallExpr is for `void = type(None)`
if isinstance(s.rvalue, (IndexExpr, CallExpr, OpExpr)): # CallExpr is for `void = type(None)`
s.rvalue.analyzed = TypeAliasExpr(alias_node)
s.rvalue.analyzed.line = s.line
# we use the column from resulting target, to get better location for errors
Expand Down
28 changes: 28 additions & 0 deletionstest-data/unit/pythoneval.test
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1682,8 +1682,19 @@ Opt4 = float | None

A = Type[int] | str
B: TypeAlias = Type[int] | str
C = type[int] | str

D = type[int] | str
x: D
reveal_type(x)
E: TypeAlias = type[int] | str
y: E
reveal_type(y)
F = list[type[int] | str]
[out]
_testTypeAliasWithNewStyleUnion.py:5: note: Revealed type is "typing._SpecialForm"
_testTypeAliasWithNewStyleUnion.py:24: note: Revealed type is "Union[Type[builtins.int], builtins.str]"
_testTypeAliasWithNewStyleUnion.py:27: note: Revealed type is "Union[Type[builtins.int], builtins.str]"

[case testTypeAliasWithNewStyleUnionInStub]
# flags: --python-version 3.7
Expand DownExpand Up@@ -1735,3 +1746,20 @@ _testEnumNameWorkCorrectlyOn311.py:12: note: Revealed type is "Union[Literal[1]?
_testEnumNameWorkCorrectlyOn311.py:13: note: Revealed type is "Literal['X']?"
_testEnumNameWorkCorrectlyOn311.py:14: note: Revealed type is "builtins.int"
_testEnumNameWorkCorrectlyOn311.py:15: note: Revealed type is "builtins.int"

[case testTypeAliasNotSupportedWithNewStyleUnion]
# flags: --python-version 3.9
from typing_extensions import TypeAlias
A = type[int] | str
B = str | type[int]
C = str | int
D: TypeAlias = str | int
[out]
_testTypeAliasNotSupportedWithNewStyleUnion.py:3: error: Invalid type alias: expression is not a valid type
_testTypeAliasNotSupportedWithNewStyleUnion.py:3: error: Value of type "Type[type]" is not indexable
_testTypeAliasNotSupportedWithNewStyleUnion.py:4: error: Invalid type alias: expression is not a valid type
_testTypeAliasNotSupportedWithNewStyleUnion.py:4: error: Value of type "Type[type]" is not indexable
_testTypeAliasNotSupportedWithNewStyleUnion.py:5: error: Invalid type alias: expression is not a valid type
_testTypeAliasNotSupportedWithNewStyleUnion.py:5: error: Unsupported left operand type for | ("Type[str]")
_testTypeAliasNotSupportedWithNewStyleUnion.py:6: error: Invalid type alias: expression is not a valid type
_testTypeAliasNotSupportedWithNewStyleUnion.py:6: error: Unsupported left operand type for | ("Type[str]")

[8]ページ先頭

©2009-2025 Movatter.jp