Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[red-knot] Allow type[] to be subscripted #13667

Merged
merged 1 commit into from
Oct 7, 2024

Conversation

AlexWaygood
Copy link
Member

Fixed a TODO by adding another TODO. It's the red-knot way!

Summary

builtins.type can be subscripted at runtime on Python 3.9+, even though it has no __class_getitem__ method and its metaclass (which is... itself) has no __getitem__ method. The special case is hardcoded directly into PyObject_GetItem in CPython. We just have to replicate the special case in our semantic model.

This will fail at runtime on Python <3.9. However, there's a bunch of outstanding questions (detailed in the TODO comment I added) regarding how we deal with subscriptions of other generic types on lower Python versions. Since we want to avoid too many false positives for now, I haven't tried to address this; I've just made type subscriptable on all Python versions.

Test Plan

cargo test -p red_knot_python_semantic --lib

Copy link
Contributor

github-actions bot commented Oct 7, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@AlexWaygood AlexWaygood merged commit 71b52b8 into main Oct 7, 2024
20 checks passed
@AlexWaygood AlexWaygood deleted the alex/redknot-type-subscription branch October 7, 2024 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
red-knot Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants