Skip to content

appending #type:ignore to an import does more than skipping analyzing the import #14778

@solstice333

Description

@solstice333

Bug Report

I ran into relatively low priority unexpected behavior. I have this code

from anytree import NodeMixin # type:ignore
from typing import *
from abc import abstractmethod

class IFoo(Protocol):
    @abstractmethod
    def foo(self) -> None:
        raise NotImplementedError()

class Foo(IFoo, NodeMixin):
     ...

def main() -> None:
    a = Foo()

if __name__ == '__main__':
    main()

anytree is a third-party package. When I run mypy on it, I expect that static analysis on anytree to be skipped, and static analysis on Foo to fail with something like "Foo needs to implement abstractmethod foo". What actually happens is that mypy exits with success saying 0 errors were found.

What I've tried to do to cause the expected behavior to occur was to use stubgen on the anytree install which generated stubs in the project folder, run mypy --install-types to install type hints for a dependency of anytree, and configure a mypy.ini in project root to point to the stubs. All of which was simple to do, but kind of tedious.

I also read https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports where it mentioned

This can result in mypy failing to warn you about errors in your code. Since operations on Any result in Any, these dynamic types can propagate through your code, making type checking less effective. See Dynamically typed code for more information.

So I guess the suppression of static checking is leaking from anytree.NodeMixin into Foo.

Actual Behavior

mypy exits with success, 0 errors

Your Environment

I'm on Windows.

  • Mypy version used: 0.990
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.10.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions