Skip to content

Commit e981431

Browse files
authored
Fix overloaded static methods on instances (#13482)
1 parent be6adae commit e981431

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

mypy/checkmember.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ def analyze_instance_member_access(
302302
mx.msg.cant_assign_to_method(mx.context)
303303
signature = function_type(method, mx.named_type("builtins.function"))
304304
signature = freshen_function_type_vars(signature)
305-
if name == "__new__":
305+
if name == "__new__" or method.is_static:
306306
# __new__ is special and behaves like a static method -- don't strip
307307
# the first argument.
308308
pass
@@ -315,6 +315,8 @@ def analyze_instance_member_access(
315315
signature, dispatched_type, method.is_class, mx.context, name, mx.msg
316316
)
317317
signature = bind_self(signature, mx.self_type, is_classmethod=method.is_class)
318+
# TODO: should we skip these steps for static methods as well?
319+
# Since generic static methods should not be allowed.
318320
typ = map_instance_to_supertype(typ, method.info)
319321
member_type = expand_type_by_instance(signature, typ)
320322
freeze_type_vars(member_type)

test-data/unit/check-overloading.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6527,3 +6527,20 @@ class Test:
65276527
def foo(self, arg: Sequence[A]) -> None: ...
65286528
def foo(self, arg: Union[Sequence[E], Sequence[A]]) -> None:
65296529
...
6530+
6531+
[case testOverloadedStaticMethodOnInstance]
6532+
from typing import overload
6533+
6534+
class Snafu(object):
6535+
@overload
6536+
@staticmethod
6537+
def snafu(value: bytes) -> bytes: ...
6538+
@overload
6539+
@staticmethod
6540+
def snafu(value: str) -> str: ...
6541+
@staticmethod
6542+
def snafu(value):
6543+
...
6544+
reveal_type(Snafu().snafu('123')) # N: Revealed type is "builtins.str"
6545+
reveal_type(Snafu.snafu('123')) # N: Revealed type is "builtins.str"
6546+
[builtins fixtures/staticmethod.pyi]

0 commit comments

Comments
 (0)