Skip to content

Suggestions: check for name collisions between modules and items in a module #144285

Description

@ngoldbaum

This suggestion is pretty confusing, particularly if you don't understand that datetime.datetime is a thing.

>>> import datetime
>>> datetime.now()
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    datetime.now()
    ^^^^^^^^^^^^
AttributeError: module 'datetime' has no attribute 'now'. Did you mean: 'datetime.now'?

When the suggestion finds a match like this, it should also check to make sure the module name and the suggestion don't clash. Perhaps it should also be able to suggest datetime.datetime.now?

Linked PRs

Activity

  1. added
    type-featureA feature request or enhancement
    stdlibStandard Library Python modules in the Lib/ directory
    on Jan 27, 2026
  2. johnslavik commented on Jan 28, 2026

    @johnslavik
    Member

    This suggestion comes from traceback._check_nested_attribute.

    Perhaps it should also be able to suggest datetime.datetime.now?

    It probably won't. See @pablogsal's take on this in #137967 (comment).

    Detecting the clash would require checking the scope for the name, too (locals, globals, etc).
    Considering what Pablo said earlier, we'd likely want to avoid this either.

    Maybe we could just rephrase the error message?
    "Did you mean 'datetime.now' instead of 'now'"?

    AttributeError: module 'datetime' has no attribute 'now'. Did you mean 'datetime.now' instead of 'now'?
    

    That should be just a bit clearer.
    Pablo, what are your thoughts on this?

  3. pablogsal commented on Jan 28, 2026

    @pablogsal
    Member

    I think your suggestion makes it cleaner so I'm game!

  4. self-assigned this
    on Jan 28, 2026
  5. plocket commented on Jan 28, 2026

    @plocket

    Maybe we could just rephrase the error message? "Did you mean 'datetime.now' instead of 'now'"?

    AttributeError: module 'datetime' has no attribute 'now'. Did you mean 'datetime.now' instead of 'now'?
    

    I think rephrasing the message has the potential to be very helpful. Speaking just for myself, I'm not sure this particular message is a solution that would help me understand what's happening. The second sentence is still ambiguous about what datetime the message is talking about. As a reader I'd still think, "Yes, I did mean 'datetime.now', and that's what I wrote.'

    I'm not sure on the style guide Python has for messages (I wasn't sure how to find this particular nuance), but would something like the following be acceptable?

    AttributeError: module 'datetime' has no attribute '.now'. Did you mean '.datetime.now' instead of '.now'?
    

    In text that explains code, I find that dots (.) at the start of attributes help contextualize the message and do a more effective job of orienting me when I'm trying to understand how pieces of architecture fit together. For me, the dots emphasize a very crucial part of the picture and tells me where those names would belong at-a-glance.

    I apologize if this isn't an acceptable format in error messages, though. I suspect formatting of var names in error messages may belong in a broader discussion than this one issue.

  6. johnslavik commented on Jan 29, 2026

    @johnslavik
    Member

    Would you find this message equally understandable?

    AttributeError: module 'datetime' has no attribute 'now'. Did you mean '.datetime.now' instead of '.now'?

    Changing the first sentence is very unlikely.
    It's much lower level. Plus, without suggestions, the message could be confusing to people used to the old format ('.now' is not a valid identifier nor attribute name).

    Changing the second sentence is rather easy though, and it is not incorrect. I personally think it's an acceptable trade-off to only change the second sentence to the proposed format.

    @pablogsal?

  7. pablogsal commented on Feb 7, 2026

    @pablogsal
    Member

    Would you find this message equally understandable?

    AttributeError: module 'datetime' has no attribute 'now'. Did you mean '.datetime.now' instead of '.now'?

    Changing the first sentence is very unlikely.
    It's much lower level. Plus, without suggestions, the message could be confusing to people used to the old format ('.now' is not a valid identifier nor attribute name).

    Changing the second sentence is rather easy though, and it is not incorrect. I personally think it's an acceptable trade-off to only change the second sentence to the proposed format.

    @pablogsal?

    I think it's indeed more clear! Let's make this happen!

  8. johnslavik commented on Feb 8, 2026

    @johnslavik
    Member

    Thanks for the dot proposal, @plocket. This makes the change much stronger.

  9. added a commit that references this issue on Feb 12, 2026
  10. added a commit that references this issue on Feb 15, 2026
  11. added a commit that references this issue on Feb 21, 2026
  12. added 2 commits that reference this issue on Feb 28, 2026
  13. added 2 commits that reference this issue on Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions