Skip to content

[ty] Don't promote module-literal types#23786

Merged
AlexWaygood merged 2 commits intomainfrom
alex/dont-promote-modules
Mar 7, 2026
Merged

[ty] Don't promote module-literal types#23786
AlexWaygood merged 2 commits intomainfrom
alex/dont-promote-modules

Conversation

@AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Mar 7, 2026

Summary

The motivation for doing this was always pretty weak: types.ModuleType is an extremely broad/vague type that isn't very useful to work with. You almost always do not want a specific module inferred as being a types.ModuleType instance unless you've explicitly annotated it as such.

It also seems completely untested: no tests fail from this change.

Test plan

I added an mdtest that fails on main.

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Mar 7, 2026
@astral-sh-bot
Copy link

astral-sh-bot bot commented Mar 7, 2026

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 87.10%. The percentage of expected errors that received a diagnostic held steady at 77.81%.

@astral-sh-bot
Copy link

astral-sh-bot bot commented Mar 7, 2026

Memory usage report

Summary

Project Old New Diff Outcome
sphinx 265.40MB 265.43MB +0.01% (28.33kB)
flake8 47.90MB 47.90MB -
prefect 693.71MB 693.71MB -
trio 117.86MB 117.86MB -

Significant changes

Click to expand detailed breakdown

sphinx

Name Old New Diff Outcome
check_file_impl 5.12MB 5.13MB +0.13% (6.76kB)
infer_deferred_types 5.60MB 5.60MB +0.05% (2.59kB)
infer_definition_types 24.06MB 24.06MB +0.01% (1.83kB)
FunctionType<'db>::signature_ 2.28MB 2.28MB +0.08% (1.81kB)
CallableType 1.07MB 1.07MB +0.16% (1.76kB)
FunctionType 3.12MB 3.12MB +0.05% (1.72kB)
is_redundant_with_impl::interned_arguments 2.07MB 2.07MB +0.08% (1.72kB)
IntersectionType 895.83kB 897.20kB +0.15% (1.38kB)
StaticClassLiteral<'db>::try_mro_ 2.11MB 2.11MB +0.06% (1.29kB)
infer_expression_types_impl 21.50MB 21.50MB +0.01% (1.17kB)
is_redundant_with_impl 1.81MB 1.81MB +0.05% (1008.00B)
Type<'db>::apply_specialization_ 1.65MB 1.65MB +0.04% (760.00B)
StaticClassLiteral<'db>::implicit_attribute_inner_ 2.38MB 2.38MB -0.03% (672.00B)
Type<'db>::apply_specialization_::interned_arguments 1.45MB 1.45MB +0.04% (640.00B)
StaticClassLiteral<'db>::implicit_attribute_inner_::interned_arguments 1.91MB 1.91MB -0.03% (576.00B)
... 25 more

@astral-sh-bot
Copy link

astral-sh-bot bot commented Mar 7, 2026

mypy_primer results

