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

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

Closed
johanvergeer opened this issue Aug 17, 2020 · 7 comments · Fixed by #13512
Closed
Labels
bug mypy got something wrong priority-1-normal topic-disallow-any The disallow-any-* family of flags topic-type-ignore # type: ignore comments

Comments

@johanvergeer
Copy link

johanvergeer commented Aug 17, 2020

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
@ilevkivskyi
Copy link
Member

Yeah, this looks like a bug.

@ilevkivskyi ilevkivskyi added bug mypy got something wrong priority-1-normal labels Sep 8, 2020
@RomainMuller
Copy link

RomainMuller commented Jan 25, 2021

I've got the exact same issue happening with file-wide # type: ignore[no-any-return], so I also believe this isn't quite specific to ignoring any particular code; instead related to file-wide ignoring anything.

@MetRonnie
Copy link

I had the same error message when importing from an auto-generated protobuf _pb2.py file, which has # type: ignore as the first line. I managed to suppress the errors by adding # type: ignore to the end of every line where we imported from the protobuf file, but that's pretty annoying.

@aberres
Copy link

aberres commented Sep 15, 2021

Yeps, same problem here.

@dpinol
Copy link

dpinol commented Nov 30, 2021

The workaround at the description is a pain when "type: ignore" needs to be placed in hundreds of lines.
Is there any other workaround?

@hauntsaninja
Copy link
Collaborator

Maybe a per module https://mypy.readthedocs.io/en/stable/config_file.html#confval-ignore_errors works for your use case?

@AlexWaygood AlexWaygood added topic-type-ignore # type: ignore comments topic-disallow-any The disallow-any-* family of flags labels Mar 31, 2022
@zplizzi
Copy link

zplizzi commented Jul 18, 2022

Fwiw, I replaced # type: ignore with # mypy: ignore-errors and this issue seemed to go away. Not sure if this works for ignoring specific errors though.

hauntsaninja added a commit to hauntsaninja/mypy that referenced this issue Aug 25, 2022
Per-module error codes were added in python#13502, let's recommend using them.

The existing type ignore behaviour is pretty unintuitive; I think most
people actually want `# mypy: ignore-errors`. There are probably people
depending on the current behaviour though.

Fixes python#13435, fixes python#12076, fixes python#11999, fixes python#11027, fixes python#9318,
fixes python#7839
hauntsaninja added a commit that referenced this issue Aug 26, 2022
Per-module error codes were added in #13502, let's recommend using them.

The existing type ignore behaviour is pretty unintuitive; I think most
people actually want `# mypy: ignore-errors`. There are probably people
depending on the current behaviour though.

Fixes #13435, fixes #12076, fixes #11999, fixes #11027, fixes #9318,
fixes #7839
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-1-normal topic-disallow-any The disallow-any-* family of flags topic-type-ignore # type: ignore comments
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants