python / python/mypy

mypy assumes `importlib.metadata` is implicitly imported via `importlib`?

Open
#18,153 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-runtime-semantics
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

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 minimal reproduction in t.py and run it with mypy 1.13.0 on Python 3.13. Compare how mypy handles importlib.metadata with urllib.parse, then trace the import handling responsible for the differing diagnostics. Done means mypy reports the missing importlib.metadata access while preserving the existing urllib.parse error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.