gh-145413: Fix nested attribute suggestions executing property code by Hiumee · Pull Request #145414 · python/cpython · GitHub
Skip to content

gh-145413: Fix nested attribute suggestions executing property code - #145414

Closed
Hiumee wants to merge 3 commits into
python:mainfrom
Hiumee:fix-issue-145413
Closed

Hiumee wants to merge 3 commits into
python:mainfrom
Hiumee:fix-issue-145413

Conversation

@Hiumee

@Hiumee Hiumee commented Mar 2, 2026

Copy link
Copy Markdown

Updated the code of _check_for_nested_attribute to check properties without triggering their code. This approach will still suggest them

Changed a test case to look for side effects and changed some contradictory comments

Updated the news section code to include imports so the code can be tested out of the box

Example

from dataclasses import dataclass
from math import pi

@dataclass
class Circle:
   radius: float
   @property
   def area(self) -> float:
      print("Property 'area' accessed")
      return pi * self.radius**2

class Container:
   def __init__(self, inner: Circle) -> None:
      self.inner = inner

circle = Circle(radius=4.0)
container = Container(circle)
print(container.area)

Output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    print(container.area)
          ^^^^^^^^^^^^^^
AttributeError: 'Container' object has no attribute 'area'. Did you mean '.inner.area' instead of '.area'?

It doesn't reach the print statement at all


📚 Documentation preview 📚: https://cpython-previews--145414.org.readthedocs.build/

@Hiumee
Hiumee requested a review from AA-Turner as a code owner March 2, 2026 11:41
@python-cla-bot

python-cla-bot Bot commented Mar 2, 2026

Copy link
Copy Markdown

@johnslavik
johnslavik requested review from johnslavik March 2, 2026 14:15
@Hiumee

Hiumee commented Mar 26, 2026

Copy link
Copy Markdown
Author

@Hiumee Hiumee closed this Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant