Skip to content

Mypy is expecting type[Never] in context when it shouldn't #18968

@aidangallagher4

Description

@aidangallagher4

Bug Report

I have written (well, copied, from here) a class classproperty to handle implementing class properties. It works perfectly fine, however when I try and type it as follows (which I think is valid), mypy raises the below error

class classproperty[T, C]:
    def __init__(self, func: Callable[[type[C]], T]):
        self.func = func

    def __get__(self, _obj: C, owner: type[C]) -> T:
        return self.func(owner)

class Test:
    @classproperty
    def my_property(cls):
        return 1
error: Argument 1 to "classproperty" has incompatible type "Callable[[Test], Any]"; expected "Callable[[type[Never]], Any]"  [arg-type]

I don't think this is correct, based on my understanding of descriptors and __get__. I asked a question on stackoverflow about this and it was suggested that this is caused by incorrect assumptions about how __get__ is called

To Reproduce

Gist link here

Expected Behavior

Mypy should pass

Actual Behavior

error: Argument 1 to "classproperty" has incompatible type "Callable[[Test], Any]"; expected "Callable[[type[Never]], Any]"  [arg-type]

Your Environment

  • Python 3.13.1
  • mypy==1.15.0

No further config

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions