Skip to content

Provide a hint when accidentally calling a module #103899

Description

@brandtbucher

This is sort of inspired by PEP 713, but I think it's worth doing whether that PEP is accepted or not.

When somebody attempts to call a module, we can provide a more helpful TypeError message than 'module' object is not callable. Instead, we can check if the module has a callable attribute with the same name:

>>> import pprint
>>> pprint(thing)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable. Did you mean: 'pprint.pprint(...)'?

As somebody who hits this fairly frequently with modules like dis and datetime, I think that this error message could reduce some confusion for beginners and friction for experienced users.

This is a cold code path, and I assume that this error is the most common reason for hitting it with a module object. So a little extra code (just one branch for non-modules) shouldn't be noticeable at all.

Linked PRs

Activity

  1. added
    type-featureA feature request or enhancement
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    3.12only security fixes
    on Apr 26, 2023
  2. self-assigned this
    on Apr 26, 2023
  3. brandtbucher commented on Apr 26, 2023

    @brandtbucher
    MemberAuthor
  4. added a commit that references this issue on May 4, 2023
  5. moved this from Todo to Done in Sprinton May 4, 2023
  6. added a commit that references this issue on May 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.12only security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)sprinttype-featureA feature request or enhancement

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions