fix Doesn't allow list["A|B"] #3193#3195
fix Doesn't allow list["A|B"] #3193#3195asukaminato0721 wants to merge 4 commits intofacebook:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes pyrefly’s handling of generic-alias subscripts used in value context (e.g. list["A | B"]([x])) so that string type arguments are treated as forward references and parsed/bound as type expressions, matching other type checkers and resolving #3193.
Changes:
- Adds a binder special-case to treat certain
Expr::Subscriptnodes as containing type expressions even in value context. - Adds a regression test covering
list["Tomato | Cucumber"]([t])withfrom __future__ import annotations.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
pyrefly/lib/binding/expr.rs |
Ensures type-argument slices of certain special subscripts are bound as types in value context (enabling forward-ref parsing). |
pyrefly/lib/test/typeform.rs |
Adds regression test for forward-ref string type arguments in generic-alias value-context usage. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
thanks for the fix! seems targeted enough and good at first glance. assigning to @stroxler but feel free to reassign if you have too much on your plate |
|
@stroxler has imported this pull request. If you are a Meta employee, you can view this in D103326319. |
|
Diff from mypy_primer, showing the effect of this PR on open source code: tornado (https://github.com/tornadoweb/tornado)
- ERROR tornado/routing.py:296:9-15: Expected a type form, got instance of `Literal['Rule']` [not-a-type]
- ERROR tornado/routing.py:298:26-35: Expected a type form, got instance of `Literal['Matcher']` [not-a-type]
- ERROR tornado/routing.py:299:26-35: Expected a type form, got instance of `Literal['Matcher']` [not-a-type]
- ERROR tornado/routing.py:300:26-35: Expected a type form, got instance of `Literal['Matcher']` [not-a-type]
- ERROR tornado/routing.py:349:55-64: Argument `dict[str, Any] | str | Unknown` is not assignable to parameter `target_kwargs` with type `dict[str, Any] | None` in function `Rule.__init__` [bad-argument-type]
+ ERROR tornado/routing.py:349:55-64: Argument `dict[str, Any] | str | Any` is not assignable to parameter `target_kwargs` with type `dict[str, Any] | None` in function `Rule.__init__` [bad-argument-type]
- ERROR tornado/routing.py:349:55-64: Argument `dict[str, Any] | str | Unknown` is not assignable to parameter `name` with type `str | None` in function `Rule.__init__` [bad-argument-type]
+ ERROR tornado/routing.py:349:55-64: Argument `dict[str, Any] | str | Any` is not assignable to parameter `name` with type `str | None` in function `Rule.__init__` [bad-argument-type]
- ERROR tornado/routing.py:351:33-38: Argument `dict[str, Any] | str | Unknown` is not assignable to parameter `matcher` with type `Matcher` in function `Rule.__init__` [bad-argument-type]
+ ERROR tornado/routing.py:351:33-38: Argument `Matcher | dict[str, Any] | str | Any` is not assignable to parameter `matcher` with type `Matcher` in function `Rule.__init__` [bad-argument-type]
- ERROR tornado/routing.py:351:33-38: Argument `dict[str, Any] | str | Unknown` is not assignable to parameter `target_kwargs` with type `dict[str, Any] | None` in function `Rule.__init__` [bad-argument-type]
+ ERROR tornado/routing.py:351:33-38: Argument `Matcher | dict[str, Any] | str | Any` is not assignable to parameter `target_kwargs` with type `dict[str, Any] | None` in function `Rule.__init__` [bad-argument-type]
- ERROR tornado/routing.py:351:33-38: Argument `dict[str, Any] | str | Unknown` is not assignable to parameter `name` with type `str | None` in function `Rule.__init__` [bad-argument-type]
+ ERROR tornado/routing.py:351:33-38: Argument `Matcher | dict[str, Any] | str | Any` is not assignable to parameter `name` with type `str | None` in function `Rule.__init__` [bad-argument-type]
- ERROR tornado/routing.py:353:49-53: Argument `Rule | tuple[str | Unknown, Any] | tuple[str | Unknown, Any, dict[str, Any]] | tuple[str | Unknown, Any, dict[str, Any], str] | Unknown` is not assignable to parameter `rule` with type `Rule` in function `RuleRouter.process_rule` [bad-argument-type]
altair (https://github.com/vega/altair)
- ERROR altair/datasets/_cache.py:153:61-71: Expected a type form, got instance of `Literal['_Dataset']` [not-a-type]
- ERROR altair/datasets/_cache.py:153:73-83: Expected a type form, got instance of `Literal['Metadata']` [not-a-type]
- ERROR altair/datasets/_cache.py:225:62-72: Expected a type form, got instance of `Literal['_Dataset']` [not-a-type]
- ERROR altair/datasets/_cache.py:225:74-85: Expected a type form, got instance of `Literal['_FlSchema']` [not-a-type]
discord.py (https://github.com/Rapptz/discord.py)
- ERROR discord/app_commands/translator.py:119:61-85: Expected a type form, got instance of `Literal['Command[Any, ..., Any]']` [not-a-type]
- ERROR discord/app_commands/translator.py:119:87-100: Expected a type form, got instance of `Literal['ContextMenu']` [not-a-type]
django-test-migrations (https://github.com/wemake-services/django-test-migrations)
- ERROR django_test_migrations/db/backends/registry.py:16:10-37: Expected a type form, got instance of `Literal['BaseDatabaseConfiguration']` [not-a-type]
prefect (https://github.com/PrefectHQ/prefect)
- ERROR src/prefect/deployments/schedules.py:14:53-69: Expected a type form, got instance of `Literal['SCHEDULE_TYPES']` [not-a-type]
- ERROR src/prefect/server/database/orm_models.py:1510:38-54: Expected a type form, got instance of `Literal['sa.Column[Any]']` [not-a-type]
|
Primer Diff Classification✅ 5 improvement(s) | 5 project(s) total | +2, -16 errors 5 improvement(s) across tornado, altair, discord.py, django-test-migrations, prefect.
Detailed analysis✅ Improvement (5)tornado (+2, -7)
altair (-4)
discord.py (-2)
django-test-migrations (-1)
prefect (-2)
Was this helpful? React with 👍 or 👎 Classification by primer-classifier (5 LLM) |
stroxler
left a comment
There was a problem hiding this comment.
LGTM, thanks! I'll try to get this merged
Summary
Fixes #3193
binding type-argument subscripts as type expressions even in value context, so
list["Tomato | Cucumber"]([t])no longer errors and forward-ref strings get parsed.Test Plan
add test