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

Commita08b623

Browse files
authored
Fix noncommutative joins with bounded TypeVars (#20345)
Fixes#20344
1 parent8de78b8 commita08b623

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

‎mypy/join.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,15 @@ def visit_erased_type(self, t: ErasedType) -> ProperType:
297297
returnself.s
298298

299299
defvisit_type_var(self,t:TypeVarType)->ProperType:
300-
ifisinstance(self.s,TypeVarType)andself.s.id==t.id:
301-
ifself.s.upper_bound==t.upper_bound:
302-
returnself.s
303-
returnself.s.copy_modified(upper_bound=join_types(self.s.upper_bound,t.upper_bound))
300+
ifisinstance(self.s,TypeVarType):
301+
ifself.s.id==t.id:
302+
ifself.s.upper_bound==t.upper_bound:
303+
returnself.s
304+
returnself.s.copy_modified(
305+
upper_bound=join_types(self.s.upper_bound,t.upper_bound)
306+
)
307+
# Fix non-commutative joins
308+
returnget_proper_type(join_types(self.s.upper_bound,t.upper_bound))
304309
else:
305310
returnself.default(self.s)
306311

‎mypy/test/testtypes.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,35 @@ def test_join_type_type_type_var(self) -> None:
10511051
self.assert_join(self.fx.type_a,self.fx.t,self.fx.o)
10521052
self.assert_join(self.fx.t,self.fx.type_a,self.fx.o)
10531053

1054+
deftest_join_type_var_bounds(self)->None:
1055+
tvar1=TypeVarType(
1056+
"tvar1",
1057+
"tvar1",
1058+
TypeVarId(-100),
1059+
[],
1060+
self.fx.o,
1061+
AnyType(TypeOfAny.from_omitted_generics),
1062+
INVARIANT,
1063+
)
1064+
any_type=AnyType(TypeOfAny.special_form)
1065+
tvar2=TypeVarType(
1066+
"tvar2",
1067+
"tvar2",
1068+
TypeVarId(-101),
1069+
[],
1070+
upper_bound=UnionType(
1071+
[
1072+
TupleType([any_type],self.fx.std_tuple),
1073+
TupleType([any_type,any_type],self.fx.std_tuple),
1074+
]
1075+
),
1076+
default=AnyType(TypeOfAny.from_omitted_generics),
1077+
variance=INVARIANT,
1078+
)
1079+
1080+
self.assert_join(tvar1,tvar2,self.fx.o)
1081+
self.assert_join(tvar2,tvar1,self.fx.o)
1082+
10541083
# There are additional test cases in check-inference.test.
10551084

10561085
# TODO: Function types + varargs and default args.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp