Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusing message with AttributeError when attribute name matches suggestion #88821

Closed
ericvsmith opened this issue Jul 16, 2021 · 12 comments
Closed
Labels
3.10 only security fixes 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@ericvsmith
Copy link
Member

BPO 44655
Nosy @ericvsmith, @pablogsal, @miss-islington
PRs
  • bpo-44655: Don't include suggestions for attributes that are the same as the missing one #27197
  • [3.10] bpo-44655: Don't include suggestions for attributes that are the same as the missing one (GH-27197) #27198
  • bpo-44655: Include the name of the type in unset __slots__ attribute errors #27199
  • [3.10] bpo-44655: Include the name of the type in unset __slots__ attribute errors (GH-27199) #27201
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-07-17.00:02:22.544>
    created_at = <Date 2021-07-16.19:14:32.030>
    labels = ['interpreter-core', 'type-bug', '3.10', '3.11']
    title = 'Confusing message with AttributeError when attribute name matches suggestion'
    updated_at = <Date 2021-07-17.00:02:22.544>
    user = 'https://github.com/ericvsmith'

    bugs.python.org fields:

    activity = <Date 2021-07-17.00:02:22.544>
    actor = 'pablogsal'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-07-17.00:02:22.544>
    closer = 'pablogsal'
    components = ['Interpreter Core']
    creation = <Date 2021-07-16.19:14:32.030>
    creator = 'eric.smith'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 44655
    keywords = ['patch']
    message_count = 12.0
    messages = ['397652', '397654', '397656', '397657', '397658', '397659', '397663', '397668', '397670', '397671', '397675', '397676']
    nosy_count = 3.0
    nosy_names = ['eric.smith', 'pablogsal', 'miss-islington']
    pr_nums = ['27197', '27198', '27199', '27201']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue44655'
    versions = ['Python 3.10', 'Python 3.11']

    @ericvsmith
    Copy link
    Member Author

    This is related to bpo-44649. This is the simplest non-dataclasses case I could come up with.

    Given:

    class Example:
        __slots__ = ('a', 'b')
        def __init__(self, a):
            self.a = a
    
    obj = Example(42)
    print(obj.b)

    I get this in 3.10 and main:

    Traceback (most recent call last):
      File "C:\home\eric\local\python\cpython\testdc.py", line 7, in <module>
        print(obj.b)
              ^^^^^
    AttributeError: b. Did you mean: 'b'?

    The error message is confusing.

    3.8 gives:

    Traceback (most recent call last):
      File "testdc.py", line 7, in <module>
        print(obj.b)
    AttributeError: b

    I don't have 3.9 around to test with.

    Maybe don't print the "Did you mean" part if the suggestion is the same as the requested attribute?

    The fact that the instance variable isn't initialized is the actual error in bpo-44649, and I'll fix it there.

    @ericvsmith ericvsmith added 3.10 only security fixes 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Jul 16, 2021
    @ericvsmith
    Copy link
    Member Author

    Also, the dot after the first 'b' was confusing to me: I thought it had something to do with an attribute of b. And the quotes around the second 'b' were also confusing, but that's mostly because the original example initialized a class variable named 'b' with the str value 'b'.

    Maybe a better error would use a colon:

    AttributeError: foo: Did you mean: 'foo1'?

    Looking at 3.10s behavior, I can't decide what logic it's using to suggest something:

    >>> 'foo'.formatx
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'str' object has no attribute 'formatx'. Did you mean: 'format'?
    >>> class C: pass
    ...
    >>> C().a
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'C' object has no attribute 'a'

    @pablogsal
    Copy link
    Member

    For attribute errors we just call dir() on the object and we do a suggestion based on the names so if a name appears in the dir() then we will consider it for the suggestion.

    @pablogsal
    Copy link
    Member

    Maybe don't print the "Did you mean" part if the suggestion is the same as the requested attribute?

    I think this is a good idea and will take care of many other similar cases. Thanks for the suggestion, Eric! I will prepare a PR

    @ericvsmith
    Copy link
    Member Author

    It's obviously not super important, but it would be nicer if the version with the suggestion were more like the version without the suggestion. Specifically, mentioning the object type:

    >>> class E:
    ...     __slots__=('a')
    ...
    >>> E().a
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: a. Did you mean: 'a'?
    >>> E().b
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'E' object has no attribute 'b'
    >>>

    @ericvsmith
    Copy link
    Member Author

    Thanks, Pablo!

    @pablogsal
    Copy link
    Member

    it would be nicer if the version with the suggestion were more like the version without the suggestion

    Agreed, but that is actually not related with the suggestions. We only append a string at the end of whatever exception it was there. This difference is based on the attribute error for a slot not initialized vs an attribute error for something that is not there. For example, in 3.9:

    ---

    class E:
        __slots__=('blech')

    E().blech

    Traceback (most recent call last):
      File "/home/pablogsal/github/cpython/lel.py", line 6, in <module>
        E().blech
    AttributeError: blech

    while

    class E:
        __slots__=('blech')

    E().bluch

    Traceback (most recent call last):
      File "/home/pablogsal/github/cpython/lel.py", line 4, in <module>
        E().bluch
    AttributeError: 'E' object has no attribute 'bluch'

    @pablogsal
    Copy link
    Member

    New changeset 6714dec by Pablo Galindo Salgado in branch 'main':
    bpo-44655: Don't include suggestions for attributes that are the same as the missing one (GH-27197)
    6714dec

    @ericvsmith
    Copy link
    Member Author

    Got it. Thanks for jumping on this quickly.

    @ericvsmith ericvsmith changed the title Confusing error with __slots__ Confusing message with AttributreError suggestions Jul 16, 2021
    @ericvsmith ericvsmith changed the title Confusing error with __slots__ Confusing message with AttributreError suggestions Jul 16, 2021
    @ericvsmith ericvsmith changed the title Confusing message with AttributreError suggestions Confusing message with AttributreError when attribute name matches suggestion Jul 16, 2021
    @ericvsmith ericvsmith changed the title Confusing message with AttributreError suggestions Confusing message with AttributreError when attribute name matches suggestion Jul 16, 2021
    @pablogsal
    Copy link
    Member

    New changeset a0b1d40 by Miss Islington (bot) in branch '3.10':
    bpo-44655: Don't include suggestions for attributes that are the same as the missing one (GH-27197) (GH-27198)
    a0b1d40

    @ericvsmith ericvsmith changed the title Confusing message with AttributreError when attribute name matches suggestion Confusing message with AttributeError when attribute name matches suggestion Jul 16, 2021
    @ericvsmith ericvsmith changed the title Confusing message with AttributreError when attribute name matches suggestion Confusing message with AttributeError when attribute name matches suggestion Jul 16, 2021
    @pablogsal
    Copy link
    Member

    New changeset f783428 by Pablo Galindo Salgado in branch 'main':
    bpo-44655: Include the name of the type in unset __slots__ attribute errors (GH-27199)
    f783428

    @pablogsal
    Copy link
    Member

    New changeset efda905 by Miss Islington (bot) in branch '3.10':
    bpo-44655: Include the name of the type in unset __slots__ attribute errors (GH-27199) (GH-27201)
    efda905

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants