pablogsal · GitHub

Bug report

Bug description:

I remember we used to have this when all the typo-detection machinery was in the C code but somehow that has not survived the transition to the new traceback machinery (or perhaps I am misremembering but I recall adding this originally when we added the first round). Either way this PR ensures that we have a nice error message when the typo is across attribute access:

from dataclasses import dataclass
from math import pi
from types import SimpleNamespace
from typing import Any, Dict
@dataclass
class Circle:
    radius: float
    @property
    def area(self) -> float:
        return pi * self.radius**2
    @property
    def perimeter(self) -> float:
        return 2 * pi * self.radius
@dataclass
class Square:
    side: float
    @property
    def area(self) -> float:
        return self.side**2
    @property
    def perimeter(self) -> float:
        return 4 * self.side
class Container:
    def __init__(self, inner: Any) -> None:
        self.inner = inner
if __name__ == "__main__":
    square = Square(side=4)
    container = Container(square)
    print(container.area)

Should print:

Traceback (most recent call last):
  File "/home/pablogsal/github/python/main/lel.py", line 42, in <module>
    print(container.area)
          ^^^^^^^^^^^^^^
AttributeError: 'Container' object has no attribute 'area'. Did you mean: 'inner.area'?

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

Read the original on github.com ↗