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

mypy assumes importlib.metadata is implicitly imported via importlib? #18153

Open
asottile-sentry opened this issue Nov 13, 2024 · 1 comment
Labels
bug mypy got something wrong topic-runtime-semantics mypy doesn't model runtime semantics correctly

Comments

@asottile-sentry
Copy link

Bug Report

mypy seems to assume that import importlib brings in the relevant submodules (though I can't see anything in typeshed that would indicate this?) even though it does not

importlib.metadata is not implicitly brought in by importlib:

$ python3 -c 'import importlib; importlib.metadata'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: module 'importlib' has no attribute 'metadata'

To Reproduce

import importlib

reveal_type(importlib.metadata)  # expected error but none?

import urllib

reveal_type(urllib.parse)  # similar situation but properly errors!

Expected Behavior

I expect two errors, but I only get one

Actual Behavior

$ mypy t.py
t.py:3: note: Revealed type is "types.ModuleType"
t.py:7: error: Module has no attribute "parse"  [attr-defined]
t.py:7: note: Revealed type is "Any"
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.13.0
  • Mypy command-line flags: n/a
  • Mypy configuration options from mypy.ini (and other config files): n/a
  • Python version used: 3.13.0
@asottile-sentry asottile-sentry added the bug mypy got something wrong label Nov 13, 2024
@brianschubert brianschubert added the topic-runtime-semantics mypy doesn't model runtime semantics correctly label Nov 13, 2024
@brianschubert
Copy link
Collaborator

It looks like this happens because importlib indirectly imports importlib.metadata in typeshed (importlib imports importlib.abc which imports importlib._bootstrap_external which imports _frozen_importlib_external which finally imports importlib.metadata), so mypy considers importlib.metadata to exist, despite this not being the case at runtime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-runtime-semantics mypy doesn't model runtime semantics correctly
Projects
None yet
Development

No branches or pull requests

2 participants