Skip to content

Importing from non-py.typed module -> "Module has no attribute" with --strict; mypy still suggests the names it doesn't accept #13908

Description

@sliedes

To me, this issue has a vague resemblance to #10360. I do not know if it is really related.

With --strict, referring to variables imported from a non-py.typed module in another module causes mypy to output two error messages of the form Module has no attribute A; maybe B? and Module has no attribute B; maybe A?, for the same module. This obviously does not make a whole lot of sense.

I do not know if this applies generally to non-py.typed packages or if there's something special about the diffusers package I used.

How to reproduce

$ pip install diffusers==0.5.1

(see the Environment section for the precise packages installed by this)

# bug.py
from diffusers import StableDiffusionImg2ImgPipeline, StableDiffusionPipeline

# bug2.py
import bug

print(bug.StableDiffusionImg2ImgPipeline)
print(bug.StableDiffusionPipeline)

Actual Behavior

$ mypy --strict bug2.py 
bug.py:1: error: Skipping analyzing "diffusers": module is installed, but missing library stubs or py.typed marker  [import]
bug.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
bug2.py:3: error: Module has no attribute "StableDiffusionImg2ImgPipeline"; maybe "StableDiffusionPipeline"?  [attr-defined]
bug2.py:4: error: Module has no attribute "StableDiffusionPipeline"; maybe "StableDiffusionImg2ImgPipeline"?  [attr-defined]
Found 3 errors in 2 files (checked 1 source file)

Without --strict, the "Skipping analyzing" error is the only one produced. With --ignore-missing-imports or # type: ignore[import] for the import in bug.py, only the "Module has no attribute" errors are produced.

Expected Behavior

Mypy should

  1. Understand that the two attributes exist in the bug module;
  2. Even failing (1), not suggest options it thinks don't exist.

So I would expect the "Skipping analyzing" to be the only error message mypy outputs.

Your Environment

  • Mypy version used: mypy 0.982 (compiled: yes)
  • Mypy command-line flags: --config-file= --strict
  • Mypy configuration options from mypy.ini (and other config files): None (--config-file=)
  • Python version used: 3.10.8
$ pip freeze
certifi==2022.9.24
charset-normalizer==2.1.1
diffusers==0.5.1
filelock==3.8.0
huggingface-hub==0.10.1
idna==3.4
importlib-metadata==5.0.0
mypy==0.982
mypy-extensions==0.4.3
numpy==1.23.4
packaging==21.3
Pillow==9.2.0
pyparsing==3.0.9
PyYAML==6.0
regex==2022.9.13
requests==2.28.1
tomli==2.0.1
tqdm==4.64.1
typing_extensions==4.4.0
urllib3==1.26.12
zipp==3.9.0

Activity

  1. hauntsaninja commented on Oct 17, 2022

    @hauntsaninja
    Collaborator

    This is because --strict implies --no-implicit-reexport. Do from diffusers import StableDiffusionImg2ImgPipeline as StableDiffusionImg2ImgPipeline

    That said, the error message should clearly be better. #9084 / #9237 improved the error message, but looks like due to various issues with the implementation it doesn't actually trigger on your example. Easy enough to fix though

  2. added a commit that references this issue on Oct 18, 2022
    fdacaa9
  3. added 3 commits that reference this issue on Oct 19, 2022
    9bba377
    0f6067d
    8d5b641
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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