Skip to content

Commit 40f8ca8

Browse files
1 check remaining
1 parent db7ed35 commit 40f8ca8

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

mypy/checkexpr.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2344,6 +2344,18 @@ def infer_function_type_arguments(
23442344
# Otherwise, we use joint.
23452345
combined_solution.append(joint_tp)
23462346

2347+
# if the outer solution is more concrete than the joint solution, use the outer solution (2 step)
2348+
if all(
2349+
(joint_tp is None and outer_tp is None)
2350+
or (
2351+
(joint_tp is not None and outer_tp is not None)
2352+
and is_subtype(outer_tp, joint_tp)
2353+
)
2354+
for outer_tp, joint_tp in zip(outer_solution[0], target_solution[0])
2355+
):
2356+
use_joint = False
2357+
use_outer = True
2358+
23472359
_num = arg_pass_nums
23482360
_c0 = constraints
23492361
_c1 = extra_constraints
@@ -2362,22 +2374,9 @@ def infer_function_type_arguments(
23622374
_s2 = inner_solution[0]
23632375
_s3 = joint_solution[0]
23642376
_s4 = reverse_joint_solution[0]
2365-
_s5 = combined_solution
2366-
2377+
_t0 = target_solution[0]
23672378
_u0 = use_inner, use_outer, use_joint
23682379

2369-
# if the outer solution is more concrete than the joint solution, use the outer solution (2 step)
2370-
if all(
2371-
(joint_tp is None and outer_tp is None)
2372-
or (
2373-
(joint_tp is not None and outer_tp is not None)
2374-
and is_subtype(outer_tp, joint_tp)
2375-
)
2376-
for outer_tp, joint_tp in zip(outer_solution[0], target_solution[0])
2377-
):
2378-
use_joint = False
2379-
use_outer = True
2380-
23812380
if use_joint:
23822381
new_inferred_args = target_solution[0]
23832382
# inferred_args = [

test-data/unit/check-generics.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2929,8 +2929,8 @@ def mix(fs: List[Callable[[S], T]]) -> Callable[[S], List[T]]:
29292929
def id(__x: U) -> U:
29302930
...
29312931
fs = [id, id, id]
2932-
reveal_type(mix(fs)) # N: Revealed type is "def [S] (S`23) -> builtins.list[S`23]"
2933-
reveal_type(mix([id, id, id])) # N: Revealed type is "def [S] (S`25) -> builtins.list[S`25]"
2932+
reveal_type(mix(fs)) # N: Revealed type is "def [S] (S`35) -> builtins.list[S`35]"
2933+
reveal_type(mix([id, id, id])) # N: Revealed type is "def [S] (S`37) -> builtins.list[S`37]"
29342934
[builtins fixtures/list.pyi]
29352935

29362936
[case testInferenceAgainstGenericCurry]

test-data/unit/check-recursive-types.test

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,21 +285,33 @@ if isinstance(b[0], Sequence):
285285
[case testRecursiveAliasWithRecursiveInstance]
286286
from typing import Sequence, Union, TypeVar
287287

288-
class A: ...
289288
T = TypeVar("T")
290289
Nested = Sequence[Union[T, Nested[T]]]
290+
def join(a: T, b: T) -> T: ...
291+
292+
class A: ...
291293
class B(Sequence[B]): ...
292294

293295
a: Nested[A]
294296
aa: Nested[A]
295297
b: B
298+
296299
a = b # OK
300+
reveal_type(a) # N: Revealed type is "__main__.B"
301+
297302
a = [[b]] # OK
303+
reveal_type(a) # N: Revealed type is "builtins.list[builtins.list[__main__.B]]"
304+
298305
b = aa # E: Incompatible types in assignment (expression has type "Nested[A]", variable has type "B")
306+
reveal_type(b) # N: Revealed type is "__main__.B"
307+
308+
reveal_type(join(a, b)) # N: Revealed type is "typing.Sequence[typing.Sequence[__main__.B]]"
309+
reveal_type(join(b, a)) # N: Revealed type is "typing.Sequence[typing.Sequence[__main__.B]]"
310+
311+
def test(a: Nested[A], b: B) -> None:
312+
reveal_type(join(a, b)) # N: Revealed type is "typing.Sequence[Union[__main__.A, typing.Sequence[Union[__main__.A, ...]]]]"
313+
reveal_type(join(b, a)) # N: Revealed type is "typing.Sequence[Union[__main__.A, typing.Sequence[Union[__main__.A, ...]]]]"
299314

300-
def join(a: T, b: T) -> T: ...
301-
reveal_type(join(a, b)) # N: Revealed type is "typing.Sequence[Union[__main__.A, typing.Sequence[Union[__main__.A, ...]]]]"
302-
reveal_type(join(b, a)) # N: Revealed type is "typing.Sequence[Union[__main__.A, typing.Sequence[Union[__main__.A, ...]]]]"
303315
[builtins fixtures/isinstancelist.pyi]
304316

305317
[case testRecursiveAliasWithRecursiveInstanceInference]

0 commit comments

Comments
 (0)