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

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

Closed
sliedes opened this issue Oct 16, 2022 · 1 comment · Fixed by #13917
Labels
bug mypy got something wrong

Comments

@sliedes
Copy link

sliedes commented Oct 16, 2022

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
@sliedes sliedes added the bug mypy got something wrong label Oct 16, 2022
@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Oct 17, 2022

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

hauntsaninja added a commit to hauntsaninja/mypy that referenced this issue Oct 18, 2022
Also only suggest public API for attribute access suggestions

Fixes python#13908

Accomplishes a similar thing to python#9084 (the logic from there could be
improved too, will send a PR for that next)
hauntsaninja added a commit that referenced this issue Oct 19, 2022
Also only suggest public API for attribute access suggestions

Fixes #13908

Accomplishes a similar thing to #9084 (the logic from there could be
improved too, will send a PR for that next)
hauntsaninja added a commit to hauntsaninja/mypy that referenced this issue Oct 19, 2022
Related to python#13917 and python#13908. This handles situations involving
per-module re-exports correctly / is consistent with the error you'd get
with attribute access.
hauntsaninja added a commit that referenced this issue Oct 20, 2022
Related to #13917 and #13908. This handles situations involving
per-module re-exports correctly / is consistent with the error you'd get
with attribute access.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants