Skip to content

Preserve concrete module extra_attrs in unions - #22577

Open
semanym wants to merge 1 commit into
python:masterfrom
semanym:fix/union-of-modules-preserve-extra-attrs
Open

semanym wants to merge 1 commit into
python:masterfrom
semanym:fix/union-of-modules-preserve-extra-attrs

Conversation

@semanym

@semanym semanym commented Nov 29, 2026

Copy link
Copy Markdown

Fixes #22302

Summary

Taking the union of two concrete modules (such as mod = json if flag else pickle) previously collapsed the type to bare types.ModuleType, losing all attribute and protocol checking.

Root Cause

  1. In mypy/typeops.py (_remove_redundant_union_items), two concrete module Instances share the same nominal TypeInfo (types.ModuleType), so is_proper_subtype treated the second module as redundant and dropped it; make_simplified_union then saw multiple distinct ExtraAttrs and erased extra_attrs to None.
  2. Even when concrete module extra_attrs were preserved on an Instance (e.g., m1 if flag else m1 or a union item), mypy/checkmember.py (analyze_member_var_access) fell back to types.ModuleType.__getattr__ -> Any whenever mx.module_symbol_table was None, and skipped itype.extra_attrs when mod_name was set.

Changes

  • mypy/typeops.py: Prevent _remove_redundant_union_items from treating one concrete module (extra_attrs.mod_name is not None) as a supertype of a different module or generic types.ModuleType. Identical modules still deduplicate, unions with generic types.ModuleType (extra_attrs is None) still simplify to types.ModuleType, and non-module extra_attrs (hasattr / functools.partial) remain unchanged.
  • mypy/types.py, mypy/server/astdiff.py: Include mod_name in ExtraAttrs.__hash__, ExtraAttrs.__eq__, and astdiff snapshots so distinct modules with identical attribute maps are not conflated.
  • mypy/checkmember.py: Skip types.ModuleType.__getattr__ when itype.extra_attrs.mod_name is set; resolve attributes (including Final write checks and PEP 562 module-level __getattr__) from itype.extra_attrs while preserving symbol-table precedence for direct module expressions (e.g., cyclic definitions).
  • mypy/checker.py, mypy/meet.py: Keep mod_name intact during hasattr() narrowing and return UninhabitedType (or NoneType without strict optional) when meeting two distinct concrete modules, enabling accurate hasattr() narrowing on unions of concrete modules.
  • mypy/binder.py: Avoid narrowing variables explicitly annotated as types.ModuleType (extra_attrs is None) to a concrete module on assignment, preserving explicit dynamic types.ModuleType escape hatches.
  • mypy/messages.py: Disambiguate distinct concrete modules as Module "<name>" via find_type_overlaps when multiple module types appear in the same diagnostic (e.g., [union-attr] errors).
  • test-data/unit/check-modules.test: Added regression tests covering unions of two concrete modules, common/missing/Final attributes, hasattr() narrowing, same-module deduplication, empty modules, unions with generic types.ModuleType, Protocol checking, and module-level __getattr__.

Verification

  • pytest -n 8 mypy/test/testcheck.py: 8363 passed, 16 skipped, 7 xfailed
  • pytest -n 8 mypy/test/testsubtypes.py mypy/test/testfinegrained.py mypy/test/testdeps.py mypy/test/testdiff.py: 1093 passed, 27 skipped
  • pytest -n 0 mypy/test/testsubtypes.py mypy/test/testtypes.py: 148 passed, 2 skipped
  • python -m mypy --config-file mypy_self_check.ini mypy: Success (0 issues in 195 source files)
  • black --check and ruff check on all modified files: passed

🤖 Generated with Claude Code

Previously, taking the union of two concrete modules (e.g. `json if flag else pickle`)
collapsed them into a bare `types.ModuleType` because `_remove_redundant_union_items`
treated any `types.ModuleType` instance as a subtype of another and `make_simplified_union`
then erased `extra_attrs`. Even when concrete module `extra_attrs` were retained,
`analyze_member_var_access` still fell back to `types.ModuleType.__getattr__ -> Any`.

This change:
- Keeps distinct concrete modules (different `extra_attrs.mod_name`) separate in
  `_remove_redundant_union_items` while still deduplicating identical modules and
  simplifying unions with generic `types.ModuleType` (`extra_attrs is None`).
- Includes `mod_name` in `ExtraAttrs.__eq__`/`__hash__` and `astdiff` snapshots.
- Resolves attribute lookups (and module-level `__getattr__` / `Final` checks) against
  `extra_attrs` for concrete module instances instead of falling back to
  `types.ModuleType.__getattr__`.
- Preserves explicit `types.ModuleType` annotations without narrowing in `binder.assign_type`,
  and handles concrete module meets and disambiguated module names in error messages.

Fixes python#22302

Co-Authored-By: Claude Code <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

colour (https://github.com/colour-science/colour)
- colour/__init__.py:1028: error: Incompatible import of "colour" (imported name has type Module, local name has type "type[colour]")  [assignment]
+ colour/__init__.py:1028: error: Incompatible import of "colour" (imported name has type Module "colour", local name has type "type[colour]")  [assignment]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant