Skip to content

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Jul 9, 2025

Summary

Fixes astral-sh/ty#790. For single-file modules (that could never have submodules), we no longer store which file imported the module on the ModuleLiteralType struct itself. This means that <module 'typing'> is always equivalent to <module 'typing'>, and <module 'typing'> | int | str is always equivalent to int | str | <module 'typing'>.

Module-literal types representing packages are still not considered equivalent types if they were originally imported in different modules, because we would not treat the types equivalently! Tests are added demonstrating why this needs to be the case.

Test Plan

  • mdtests
  • Ran QUICKCHECK_TESTS=100000 cargo test --release -p ty_python_semantic -- --ignored types::property_tests::stable

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Jul 9, 2025
Copy link
Contributor

github-actions bot commented Jul 9, 2025

mypy_primer results

Changes were detected when running on open source projects
meson (https://github.com/mesonbuild/meson)
- error[invalid-assignment] unittests/failuretests.py:49:5: Object of type `def new_which(cmd, *kwargs) -> Unknown` is not assignable to attribute `which` on type `<module 'shutil'> | <module 'shutil'>`
+ error[invalid-assignment] unittests/failuretests.py:49:5: Implicit shadowing of function `which`
Memory usage changes were detected when running on open source projects
trio (https://github.com/python-trio/trio)
-     memo metadata = ~21MB
+     memo metadata = ~20MB
-     memo fields = ~159MB
+     memo fields = ~152MB

sphinx (https://github.com/sphinx-doc/sphinx)
-     struct fields = ~15MB
+     struct fields = ~14MB

prefect (https://github.com/PrefectHQ/prefect)
-     struct metadata = ~25MB
+     struct metadata = ~23MB

@AlexWaygood
Copy link
Member Author

Mypy_primer indicates that this leads to a small reduction in memory usage and fewer error messages that mention confusing types such as <module 'shutil'> | <module 'shutil'> 😆

@AlexWaygood AlexWaygood marked this pull request as ready for review July 9, 2025 18:41
@AlexWaygood AlexWaygood force-pushed the alex/moduletype-equivalence branch from 461daae to 0edf8be Compare July 10, 2025 08:55
@AlexWaygood AlexWaygood requested a review from MichaReiser July 10, 2025 08:59
Co-authored-by: Micha Reiser <micha@reiser.io>
@AlexWaygood AlexWaygood force-pushed the alex/moduletype-equivalence branch from d91c1b1 to a97b8dc Compare July 10, 2025 09:07
@AlexWaygood AlexWaygood enabled auto-merge (squash) July 10, 2025 09:08
@AlexWaygood AlexWaygood merged commit 934aaa2 into main Jul 10, 2025
36 checks passed
@AlexWaygood AlexWaygood deleted the alex/moduletype-equivalence branch July 10, 2025 09:11
UnboundVariable pushed a commit to UnboundVariable/ruff that referenced this pull request Jul 11, 2025
…re_help

* 'main' of https://github.com/astral-sh/ruff:
  Add simple integration tests for all output formats (astral-sh#19265)
  [`flake8-return`] Fix false-positive for variables used inside nested functions in `RET504`  (astral-sh#18433)
  [ty] Add a `--quiet` mode (astral-sh#19233)
  Treat form feed as valid whitespace before a line continuation (astral-sh#19220)
  [ty] Make `check_file` a salsa query (astral-sh#19255)
  [ty] Consolidate submodule resolving code between `types.rs` and `ide_support.rs` (astral-sh#19256)
  [ty] Remove countme from salsa-structs (astral-sh#19257)
  [ty] Improve and document equivalence for module-literal types (astral-sh#19243)
  [ty] Optimize protocol subtyping by removing expensive and unnecessary equivalence check from the top of `Type::has_relation_to()` (astral-sh#19230)
  [ty] Ecosystem analyzer: parallelize, fix race condition (astral-sh#19252)
  [ty] Add completion kind to playground (astral-sh#19251)
  [ty] Deploy ecosystem diff to Cloudflare pages (astral-sh#19234)
  [ty] Add semantic token provider to playground (astral-sh#19232)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ty Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect implementation of equivalence and subtyping for module-literal types?
2 participants