From 3d7e030f09e8ff9e0bfc974b360b4e93aa55ad33 Mon Sep 17 00:00:00 2001 From: Eric Mark Martin Date: Thu, 6 Feb 2025 21:49:50 -0500 Subject: [PATCH 1/2] align is_classmethod with is_staticmethod Make classmethod detection more in line with staticmethod detection. This correctly handles a few additional cases: for example, callables created via PyCMethod_New and friends will no longer be classified as classmethods. --- mypy/stubgenc.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mypy/stubgenc.py b/mypy/stubgenc.py index b5bb4f8f727b..192a044e115e 100755 --- a/mypy/stubgenc.py +++ b/mypy/stubgenc.py @@ -553,12 +553,12 @@ def is_method(self, class_info: ClassInfo, name: str, obj: object) -> bool: def is_classmethod(self, class_info: ClassInfo, name: str, obj: object) -> bool: if self.is_c_module: - return inspect.isbuiltin(obj) or type(obj).__name__ in ( - "classmethod", - "classmethod_descriptor", - ) + raw_lookup: Mapping[str, Any] = getattr(class_info.cls, "__dict__") # noqa: B009 + raw_value = raw_lookup.get(name, obj) + classmethod_descriptor = type(int.__dict__["from_bytes"]) + return isinstance(raw_value, classmethod) or isinstance(raw_value, classmethod_descriptor) else: - return inspect.ismethod(obj) + return isinstance(inspect.getattr_static(class_info.cls, name), classmethod) def is_staticmethod(self, class_info: ClassInfo | None, name: str, obj: object) -> bool: if class_info is None: From 472917d65e841c85dedf336a8801bc8299a98c8d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 11 Feb 2025 04:51:49 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypy/stubgenc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/stubgenc.py b/mypy/stubgenc.py index 192a044e115e..02feae344d52 100755 --- a/mypy/stubgenc.py +++ b/mypy/stubgenc.py @@ -556,7 +556,7 @@ def is_classmethod(self, class_info: ClassInfo, name: str, obj: object) -> bool: raw_lookup: Mapping[str, Any] = getattr(class_info.cls, "__dict__") # noqa: B009 raw_value = raw_lookup.get(name, obj) classmethod_descriptor = type(int.__dict__["from_bytes"]) - return isinstance(raw_value, classmethod) or isinstance(raw_value, classmethod_descriptor) + return isinstance(raw_value, (classmethod, classmethod_descriptor)) else: return isinstance(inspect.getattr_static(class_info.cls, name), classmethod)