Skip to content

Better support for property-subclasses / read-only descriptors #19178

Open
@randolf-scholz

Description

@randolf-scholz

It seems mypy applies a bunch of special casing to @property that does not carry over when using a subclass of property or when defining a read-only descriptor.

https://mypy-play.net/?mypy=latest&python=3.12&gist=9a1c98026783a48c82f6a92e00acc941

from typing import Callable, ClassVar, Self, overload

class FooAbstract:
    @property
    def support(self) -> None: ...
    
class Foo(FooAbstract):
    support: ClassVar[None] = None  # ✅
    
# custom Property
class custom_property(property): ...

class BarAbstract:
    @custom_property
    def support(self) -> None: ...
    
class Bar(BarAbstract):
    support: ClassVar[None] = None  # ❌
    
# custom descriptor
class custom_getter[T, R]:
    def __init__(self, fn: Callable[[T], R]) -> None:
        self.fn = fn
        
    @overload  # type: ignore[no-overload-impl]
    def __get__(self, instance: None, owner: type, /) -> Self: ...
    @overload
    def __get__(self, instance: T, owner: type | None = ..., /) -> R: ...

class BazAbstract:
    @custom_getter
    def support(self) -> None: ...
    
class Baz(BazAbstract):
    support: ClassVar[None] = None  # ❌

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions