fix in operation on tuple of Literal doesn't narrow type #3474#3504
fix in operation on tuple of Literal doesn't narrow type #3474#3504asukaminato0721 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes a type-narrowing gap for membership checks (x in some_tuple) by narrowing the left-hand side to the tuple’s element type (including tuple[Literal[...], ...]), while intentionally skipping tuple shapes involving Any-like element types or unresolved variadic/unpack forms.
Changes:
- Add
tuple_membership_typehelper to derive a safe “membership element type” fromType::Tuple, with bail-outs forAny-like elements and unresolved variadics. - Extend
AtomicNarrowOp::Innarrowing to intersect the LHS with the RHS tuple’s derived membership type. - Add a regression test covering
str in tuple[Literal[...], ...]narrowing via aLiteralalias +get_argspattern.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| pyrefly/lib/alt/narrow.rs | Implements tuple-type-based narrowing for in checks via a new helper and integrates it into AtomicNarrowOp::In. |
| pyrefly/lib/test/narrow.rs | Adds a regression test asserting in over a tuple[Literal[...], ...] narrows a str to the Literal union alias. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Diff from mypy_primer, showing the effect of this PR on open source code: rotki (https://github.com/rotki/rotki)
+ ERROR rotkehlchen/api/services/transactions.py:144:24-45: Object of class `ChainManagerWithTransactions` has no attribute `node_inquirer` [missing-attribute]
- ERROR rotkehlchen/api/services/transactions.py:733:40-737:18: Argument `list[SupportedBlockchain]` is not assignable to parameter `iterable` with type `Iterable[CHAINS_WITH_TRANSACTION_DECODERS_TYPE]` in function `list.extend` [bad-argument-type]
mongo-python-driver (https://github.com/mongodb/mongo-python-driver)
+ ERROR pymongo/common.py:371:12-17: Returned type `Literal['nearest', 'primary', 'primaryPreferred', 'secondary', 'secondaryPreferred']` is not assignable to declared return type `_ServerMode` [bad-return]
sphinx (https://github.com/sphinx-doc/sphinx)
- ERROR sphinx/ext/imgmath.py:257:32-37: `depth` may be uninitialized [unbound-name]
- ERROR sphinx/ext/imgmath.py:282:28-33: `depth` may be uninitialized [unbound-name]
xarray (https://github.com/pydata/xarray)
- ERROR xarray/core/dataset.py:9175:37-61: Argument `Literal[0] | Mapping[Any, Mapping[Any, float | tuple[float, float]] | float | tuple[float, float]] | Mapping[Any, float | tuple[float, float]] | dict[Any, Any] | float | tuple[float, float] | Any | None` is not assignable to parameter `constant_values` with type `Mapping[Any, float | tuple[float, float]] | float | tuple[float, float] | None` in function `xarray.core.variable.Variable.pad` [bad-argument-type]
+ ERROR xarray/core/dataset.py:9175:37-61: Argument `Literal[0] | Mapping[Any, Mapping[Any, float | tuple[float, float]] | float | tuple[float, float]] | Mapping[Any, float | tuple[float, float]] | dict[Hashable, Any] | float | tuple[float, float] | Any | None` is not assignable to parameter `constant_values` with type `Mapping[Any, float | tuple[float, float]] | float | tuple[float, float] | None` in function `xarray.core.variable.Variable.pad` [bad-argument-type]
parso (https://github.com/davidhalter/parso)
+ ERROR parso/python/pep8.py:603:54-64: Object of class `str` has no attribute `value` [missing-attribute]
- ERROR parso/python/pep8.py:621:38-49: Object of class `NoneType` has no attribute `parent` [missing-attribute]
+ ERROR parso/python/pep8.py:621:38-49: Object of class `str` has no attribute `parent` [missing-attribute]
+ ERROR parso/python/pep8.py:666:62-72: Object of class `str` has no attribute `value` [missing-attribute]
pylint (https://github.com/pycqa/pylint)
- ERROR pylint/checkers/stdlib.py:703:71-85: Argument `str | Unknown | None` is not assignable to parameter `func_name` with type `str` in function `StdlibChecker._check_open_call` [bad-argument-type]
scrapy (https://github.com/scrapy/scrapy)
- ERROR scrapy/utils/deprecate.py:83:34-37: Argument `type[Any] | None` is not assignable to parameter `cls` with type `type[Any]` in function `_clspath` [bad-argument-type]
schema_salad (https://github.com/common-workflow-language/schema_salad)
- ERROR src/schema_salad/avro/schema.py:712:36-50: Argument `Any | None` is not assignable to parameter `atype` with type `str` in function `PrimitiveSchema.__init__` [bad-argument-type]
|
Primer Diff Classification❌ 1 regression(s) | ✅ 5 improvement(s) | ➖ 2 neutral | 8 project(s) total | +6, -8 errors 1 regression(s) across parso. error kinds:
Detailed analysis❌ Regression (1)parso (+3, -1)
✅ Improvement (5)mongo-python-driver (+1)
sphinx (-2)
pylint (-1)
scrapy (-1)
schema_salad (-1)
➖ Neutral (2)rotki (+1, -1)
xarray (+1, -1)
Was this helpful? React with 👍 or 👎 Classification by primer-classifier (1 heuristic, 7 LLM) |
Summary
Fixes #3474
x in some_tuple now can narrow x from the tuple’s element type, including
tuple[Literal[...], ...], while skipping tuple shapes with Any or unresolved variadics.Test Plan
add test