Skip to content

Make any callable compatible with (*args: Any, **kwargs: Any) #11203

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

Merged
merged 8 commits into from
Aug 13, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 9 additions & 1 deletion mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# import mypy.solve
from mypy.nodes import (
FuncBase, Var, Decorator, OverloadedFuncDef, TypeInfo, CONTRAVARIANT, COVARIANT,

ARG_STAR, ARG_STAR2,
)
from mypy.maptype import map_instance_to_supertype
from mypy.expandtype import expand_type_by_instance
Expand Down Expand Up @@ -893,6 +893,14 @@ def g(x: int) -> int: ...
right_star = right.var_arg()
right_star2 = right.kw_arg()

# Treat "def _(*a: Any, **kw: Any) -> X" similarly to "Callable[..., X]"
if (
right.arg_kinds == [ARG_STAR, ARG_STAR2]
and right_star and isinstance(get_proper_type(right_star.typ), AnyType)
and right_star2 and isinstance(get_proper_type(right_star2.typ), AnyType)
):
return True

# Match up corresponding arguments and check them for compatibility. In
# every pair (argL, argR) of corresponding arguments from L and R, argL must
# be "more general" than argR if L is to be a subtype of R.
Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -6891,3 +6891,14 @@ class B(A): # E: Final class __main__.B has abstract attributes "foo"
[case testUndefinedBaseclassInNestedClass]
class C:
class C1(XX): pass # E: Name "XX" is not defined


[case testArgsKwargsInheritance]
from typing import Any

class A(object):
def f(self, *args: Any, **kwargs: Any) -> int: ...

class B(A):
def f(self, x: int) -> int: ...
Copy link
Member

Choose a reason for hiding this comment

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

I understand that this is very handy in some cases, #5876 has a handful of them. But, it is important to remember that this is not type-safe.

from typing import Any

class A(object):
    def f(self, *args: Any, **kwargs: Any) -> int: ...

class B(A):
    def f(self, x: int) -> int: ...

def test(arg: A):
    arg.f(x=0, kw1=1, kw2=2)

test(A())  # ok
test(B())  # mypy is ok with that, but raises TypeError in real-life

It breaks Liskov's law.

Maybe we can somehow limit the scope of this new rule? For example:

  • It is fine for decorator example from Make any callable compatible with (*args: Any, **kwargs: Any)? #5876
  • It is fine for Protocol check (when protocol defines less-or-equally broad signature): Procol.method(self, arg: int) -> is ok with Impl.method(self, *args, **kwargs)
  • It is fine for callbacks
  • Maybe something else
  • It is not fine for subclassing

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I think the ask is explicitly for something that is not type safe. The check when "protocol defines less-or-equally broad signature" should already work. The decorator example from #5876 is a little interesting; this PR doesn't actually change inference there.

#5876 is probably a top five most popular mypy issue, so ideally something would be done. Maybe the answer is another flag.

[builtins fixtures/dict.pyi]
8 changes: 4 additions & 4 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ if int():
ee_var = everywhere

if int():
ee_var = specific_1 # The difference between Callable[..., blah] and one with a *args: Any, **kwargs: Any is that the ... goes loosely both ways.
ee_var = specific_1
if int():
ee_def = specific_1 # E: Incompatible types in assignment (expression has type "Callable[[int, str], None]", variable has type "Callable[[VarArg(Any), KwArg(Any)], None]")
ee_def = specific_1

[builtins fixtures/dict.pyi]

Expand Down Expand Up @@ -1827,7 +1827,7 @@ def f2(*args, **kwargs) -> int: pass
d(f1)
e(f2)
d(f2)
e(f1) # E: Argument 1 to "e" has incompatible type "Callable[[VarArg(Any)], int]"; expected "Callable[[VarArg(Any), KwArg(Any)], int]"
e(f1)

[builtins fixtures/dict.pyi]

Expand Down Expand Up @@ -2224,7 +2224,7 @@ from typing import Callable
class A:
def f(self) -> None:
# In particular, test that the error message contains "g" of "A".
self.g() # E: Too few arguments for "g" of "A"
self.g() # E: Too few arguments for "g" of "A"
self.g(1)
@dec
def g(self, x: str) -> None: pass
Expand Down