python / python/mypy

Better support for property-subclasses / read-only descriptors

Open
#19,178 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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  # ❌

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test is named; start with the supplied mypy-play reproduction and trace how property overrides are analyzed. Done means equivalent property-subclass and read-only-descriptor cases receive the expected treatment when overridden by a ClassVar.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.