Skip to content

"Intermittent" import error when checking a module that import another module which imports a 3rd party package #7276

Closed
@Quibi

Description

@Quibi

Hi,

Sorry for the title. I try to explain myself better.

I have found a strange behaviour in mypy related to an import error in a imported module. That is, an error in a module 'b' importing a module 'c' while checking module 'a', which imports 'b'. 'c', of course, is a 3rd party package.

In such a case, mypy reports nothing on a first round. But if it is run again, it reported a error: Cannot find module named 'c'. On a third run, nothing is reported again, while on a fourth run the error re-appears, and so on...

This happens even when the type check has been disabled in the import line.

A possible scenario to reproduce this "bug" is as follows:

  1. Create a 'strange_report' package (with an empty __init__.py).

  2. Create a subpackage called 'a' inside 'strange_report' (with an empty __init__.py too).

  3. Write an a.py module (importer module) such as:

from a.b import B

b = B()  # just for visually check that a.py was run
  1. Write a b.py module (imported module)
from ruamel.yaml import YAMLError  # type: ignore


class B:
    def __init__(self):
        print(f"New 'B' object {self} has been created")
  1. Open terminal and change to directory 'strange_report'.

  2. Module a.py can be run with python -m a.a

  3. If you run mypy a/a.py for the first time, you do not get any reports (however, directory '.mypy_cache' is created).

  4. But if you run mypy a/a.py once again, you get an error:

madera:strange_error jv$ mypy a/a.py
a/b.py:1: error: Cannot find module named 'ruamel'
a/a.py:1: error: Cannot find module named 'ruamel'
a/a.py:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
  1. You can repeat 7. and 8. again and again with similar results. Same results are gotten with or without # type: ignore.

However, if cache directory '.mypy_cache' is deleted before running mypy every time, it does not report anything.

Curiously enough, if you check mypy a/b.py it consistently reports nothing.

I am running on:

  • macOS HIgh Sierra 10.13.6
  • Python 3.7.4
  • mypy 0.720
  • ruamel.yaml 0.16.0 (so, yes, ruamel.yaml is installed; I use this 3rd party package because it is causing the bug in my system; no any particular purposes, I imagined that same bug can appeared with similar packages but cannot confirm that)
  • Any mypy configuration beyond the default one.

Thank for your attention. I hope that will be helpful.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions