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

Fix overloading with a typevar missing #15320

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 25 additions & 5 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# Circular import; done in the function instead.
# import mypy.solve
from mypy.nodes import (
ARG_POS,
ARG_STAR,
ARG_STAR2,
CONTRAVARIANT,
Expand Down Expand Up @@ -1692,11 +1693,30 @@ def unify_generic_callable(
return_constraint_direction = mypy.constraints.SUBTYPE_OF

constraints: list[mypy.constraints.Constraint] = []
for arg_type, target_arg_type in zip(type.arg_types, target.arg_types):
c = mypy.constraints.infer_constraints(
arg_type, target_arg_type, mypy.constraints.SUPERTYPE_OF
)
constraints.extend(c)
# check by names
argument_names_map = {}

for i in range(len(target.arg_types)):
if target.arg_names[i] and target.arg_kinds[i] != ARG_POS:
argument_names_map[target.arg_names[i]] = target.arg_types[i]

for i in range(len(type.arg_types)):
if type.arg_names[i] and type.arg_names[i] in argument_names_map:
c = mypy.constraints.infer_constraints(
argument_names_map[type.arg_names[i]],
type.arg_types[i],
mypy.constraints.SUPERTYPE_OF,
)
constraints.extend(c)

# check pos-only arguments
for arg, target_arg in zip(type.formal_arguments(), target.formal_arguments()):
if arg.pos is not None and target_arg.pos is not None:
c = mypy.constraints.infer_constraints(
arg.typ, target_arg.typ, mypy.constraints.SUPERTYPE_OF
)
constraints.extend(c)

if not ignore_return:
c = mypy.constraints.infer_constraints(
type.ret_type, target.ret_type, return_constraint_direction
Expand Down
45 changes: 45 additions & 0 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -6545,3 +6545,48 @@ class Snafu(object):
reveal_type(Snafu().snafu('123')) # N: Revealed type is "builtins.str"
reveal_type(Snafu.snafu('123')) # N: Revealed type is "builtins.str"
[builtins fixtures/staticmethod.pyi]

[case testOverloadedFunctionWithTypevarMissing]
import typing

class A: ...

T = typing.TypeVar("T", bound=A)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should investigate why current mypy is a-ok without the bound=A here, but errors when it is. That's strange and probably a sign of incorrect special casing: something I hate!


@typing.overload
def f(a: T) -> T: ...

@typing.overload
def f(*, copy: bool = False) -> None: ...

def f(a: T = ..., *, copy: bool = False) -> T:
...

reveal_type(f) # N: Revealed type is "Overload(def [T <: __main__.A] (a: T`-1) -> T`-1, def (*, copy: builtins.bool =))"

[case testOverloadingWithTypeVarWhereTargetIsPosOnly]
from typing import overload, TypeVar, Union

T1 = TypeVar("T1")
T2 = TypeVar("T2")

@overload
def f(
*,
x: T1,
) -> None:
...

@overload
def f( # type: ignore
*,
x: T2,
) -> None:
...

def f(
x: Union[T1, T2]
) -> None:
...

reveal_type(f) # N: Revealed type is "Overload(def [T1] (*, x: T1`-1), def [T2] (*, x: T2`-1))"