Skip to content

Add back typo detection to sub-attribute access #137967

Closed

Description

@pablogsal

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

Activity

  1. added a commit that references this issue on Aug 19, 2025
  2. added
    type-featureA feature request or enhancement
    type-bugAn unexpected behavior, bug, or error
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Aug 19, 2025
  3. added
    stdlibStandard Library Python modules in the Lib/ directory
    topic-replRelated to the interactive shell
    on Aug 19, 2025
  4. added a commit that references this issue on Aug 19, 2025
  5. SylvainDe commented on Aug 20, 2025

    @SylvainDe
    Contributor

    Looks great!

    Out of curiosity, is there any way for not having the following suggestion :

    AttributeError: 'Container' object has no attribute 'area'. Did you mean: 'container.inner.area'?

    ? (Maybe it'd harder to implement, maybe it's just not wanted in the first place)

  6. pablogsal commented on Aug 20, 2025

    @pablogsal
    MemberAuthor

    Not sure I follow, are you saying you want container.inner.area instead of inner.area?

  7. SylvainDe commented on Aug 20, 2025

    @SylvainDe
    Contributor
  8. pablogsal commented on Aug 21, 2025

    @pablogsal
    MemberAuthor

    Yeah! I think it would make the suggestion easier to understand and to use. As a user, my interpretation of the current suggestion is that I've written container instead of inner (which would be the suggestion provided in case of typo iirc) but in fact, the container part is valid but I've forgotten a sub attribute. I hope it makes sense. If it does not, du not worry about it, I've probably missed something entirely. Le mer. 20 août 2025, 20:58, Pablo Galindo Salgado @.***> a écrit :
    …

    Yeah unfortunately is not possible or at least not very easy: at the time we produce the message we don't have access to the variable name the object is assigned to. We could find it in the frame but that's slow and is a but complicated so it will have some performance impact. Furthermore the rest of the similar error messages don't have the name and that will make it a bit inconsistent, so I think I am going to go with this as it is.

    Thanks for the suggestion though!

  9. SylvainDe commented on Aug 21, 2025

    @SylvainDe
    Contributor

    Sure! That totally makes sense! Thanks for everything you've done for error messages and for replying to my suggestion!

  10. added a commit that references this issue on Aug 21, 2025
  11. added 2 commits that reference this issue on Aug 21, 2025
  12. added a commit that references this issue on Aug 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-replRelated to the interactive shelltype-bugAn unexpected behavior, bug, or errortype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions