-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
Closed
Labels
stdlibPython modules in the Lib dirPython modules in the Lib dirtopic-replRelated to the interactive shellRelated to the interactive shelltype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or errortype-featureA feature request or enhancementA feature request or enhancement
Description
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
bswck and SylvainDe
Metadata
Metadata
Assignees
Labels
stdlibPython modules in the Lib dirPython modules in the Lib dirtopic-replRelated to the interactive shellRelated to the interactive shelltype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or errortype-featureA feature request or enhancementA feature request or enhancement