Skip to content

Commit 7f8cf7e

Browse files
committed
Fix special form in nested
1 parent dc9fd24 commit 7f8cf7e

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

mypy/semanal.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8263,13 +8263,19 @@ def remove_imported_names_from_symtable(names: SymbolTable, module: str) -> None
82638263

82648264
def make_any_non_explicit(t: Type) -> Type:
82658265
"""Replace all Any types within in with Any that has attribute 'explicit' set to False"""
8266+
# An alias whose target is exactly Any should still behave like a real Any when used.
8267+
# Explicit Any nested inside a larger alias target is kept as the historical
8268+
# non-real Any flavor to avoid making large generic aliases overload-ambiguous.
8269+
proper_t = get_proper_type(t)
8270+
if isinstance(proper_t, AnyType) and proper_t.type_of_any == TypeOfAny.explicit:
8271+
return proper_t.copy_modified(TypeOfAny.from_alias_target)
82668272
return t.accept(MakeAnyNonExplicit())
82678273

82688274

82698275
class MakeAnyNonExplicit(TrivialSyntheticTypeTranslator):
82708276
def visit_any(self, t: AnyType) -> Type:
82718277
if t.type_of_any == TypeOfAny.explicit:
8272-
return t.copy_modified(TypeOfAny.from_alias_target)
8278+
return t.copy_modified(TypeOfAny.special_form)
82738279
return t
82748280

82758281
def visit_type_alias_type(self, t: TypeAliasType) -> Type:

test-data/unit/check-overloading.test

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6997,3 +6997,35 @@ def f(x: Incomplete) -> Incomplete: # no error
69976997

69986998
a: Incomplete = 1 # no error
69996999
[builtins fixtures/tuple.pyi]
7000+
7001+
[case testOverloadNestedAliasAnyDoesNotTriggerAmbiguity]
7002+
# Explicit Any nested inside a larger type alias should not trigger the
7003+
# overload-ambiguity fallback. This matches numpy/pandas-style aliases such as
7004+
# `ndarray[tuple[Any, ...], dtype[T]]`.
7005+
from typing import Any, Generic, TypeVar, overload
7006+
from typing_extensions import Never, TypeAlias
7007+
7008+
S = TypeVar("S")
7009+
Sh = TypeVar("Sh")
7010+
7011+
class Array(Generic[Sh, S]): pass
7012+
7013+
Shape: TypeAlias = tuple[Any, ...]
7014+
BoolArray: TypeAlias = Array[Shape, bool]
7015+
7016+
class Series(Generic[S]):
7017+
@overload
7018+
def __add__(self: Series[Never], other: object) -> Series[Any]: ... # type: ignore[overload-overlap]
7019+
@overload
7020+
def __add__(self: Series[S], other: Array[Any, S]) -> Series[S]: ...
7021+
@overload
7022+
def __add__(self: Series[S], other: Array[Any, bool]) -> Series[S]: ...
7023+
@overload
7024+
def __add__(self: Series[bool], other: Array[Any, bool]) -> Series[bool]: ...
7025+
@overload
7026+
def __add__(self: Series[str], other: Array[Any, bool]) -> Never: ...
7027+
def __add__(self, other): ...
7028+
7029+
def f(x: Series[Any], y: BoolArray) -> None:
7030+
reveal_type(x + y) # N: Revealed type is "__main__.Series[Any]"
7031+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)