Skip to content

Module ... has no attribute ... when #type: ignore[misc] is set at the top of a module #9318

@johanvergeer

Description

@johanvergeer

In our mypy config file we do not allow Any to be used. Because this cannot be avoided all the time we use #type: ignore[misc] at the top of a module.

We created a module called typing.py.

# type: ignore[misc] # Ignores 'Any' input parameter
from typing import Any, Iterable

JsonSerializable = Any
CsvSerializable = Iterable[Any]

This is imported in another module.

from my_app.typing import JsonSerializable

def foo(data: JsonSerializable) -> None:
    ...

Actual result

PS C:\workspace\my_app> mypy
src\my_app\foo.py:6: error: Module 'my_app.typing' has no attribute 'JsonSerializable'  [attr-defined]
    from my_app.typing import JsonSerializable

Found 1 errors in 1 files (checked 6 source files)

Expected result

PS C:\workspace\my_app> mypy
Success: no issues found in 6 source files

How did we solve it?

When we change typing.py so it ignores Any on each line we don't have any issues.

from typing import Any, Iterable

JsonSerializable = Any  # type: ignore[misc] # Ignores 'Any' input parameter
CsvSerializable = Iterable[Any]  # type: ignore[misc] # Ignores 'Any' input parameter

Configuration

  • Python 3.8.2
  • Mypy 0.782
  • Windows 10
  • Not in a virtualenv

.mypy.ini

[mypy]
python_version = 3.8

disallow_any_expr = False
disallow_any_decorated = False
disallow_any_explicit = True
disallow_any_generics = True
disallow_subclassing_any = True

disallow_untyped_calls = True
disallow_untyped_defs = True
check_untyped_defs = False
disallow_untyped_decorators = False

no_implicit_optional = True

warn_redundant_casts = True
warn_unused_ignores = True
warn_return_any = True
warn_unreachable = True
strict_equality = True

allow_redefinition = False

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions