python / python/mypy

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

Open
#14,778 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in the issue and run mypy 0.990 to confirm that the import suppression also hides the abstract-method error in Foo. Trace the handling of # type: ignore on imports and add a regression test showing that the import is skipped without suppressing errors in code that uses the imported base class; done means the example reports Foo's missing foo implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.