Skip to content

False positive in @classmethod check for subclasses #539

@arnimarj

Description

@arnimarj

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

Typeguard version

4.4.4

Python version

3.13.9

What happened?

It seems that the protocol check for @classmethods when a base class implements the protocol, and a base class inherits it, the check fails.

The method is wrongly classified as an instance method.

How can we reproduce the bug?

This shows both a true negative (checking the base class), and a false positive (checking the subclass):

from typing import Protocol, Self

import typeguard


class HasClassFunc(Protocol):
    @classmethod
    def class_func(cls) -> Self: ...


class BaseClass:
    @classmethod
    def class_func(cls) -> Self:
        return cls()


class SubClass(BaseClass):
    pass


@typeguard.typechecked
def my_func(b: type[HasClassFunc]) -> HasClassFunc:
    return b.class_func()


# succeeds
my_func(BaseClass)

# fails
my_func(SubClass)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions