Skip to content

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

Closed
@solzard

Description

@solzard

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions