Skip to content

Commit 55b3bac

Browse files
replace one hack with another
1 parent 76f3571 commit 55b3bac

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

mypy/checkexpr.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,6 +2108,19 @@ def infer_function_type_arguments(
21082108
formal_to_actual,
21092109
context=self.argument_infer_context(),
21102110
)
2111+
# HACK: convert "Literal?" constraints to their non-literal versions.
2112+
inner_constraints = [
2113+
Constraint(
2114+
c.original_type_var,
2115+
c.op,
2116+
(
2117+
c.target.copy_modified(last_known_value=None)
2118+
if isinstance(c.target, Instance)
2119+
else c.target
2120+
),
2121+
)
2122+
for c in inner_constraints
2123+
]
21112124

21122125
# compute the outer solution
21132126
outer_constraints = self.infer_constraints_from_context(callee_type, context)
@@ -2131,9 +2144,10 @@ def infer_function_type_arguments(
21312144
# NOTE: The order of constraints is important here!
21322145
# solve(outer + inner) and solve(inner + outer) may yield different results.
21332146
# we need to use outer first.
2147+
joint_constraints = outer_constraints + inner_constraints
21342148
joint_solution = solve_constraints(
21352149
callee_type.variables,
2136-
outer_constraints + inner_constraints,
2150+
joint_constraints,
21372151
strict=self.chk.in_checked_function(),
21382152
allow_polymorphic=False,
21392153
)
@@ -2152,10 +2166,6 @@ def infer_function_type_arguments(
21522166
None in joint_solution[0]
21532167
# If the outer solution is more concrete than the joint solution, prefer the outer solution.
21542168
or is_subtype(outer_ret_type, joint_ret_type)
2155-
or ( # HACK to fix testLiteralAndGenericWithUnion
2156-
isinstance(outer_ret_type, UnionType)
2157-
and any(is_subtype(val, joint_ret_type) for val in outer_ret_type.items)
2158-
)
21592169
):
21602170
use_joint = False
21612171
else:

mypy/constraints.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,13 @@ class Constraint:
7777
"""
7878

7979
type_var: TypeVarId
80+
original_type_var: TypeVarLikeType
8081
op = 0 # SUBTYPE_OF or SUPERTYPE_OF
8182
target: Type
8283

8384
def __init__(self, type_var: TypeVarLikeType, op: int, target: Type) -> None:
8485
self.type_var = type_var.id
86+
self.original_type_var = type_var
8587
self.op = op
8688
# TODO: should we add "assert not isinstance(target, UnpackType)"?
8789
# UnpackType is a synthetic type, and is never valid as a constraint target.

test-data/unit/check-generics.test

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2998,7 +2998,7 @@ def lift(f: F[T]) -> F[Optional[T]]: ...
29982998
def g(x: T) -> T:
29992999
return x
30003000

3001-
reveal_type(lift(g)) # N: Revealed type is "def [T] (Union[T`1, None]) -> Union[T`1, None]"
3001+
reveal_type(lift(g)) # N: Revealed type is "__main__.F[Union[T`-1, None]]"
30023002
[builtins fixtures/list.pyi]
30033003

30043004
[case testInferenceAgainstGenericSplitOrder]
@@ -3198,11 +3198,11 @@ def dec(f: Callable[P, Callable[[T], S]]) -> Callable[Concatenate[T, P], S]: ...
31983198
def id() -> Callable[[U], U]: ...
31993199
def either(x: U) -> Callable[[U], U]: ...
32003200
def pair(x: U) -> Callable[[V], Tuple[V, U]]: ...
3201-
reveal_type(dec(id)) # N: Revealed type is "def [T] (T`3) -> T`3"
3202-
reveal_type(dec(either)) # N: Revealed type is "def [T] (T`6, x: T`6) -> T`6"
3203-
reveal_type(dec(pair)) # N: Revealed type is "def [T, U] (T`9, x: U`-1) -> tuple[T`9, U`-1]"
3201+
reveal_type(dec(id)) # N: Revealed type is "def (U`-1) -> U`-1"
3202+
reveal_type(dec(either)) # N: Revealed type is "def [T] (T`7, x: T`7) -> T`7"
3203+
reveal_type(dec(pair)) # N: Revealed type is "def [T, U] (T`10, x: U`-1) -> tuple[T`10, U`-1]"
32043204
# This is counter-intuitive but looks correct, dec matches itself only if P can be empty
3205-
reveal_type(dec(dec)) # N: Revealed type is "def [T, S] (T`13, f: def () -> def (T`13) -> S`14) -> S`14"
3205+
reveal_type(dec(dec)) # N: Revealed type is "def [T, S] (T`14, f: def () -> def (T`14) -> S`15) -> S`15"
32063206
[builtins fixtures/list.pyi]
32073207

32083208
[case testInferenceAgainstGenericParamSpecVsParamSpec]

test-data/unit/check-varargs.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -629,9 +629,9 @@ from typing import TypeVar
629629
T = TypeVar('T')
630630

631631
def f(*args: T) -> T: ...
632-
reveal_type(f(*(1, None))) # N: Revealed type is "Union[Literal[1]?, None]"
633-
reveal_type(f(1, *(None, 1))) # N: Revealed type is "Union[Literal[1]?, None]"
634-
reveal_type(f(1, *(1, None))) # N: Revealed type is "Union[Literal[1]?, None]"
632+
reveal_type(f(*(1, None))) # N: Revealed type is "Union[builtins.int, None]"
633+
reveal_type(f(1, *(None, 1))) # N: Revealed type is "Union[builtins.int, None]"
634+
reveal_type(f(1, *(1, None))) # N: Revealed type is "Union[builtins.int, None]"
635635
[builtins fixtures/tuple.pyi]
636636

637637

0 commit comments

Comments
 (0)