Skip to content

Commit

Permalink
Fix overloaded static methods on instances (#13482)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilevkivskyi committed Aug 23, 2022
1 parent be6adae commit e981431
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def analyze_instance_member_access(
mx.msg.cant_assign_to_method(mx.context)
signature = function_type(method, mx.named_type("builtins.function"))
signature = freshen_function_type_vars(signature)
if name == "__new__":
if name == "__new__" or method.is_static:
# __new__ is special and behaves like a static method -- don't strip
# the first argument.
pass
Expand All @@ -315,6 +315,8 @@ def analyze_instance_member_access(
signature, dispatched_type, method.is_class, mx.context, name, mx.msg
)
signature = bind_self(signature, mx.self_type, is_classmethod=method.is_class)
# TODO: should we skip these steps for static methods as well?
# Since generic static methods should not be allowed.
typ = map_instance_to_supertype(typ, method.info)
member_type = expand_type_by_instance(signature, typ)
freeze_type_vars(member_type)
Expand Down
17 changes: 17 additions & 0 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -6527,3 +6527,20 @@ class Test:
def foo(self, arg: Sequence[A]) -> None: ...
def foo(self, arg: Union[Sequence[E], Sequence[A]]) -> None:
...

[case testOverloadedStaticMethodOnInstance]
from typing import overload

class Snafu(object):
@overload
@staticmethod
def snafu(value: bytes) -> bytes: ...
@overload
@staticmethod
def snafu(value: str) -> str: ...
@staticmethod
def snafu(value):
...
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]

0 comments on commit e981431

Please sign in to comment.