Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of overloads with ParamSpec #12953

Merged
merged 2 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions mypy/meet.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
TupleType, TypedDictType, ErasedType, UnionType, PartialType, DeletedType,
UninhabitedType, TypeType, TypeOfAny, Overloaded, FunctionLike, LiteralType,
ProperType, get_proper_type, get_proper_types, TypeAliasType, TypeGuardedType,
ParamSpecType, Parameters, UnpackType, TypeVarTupleType,
ParamSpecType, Parameters, UnpackType, TypeVarTupleType, TypeVarLikeType
)
from mypy.subtypes import is_equivalent, is_subtype, is_callable_compatible, is_proper_subtype
from mypy.erasetype import erase_type
Expand Down Expand Up @@ -134,6 +134,8 @@ def get_possible_variants(typ: Type) -> List[Type]:
return typ.values
else:
return [typ.upper_bound]
elif isinstance(typ, ParamSpecType):
AlexWaygood marked this conversation as resolved.
Show resolved Hide resolved
return [typ.upper_bound]
elif isinstance(typ, UnionType):
return list(typ.items)
elif isinstance(typ, Overloaded):
Expand Down Expand Up @@ -255,14 +257,14 @@ def _is_overlapping_types(left: Type, right: Type) -> bool:

def is_none_typevar_overlap(t1: Type, t2: Type) -> bool:
AlexWaygood marked this conversation as resolved.
Show resolved Hide resolved
t1, t2 = get_proper_types((t1, t2))
return isinstance(t1, NoneType) and isinstance(t2, TypeVarType)
return isinstance(t1, NoneType) and isinstance(t2, TypeVarLikeType)

if prohibit_none_typevar_overlap:
if is_none_typevar_overlap(left, right) or is_none_typevar_overlap(right, left):
return False

if (len(left_possible) > 1 or len(right_possible) > 1
or isinstance(left, TypeVarType) or isinstance(right, TypeVarType)):
or isinstance(left, TypeVarLikeType) or isinstance(right, TypeVarLikeType)):
for l in left_possible:
for r in right_possible:
if _is_overlapping_types(l, r):
Expand Down
30 changes: 30 additions & 0 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -6506,3 +6506,33 @@ if True:
@overload
def f3(g: D) -> D: ...
def f3(g): ... # E: Name "f3" already defined on line 32

[case testOverloadingWithParamSpec]
from typing import TypeVar, Callable, Any, overload
from typing_extensions import ParamSpec, Concatenate

P = ParamSpec("P")
R = TypeVar("R")

@overload
def func(x: Callable[Concatenate[Any, P], R]) -> Callable[P, R]: ... # E: Overloaded function signatures 1 and 2 overlap with incompatible return types
@overload
def func(x: Callable[P, R]) -> Callable[Concatenate[str, P], R]: ...
def func(x: Callable[..., R]) -> Callable[..., R]: ...

def foo(arg1: str, arg2: int) -> bytes: ...
reveal_type(func(foo)) # N: Revealed type is "def (arg2: builtins.int) -> builtins.bytes"

def bar() -> int: ...
reveal_type(func(bar)) # N: Revealed type is "def (builtins.str) -> builtins.int"

baz: Callable[[str, str], str] = lambda x, y: 'baz'
reveal_type(func(baz)) # N: Revealed type is "def (builtins.str) -> builtins.str"

eggs = lambda: 'eggs'
reveal_type(func(eggs)) # N: Revealed type is "def (builtins.str) -> builtins.str"

spam: Callable[..., str] = lambda x, y: 'baz'
reveal_type(func(spam)) # N: Revealed type is "def (*Any, **Any) -> Any"

[builtins fixtures/paramspec.pyi]