From 9179c5530369e8d7f5f235eb7f424d44353ea456 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Tue, 23 Aug 2022 01:48:10 +0100 Subject: [PATCH] Fix overloaded static methods on instances --- mypy/checkmember.py | 4 +++- test-data/unit/check-overloading.test | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/mypy/checkmember.py b/mypy/checkmember.py index 6777c4354a04..3be961ee9fdc 100644 --- a/mypy/checkmember.py +++ b/mypy/checkmember.py @@ -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 @@ -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) diff --git a/test-data/unit/check-overloading.test b/test-data/unit/check-overloading.test index c20e13e8e3e2..62e3d08b9aff 100644 --- a/test-data/unit/check-overloading.test +++ b/test-data/unit/check-overloading.test @@ -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]