Skip to content

Fix __annotations__ being undefined #10969

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

Merged
merged 72 commits into from
Mar 14, 2022

Conversation

Nickedude
Copy link
Contributor

@Nickedude Nickedude commented Aug 11, 2021

Fix annotations being undefined

Closes #10330

Using the variable __annotations__ currently leads to an error being raised incorrectly by mypy, as shown in the example in issue #10330:

print(__annotations__)

test.py:1: error: Name '__annotations__' is not defined
Found 1 error in 1 file (checked 1 source file)

This is because mypy can't find the definition of __annotations__. This PR adds a definition of __annotations__ to mypy, which means this type of error should not be raised anymore.

@Nickedude
Copy link
Contributor Author

@JukkaL bump :)

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, there's a problem with test stubs.

@Nickedude Nickedude requested a review from JukkaL December 12, 2021 10:37
Copy link
Collaborator

@97littleleaf11 97littleleaf11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@github-actions

This comment has been minimized.

@97littleleaf11
Copy link
Collaborator

@AlexWaygood @hauntsaninja Any suggestions on stub test failures?

@AlexWaygood
Copy link
Member

AlexWaygood commented Mar 13, 2022

Interesting! The teststubtest failures are (I think) because of this behaviour on Python <= 3.9 for modules which do not have any type annotations in their global namespace:

>>> import functools
>>> functools.__annotations__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'functools' has no attribute '__annotations__'

In Python >= 3.10, the behaviour is different: __annotations__ is always "present" (I think strictly speaking it's a descriptor, but let's leave that aside) in a module's global namespace, even if there are zero type annotations in that module's global namespace:

>>> import functools
>>> getattr(functools, "__annotations__")
{}
>>> hasattr(functools, "__annotations__")
True

So I'd probably say, in an ideal world:

  • __annotations__ should always be added as an implicit module attribute if the Python version is >= 3.10.
  • For Python < 3.10, it should probably only be added as an implicit module attribute if:
    • The module is being run as main; or,
    • The module has at least one type annotation in the global namespace.

But that sounds... complicated ://

@hauntsaninja
Copy link
Collaborator

I find https://docs.python.org/3/howto/annotations.html#annotations-howto to be a useful reference.

Leaving aside questions of how __annotations__ should work, if you just want stubtest to shut up, just add __annotations__ to here (make sure to add it above the TODO):

"__path__", # mypy adds __path__ to packages, but C packages don't have it

@github-actions
Copy link
Contributor

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

@97littleleaf11
Copy link
Collaborator

97littleleaf11 commented Mar 14, 2022

if you just want stubtest to shut up

Yeah let's just follow other dunders.

@hauntsaninja hauntsaninja merged commit f9dadb1 into python:master Mar 14, 2022
@hauntsaninja
Copy link
Collaborator

Thanks for getting this over the line!

@KotlinIsland
Copy link
Contributor

#4182

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.

Module-level __annotations__ variable is flagged as undefined
8 participants