Changes were detected when running on open source projects
cibuildwheel (https://github.com/pypa/cibuildwheel)
- cibuildwheel/platforms/__init__.py:31:67: error[invalid-assignment] Object of type `dict[Literal["linux", "macos", "windows", "pyodide", "android", "ios"], PlatformModule | ModuleType]` is not assignable to `dict[Literal["linux", "macos", "windows", "pyodide", "android", "ios"], PlatformModule]`
+ cibuildwheel/platforms/__init__.py:31:67: error[invalid-assignment] Object of type `dict[Literal["linux", "macos", "windows", "pyodide", "android", "ios"], PlatformModule | <module 'cibuildwheel.platforms.linux'> | <module 'cibuildwheel.platforms.windows'> | ... omitted 4 union elements]` is not assignable to `dict[Literal["linux", "macos", "windows", "pyodide", "android", "ios"], PlatformModule]`

pycryptodome (https://github.com/Legrandin/pycryptodome)
- lib/Crypto/SelfTest/Hash/test_CMAC.py:434:9: error[invalid-assignment] Invalid subscript assignment with key of type `Literal[4]` and value of type `dict[str, str | ModuleType]` on object of type `list[str | ModuleType]`
+ lib/Crypto/SelfTest/Hash/test_KMAC.py:328:28: error[invalid-argument-type] Argument to function `new` is incorrect: Expected `bytes | bytearray | memoryview[int]`, found `Unknown | str`
+ lib/Crypto/SelfTest/Hash/test_KMAC.py:328:28: error[invalid-argument-type] Argument to function `new` is incorrect: Expected `bytes | bytearray | memoryview[int]`, found `Unknown | str`
+ lib/Crypto/SelfTest/Hash/test_KMAC.py:328:39: error[invalid-argument-type] Argument to function `new` is incorrect: Expected `bytes | bytearray | memoryview[int]`, found `Unknown | str`
+ lib/Crypto/SelfTest/Hash/test_KMAC.py:328:39: error[invalid-argument-type] Argument to function `new` is incorrect: Expected `bytes | bytearray | memoryview[int]`, found `Unknown | str`
+ lib/Crypto/SelfTest/Hash/test_KMAC.py:328:48: error[invalid-argument-type] Argument to function `new` is incorrect: Expected `bytes | bytearray | memoryview[int]`, found `Unknown | str`
+ lib/Crypto/SelfTest/Hash/test_KMAC.py:328:48: error[invalid-argument-type] Argument to function `new` is incorrect: Expected `bytes | bytearray | memoryview[int]`, found `Unknown | str`
- lib/Crypto/SelfTest/Hash/test_TupleHash.py:269:13: error[invalid-assignment] Invalid subscript assignment with key of type `Literal[4]` and value of type `Unknown | ModuleType` on object of type `list[None]`
+ lib/Crypto/SelfTest/Hash/test_TupleHash.py:269:13: error[invalid-assignment] Invalid subscript assignment with key of type `Literal[4]` and value of type `Unknown | <module 'Crypto.Hash.TupleHash128'> | <module 'Crypto.Hash.TupleHash256'>` on object of type `list[None]`
+ lib/Crypto/SelfTest/Hash/test_TupleHash.py:276:30: error[invalid-argument-type] Argument to function `new` is incorrect: Expected `int`, found `Unknown | str`
+ lib/Crypto/SelfTest/Hash/test_TupleHash.py:276:30: error[invalid-argument-type] Argument to function `new` is incorrect: Expected `int`, found `Unknown | str`
+ lib/Crypto/SelfTest/Hash/test_TupleHash.py:277:30: error[invalid-argument-type] Argument to function `new` is incorrect: Expected `int`, found `Unknown | str`
+ lib/Crypto/SelfTest/Hash/test_TupleHash.py:277:30: error[invalid-argument-type] Argument to function `new` is incorrect: Expected `int`, found `Unknown | str`
+ lib/Crypto/SelfTest/Hash/test_TupleHash.py:281:28: error[invalid-argument-type] Argument to bound method `update` is incorrect: Expected `bytes | bytearray | memoryview[int]`, found `Unknown | str`
+ lib/Crypto/SelfTest/Hash/test_TupleHash.py:284:24: error[invalid-argument-type] Argument to bound method `update` is incorrect: Expected `bytes | bytearray | memoryview[int]`, found `Unknown | str`
- lib/Crypto/SelfTest/Protocol/test_KDF.py:332:35: error[invalid-argument-type] Argument to function `HKDF` is incorrect: Expected `int`, found `Unknown | int | ModuleType`
+ lib/Crypto/SelfTest/Protocol/test_KDF.py:332:35: error[invalid-argument-type] Argument to function `HKDF` is incorrect: Expected `int`, found `Unknown | int | <module 'Crypto.Hash.SHA256'> | <module 'Crypto.Hash.SHA1'>`
- lib/Crypto/SelfTest/Protocol/test_KDF.py:332:50: error[invalid-argument-type] Argument to function `HKDF` is incorrect: Expected `ModuleType`, found `Unknown | int | ModuleType`
+ lib/Crypto/SelfTest/Protocol/test_KDF.py:332:50: error[invalid-argument-type] Argument to function `HKDF` is incorrect: Expected `ModuleType`, found `Unknown | int | <module 'Crypto.Hash.SHA256'> | <module 'Crypto.Hash.SHA1'>`
- lib/Crypto/SelfTest/Signature/test_dss.py:652:18: error[unresolved-attribute] Attribute `test_key` is not defined on `tuple[str, str, str, str, ModuleType, str]` in union `Unknown | tuple[str, str, str, str, ModuleType, str]`
+ lib/Crypto/SelfTest/Signature/test_dss.py:652:18: error[unresolved-attribute] Attribute `test_key` is not defined on `tuple[str, str, str, str, <module 'Crypto.Hash.SHA1'>, str]`, `tuple[str, str, str, str, <module 'Crypto.Hash.SHA224'>, str]`, `tuple[str, str, str, str, <module 'Crypto.Hash.SHA256'>, str]`, `tuple[str, str, str, str, <module 'Crypto.Hash.SHA384'>, str]`, `tuple[str, str, str, str, <module 'Crypto.Hash.SHA512'>, str]` in union `Unknown | tuple[str, str, str, str, <module 'Crypto.Hash.SHA1'>, str] | tuple[str, str, str, str, <module 'Crypto.Hash.SHA224'>, str] | ... omitted 3 union elements`
- lib/Crypto/SelfTest/Signature/test_dss.py:656:24: error[unresolved-attribute] Attribute `module` is not defined on `tuple[str, str, str, str, ModuleType, str]` in union `Unknown | tuple[str, str, str, str, ModuleType, str]`
- lib/Crypto/SelfTest/Signature/test_dss.py:656:39: error[unresolved-attribute] Attribute `message` is not defined on `tuple[str, str, str, str, ModuleType, str]` in union `Unknown | tuple[str, str, str, str, ModuleType, str]`
- lib/Crypto/SelfTest/Signature/test_dss.py:658:30: error[unresolved-attribute] Attribute `result` is not defined on `tuple[str, str, str, str, ModuleType, str]` in union `Unknown | tuple[str, str, str, str, ModuleType, str]`
+ lib/Crypto/SelfTest/Signature/test_dss.py:656:24: error[unresolved-attribute] Attribute `module` is not defined on `tuple[str, str, str, str, <module 'Crypto.Hash.SHA1'>, str]`, `tuple[str, str, str, str, <module 'Crypto.Hash.SHA224'>, str]`, `tuple[str, str, str, str, <module 'Crypto.Hash.SHA256'>, str]`, `tuple[str, str, str, str, <module 'Crypto.Hash.SHA384'>, str]`, `tuple[str, str, str, str, <module 'Crypto.Hash.SHA512'>, str]` in union `Unknown | tuple[str, str, str, str, <module 'Crypto.Hash.SHA1'>, str] | tuple[str, str, str, str, <module 'Crypto.Hash.SHA224'>, str] | ... omitted 3 union elements`
+ lib/Crypto/SelfTest/Signature/test_dss.py:656:39: error[unresolved-attribute] Attribute `message` is not defined on `tuple[str, str, str, str, <module 'Crypto.Hash.SHA1'>, str]`, `tuple[str, str, str, str, <module 'Crypto.Hash.SHA224'>, str]`, `tuple[str, str, str, str, <module 'Crypto.Hash.SHA256'>, str]`, `tuple[str, str, str, str, <module 'Crypto.Hash.SHA384'>, str]`, `tuple[str, str, str, str, <module 'Crypto.Hash.SHA512'>, str]` in union `Unknown | tuple[str, str, str, str, <module 'Crypto.Hash.SHA1'>, str] | tuple[str, str, str, str, <module 'Crypto.Hash.SHA224'>, str] | ... omitted 3 union elements`
+ lib/Crypto/SelfTest/Signature/test_dss.py:658:30: error[unresolved-attribute] Attribute `result` is not defined on `tuple[str, str, str, str, <module 'Crypto.Hash.SHA1'>, str]`, `tuple[str, str, str, str, <module 'Crypto.Hash.SHA224'>, str]`, `tuple[str, str, str, str, <module 'Crypto.Hash.SHA256'>, str]`, `tuple[str, str, str, str, <module 'Crypto.Hash.SHA384'>, str]`, `tuple[str, str, str, str, <module 'Crypto.Hash.SHA512'>, str]` in union `Unknown | tuple[str, str, str, str, <module 'Crypto.Hash.SHA1'>, str] | tuple[str, str, str, str, <module 'Crypto.Hash.SHA224'>, str] | ... omitted 3 union elements`
- Found 1349 diagnostics
+ Found 1360 diagnostics

sympy (https://github.com/sympy/sympy)
- sympy/utilities/tests/test_lambdify.py:2296:24: error[invalid-argument-type] Argument to bound method `append` is incorrect: Expected `ModuleType`, found `Literal["numpy"]`
+ sympy/utilities/tests/test_lambdify.py:2296:24: error[invalid-argument-type] Argument to bound method `append` is incorrect: Expected `<module 'math'>`, found `Literal["numpy"]`

@astral-sh-bot
Copy link

astral-sh-bot bot commented Mar 7, 2026

ecosystem-analyzer results

Lint rule Added Removed Changed
invalid-argument-type 12 0 3
unresolved-attribute 0 0 4
invalid-assignment 0 1 2
Total 12 1 9

Full report with detailed diff (timing results)

@AlexWaygood
Copy link
Member Author

The new ecosystem diagnostics on pycryptodome seem like a pretty clear improvement to me -- I've added an mdtest that's a minimized version of them and fails on main.

@AlexWaygood AlexWaygood marked this pull request as ready for review March 7, 2026 15:31
@astral-sh-bot astral-sh-bot bot requested a review from oconnor663 March 7, 2026 15:31
@AlexWaygood AlexWaygood merged commit edac24b into main Mar 7, 2026
51 checks passed
@AlexWaygood AlexWaygood deleted the alex/dont-promote-modules branch March 7, 2026 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ecosystem-analyzer ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants