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

Add an explanation of what error mypy produces when importing from a module with a top-line comment # type: ignore #14340

Closed
solzard opened this issue Dec 22, 2022 · 1 comment · Fixed by #14342
Labels
documentation topic-type-ignore # type: ignore comments

Comments

@solzard
Copy link

solzard commented Dec 22, 2022

Documentation

Ignoring a whole file explains the side effect of using # type: ignore at the top.

A # type: ignore comment at the top of a module (before any statements, including imports or docstrings) has the effect of ignoring the entire contents of the module.

However, it doesn't show what kind of errors mypy produces when importing from such a module.
As shown in the following example, mypy provides an error styled of error: Module "{{MODULE_NAME}}" has no attribute "{{ATTRIBUTE_NAME}}" [attr-defined].

Neither has no attribute nor [attr-defined] appears in Ignoring a whole file nor in the whole page of Common issues and solutions.

It took me an hour to find this cause, and I believe it's worth mentioning in the documentation.

foo.py:

from bar import bar_func, bar_value

bar.py:

# type: ignore
bar_value = "bar"

def bar_func():
    pass

Executing mypy on foo.py:

> mypy foo.py
foo.py:1: error: Module "bar" has no attribute "bar_func"  [attr-defined]
foo.py:1: error: Module "bar" has no attribute "bar_value"  [attr-defined]
Found 2 errors in 1 file (checked 1 source file)

Environment:

> mypy --version
mypy 0.991 (compiled: yes)

> python3 --version
Python 3.11.1

> sw_vers
ProductName:	macOS
ProductVersion:	12.6.2
BuildVersion:	21G320

> sysctl -n machdep.cpu.brand_string
Apple M1 Pro
@hauntsaninja
Copy link
Collaborator

Yeah, I agree that this is not intuitive. Let me know if #14342 would have helped you counterfactually

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation topic-type-ignore # type: ignore comments
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants