python / python/mypy

Mypy doesn't object to `cached_property` methods that don't exist

Open
#18,327 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

Mypy includes functools.cached_property on it's list of property-list types, but considers it to be too identical to property. It allows the use of cached_property.setter and cached_property.deleter without raising an error, despite the fact that neither of these are methods that exist on cached_property.

I discovered this while looking at permutations of property-like behavior for https://github.com/python/typeshed/pull/13276.

Actually, I realized just now that it can be anything from any of the property types. See the second playground link. [Edit: this part fixed by #19313]

To Reproduce

Playground link showing : https://mypy-play.net/?mypy=latest&python=3.12&gist=f5336c7a8df8f028a565bab576b74062

This one shows that mypy doesn't object to non-existent methods of property used as a decorator:

https://mypy-play.net/?mypy=latest&python=3.12&gist=d91764177a604ba5640a1fe741718f52 [Edit: this part fixed by #19313]

Expected Behavior

I expect mypy to generate errors for methods that don't exist in the stubs, even with the special-cased behavior.

Actual Behavior

No errors are generated.

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

Start by reproducing the behavior in the linked mypy playground with Python 3.12. Then inspect mypy's special-cased handling for property-like types, focusing on why cached_property accepts setter and deleter. Done means mypy reports errors for methods that are absent from cached_property while preserving valid cached_property behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.