Skip to content

Fix overloaded static methods on instances #13482

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 1 commit into from
Aug 23, 2022
Merged
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
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]