Skip to content

Add types.ModuleType.__doc__#12918

Merged
JelleZijlstra merged 1 commit intomainfrom
AlexWaygood-ModuleType-doc
Oct 29, 2024
Merged

Add types.ModuleType.__doc__#12918
JelleZijlstra merged 1 commit intomainfrom
AlexWaygood-ModuleType-doc

Conversation

@AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Oct 29, 2024

This would be helpful for red-knot, the work-in-progress type checker we're building at Astral.

To figure out which symbols are available in all modules as "implicit globals", red-knot iterates through the symbols declared in typeshed's stub for types.ModuleType. Currently the only implicit global that is not declared in the stub for types.ModuleType is __doc__1; this PR adds it to the stub.

While adding ModuleType.__doc__ is somewhat "redundant" from a purist view of the stubs (since it's the same type as object.__doc__, and ModuleType inherits from object), it's still accurate. We could special-case __doc__ in red-knot instead of adding it to the typeshed stubs, but we'd prefer to figure out which attributes exist as implicit globals by inspecting typeshed's stubs and doing the absolute minimum amount of special-casing. This approach means that we'll naturally pick up new implicit globals if they're ever added to the stubs in the future.

Footnotes

  1. ...Well. __annotations__ is also available as an implicit global in some versions of Python:

    Python 3.13.0 (main, Oct  8 2024, 11:56:29) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> __annotations__
    {}
    

    But not on others:

    Python 3.14.0a1+ (heads/main:91ddde4af0c, Oct 22 2024, 14:16:04) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> __annotations__
    Traceback (most recent call last):
      File "<python-input-0>", line 1, in <module>
        __annotations__
    NameError: name '__annotations__' is not defined
    

    So it seems like it's probably a bad idea for users to rely on that one being defined as an implicit global. Therefore this PR does not add ModuleType.__annotations__, only ModuleType.__doc__.

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@JelleZijlstra JelleZijlstra merged commit 01e7e80 into main Oct 29, 2024
@JelleZijlstra JelleZijlstra deleted the AlexWaygood-ModuleType-doc branch October 29, 2024 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants