Skip to content

Conversation

carljm
Copy link
Contributor

@carljm carljm commented Jul 3, 2025

Summary

Fixes astral-sh/ty#741

The situation with shadowing types at runtime is a bit muddy. It's not a builtin module, and isn't explicitly non-shadowable. But in practice, it is typically imported very early in interpreter startup, and this means in most cases it practically can't be shadowed. Even in the scenarios where shadowing it is possible, this usually just causes interpreter startup to fail, due to trying to import things from it.

Since shadowing it doesn't really work at runtime, and also causes problems in type-checking (since we try to import things from it implicitly), the simplest approach is just to not support shadowing it.

Neither mypy nor pyright support shadowing it. Mypy has a nice diagnostic specifically informing you that you are trying to shadow it and that doesn't work; I added a TODO for us to consider adding this in future.

Test Plan

Added an mdtest which stack-overflowed before this PR.

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

github-actions bot commented Jul 3, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@carljm carljm force-pushed the cjm/noshadowtypes branch from db66736 to 3769072 Compare July 4, 2025 01:03
Copy link
Contributor

github-actions bot commented Jul 4, 2025

mypy_primer results

Changes were detected when running on open source projects
pytest-robotframework (https://github.com/detachhead/pytest-robotframework)
- TOTAL MEMORY USAGE: ~45MB
+ TOTAL MEMORY USAGE: ~49MB

mypy_primer (https://github.com/hauntsaninja/mypy_primer)
-     memo fields = ~41MB
+     memo fields = ~45MB

dulwich (https://github.com/dulwich/dulwich)
-     memo fields = ~129MB
+     memo fields = ~117MB

discord.py (https://github.com/Rapptz/discord.py)
- TOTAL MEMORY USAGE: ~251MB
+ TOTAL MEMORY USAGE: ~228MB

pydantic (https://github.com/pydantic/pydantic)
- TOTAL MEMORY USAGE: ~156MB
+ TOTAL MEMORY USAGE: ~142MB

isort (https://github.com/pycqa/isort)
- TOTAL MEMORY USAGE: ~80MB
+ TOTAL MEMORY USAGE: ~88MB

bandersnatch (https://github.com/pypa/bandersnatch)
-     memo fields = ~72MB
+     memo fields = ~66MB

psycopg (https://github.com/psycopg/psycopg)
- TOTAL MEMORY USAGE: ~228MB
+ TOTAL MEMORY USAGE: ~207MB

django-stubs (https://github.com/typeddjango/django-stubs)
- TOTAL MEMORY USAGE: ~189MB
+ TOTAL MEMORY USAGE: ~171MB

sphinx (https://github.com/sphinx-doc/sphinx)
- TOTAL MEMORY USAGE: ~304MB
+ TOTAL MEMORY USAGE: ~276MB

scipy (https://github.com/scipy/scipy)
- TOTAL MEMORY USAGE: ~1271MB
+ TOTAL MEMORY USAGE: ~1156MB

@carljm carljm marked this pull request as ready for review July 4, 2025 01:10
Copy link
Contributor

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vanquisher of stack overflows!

@AlexWaygood AlexWaygood added the bug Something isn't working label Jul 4, 2025
@AlexWaygood AlexWaygood enabled auto-merge (squash) July 4, 2025 10:34
@AlexWaygood AlexWaygood merged commit 7712c2f into main Jul 4, 2025
36 checks passed
@AlexWaygood AlexWaygood deleted the cjm/noshadowtypes branch July 4, 2025 10:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working ty Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Stack overflow with file called types.py
3 participants