Skip to content

Commit 1da86bb

Browse files
committed
Retain None as constraints bottom if no bottoms were provided
1 parent a0665e1 commit 1da86bb

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

mypy/solve.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,10 @@ def solve_one(lowers: Iterable[Type], uppers: Iterable[Type]) -> Type | None:
281281
# Process each bound separately, and calculate the lower and upper
282282
# bounds based on constraints. Note that we assume that the constraint
283283
# targets do not have constraint references.
284-
if type_state.infer_unions:
284+
if type_state.infer_unions and lowers:
285285
# This deviates from the general mypy semantics because
286286
# recursive types are union-heavy in 95% of cases.
287+
# Retain `None` when no bottoms were provided to avoid bogus `Never` inference.
287288
bottom = UnionType.make_union(list(lowers))
288289
else:
289290
# The order of lowers is non-deterministic.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ reveal_type(flatten([1, [2, [3]]])) # N: Revealed type is "builtins.list[builti
5454

5555
class Bad: ...
5656
x: Nested[int] = [1, [2, [3]]]
57-
x = [1, [Bad()]] # E: List item 1 has incompatible type "list[Bad]"; expected "Union[int, Nested[int]]"
57+
x = [1, [Bad()]] # E: List item 0 has incompatible type "Bad"; expected "Union[int, Nested[int]]"
5858
[builtins fixtures/isinstancelist.pyi]
5959

6060
[case testRecursiveAliasGenericInferenceNested]
@@ -605,7 +605,7 @@ class NT(NamedTuple, Generic[T]):
605605
class A: ...
606606
class B(A): ...
607607

608-
nti: NT[int] = NT(key=0, value=NT(key=1, value=A())) # E: Argument "value" to "NT" has incompatible type "NT[A]"; expected "Union[int, NT[int]]"
608+
nti: NT[int] = NT(key=0, value=NT(key=1, value=A())) # E: Argument "value" to "NT" has incompatible type "A"; expected "Union[int, NT[int]]"
609609
reveal_type(nti) # N: Revealed type is "tuple[builtins.int, Union[builtins.int, ...], fallback=__main__.NT[builtins.int]]"
610610

611611
nta: NT[A]

0 commit comments

Comments
 (0)