Fix negative narrowing for isinstance tuple targets
Fixes #3412.
This fixes negative narrowing for isinstance checks where the target is a tuple of runtime classes, such as isinstance(x, (str, bytes)).
Summary
- For
isinstance(x, (A, B)),narrow_is_not_instancepreviously subtracted each tuple element from the full input type independently and then intersected the per-element results. - When the input contained a member that overlapped with multiple tuple elements (e.g.
Iterable[Any]is a supertype of bothstrandbytes), the intersection step would reintroduce the subtracted alternatives through subset-based intersection. Concretely,intersect_impl(Iterable[Any], str)returnsstrbecausestr <: Iterable[Any]. - The fix walks the input union once and subtracts every tuple element from each union member sequentially, so the per-element scope doesn't reintroduce the overlap. Targets that don't allow negative narrowing (e.g. a non-final
type[C]variable) are skipped, preserving existing conservative behavior.
Repro
from typing import reveal_type, Any, Iterable
def arrlen(obj: Any) -> int | None: ...
def main(a: float | str | bytes | Iterable[Any]) -> None:
if isinstance(a, (str, bytes)):
pass
elif arrlen(a) is not None:
reveal_type(a)
Before: Iterable[Any] | bytes | float | str
After: Iterable[Any] | float
Test plan
- [x] Added
test_isinstance_tuple_negative_with_overlapinpyrefly/lib/test/narrow.rs. - [x]
cargo test -p pyrefly --lib— 5152 passed, 0 failed. - [x] All 40
test_isinstance_*tests continue to pass, including the existingtest_isinstance_type_negative_partial_narrowwhich exercises the not-finaltype[C]skip path. - [x] Verified the repro file against the locally built
target/debug/pyrefly.
Diff from mypy_primer, showing the effect of this PR on open source code:
bokeh (https://github.com/bokeh/bokeh)
- ERROR src/bokeh/plotting/contour.py:308:31-36: Argument `Sequence[ColorLike] | str | tuple[int, int, int] | tuple[int, int, int, float] | tuple[str, ...]` is not assignable to parameter `palette` with type `tuple[str, ...]` in function `bokeh.palettes.interp_palette` [bad-argument-type]
+ ERROR src/bokeh/plotting/contour.py:308:31-36: Argument `Sequence[ColorLike] | tuple[int, int, int] | tuple[int, int, int, float] | tuple[str, ...]` is not assignable to parameter `palette` with type `tuple[str, ...]` in function `bokeh.palettes.interp_palette` [bad-argument-type]
streamlit (https://github.com/streamlit/streamlit)
- ERROR lib/streamlit/elements/media.py:488:28-40: Object of class `RawIOBase` has no attribute `tobytes`
- Object of class `str` has no attribute `tobytes` [missing-attribute]
pandera (https://github.com/pandera-dev/pandera)
- ERROR pandera/engines/geopandas_engine.py:151:29-58: Cannot index into `Series` [bad-index]
ibis (https://github.com/ibis-project/ibis)
- ERROR ibis/backends/polars/__init__.py:298:54-58: Argument `Iterable[Path | str] | str` is not assignable to parameter `obj` with type `Sized` in function `len` [bad-argument-type]
+ ERROR ibis/backends/polars/__init__.py:298:54-58: Argument `Iterable[Path | str]` is not assignable to parameter `obj` with type `Sized` in function `len` [bad-argument-type]
- ERROR ibis/backends/polars/__init__.py:301:54-58: Argument `Iterable[Path | str] | str | Unknown` is not assignable to parameter `obj` with type `Sized` in function `len` [bad-argument-type]
+ ERROR ibis/backends/polars/__init__.py:301:54-58: Argument `Iterable[Path | str] | Unknown` is not assignable to parameter `obj` with type `Sized` in function `len` [bad-argument-type]
strawberry (https://github.com/strawberry-graphql/strawberry)
- ERROR strawberry/extensions/mask_errors.py:61:34-55: Object of class `ExecutionResult` has no attribute `initial_result` [missing-attribute]
static-frame (https://github.com/static-frame/static-frame)
- ERROR static_frame/core/util.py:2491:63-66: Type `bool` is not iterable [not-iterable]
- ERROR static_frame/core/util.py:2491:63-66: Type `date` is not iterable [not-iterable]
- ERROR static_frame/core/util.py:2491:63-66: Type `int` is not iterable [not-iterable]
- ERROR static_frame/core/util.py:2491:63-66: Type `integer` is not iterable [not-iterable]
werkzeug (https://github.com/pallets/werkzeug)
- ERROR src/werkzeug/datastructures/structures.py:42:23-33: Yielded type `tuple[tuple[K, V] | K, list[V] | tuple[V, ...] | V | Unknown]` is not assignable to declared yield type `tuple[K, V]` [invalid-yield]
+ ERROR src/werkzeug/datastructures/structures.py:42:23-33: Yielded type `tuple[tuple[K, V] | K, V | Unknown]` is not assignable to declared yield type `tuple[K, V]` [invalid-yield]
kornia (https://github.com/kornia/kornia)
- ERROR kornia/models/depth_estimation/base.py:62:35-45: Object of class `list` has no attribute `cpu` [missing-attribute]
- ERROR kornia/models/depth_estimation/base.py:63:46-59: Object of class `list` has no attribute `device` [missing-attribute]
- ERROR kornia/models/depth_estimation/base.py:63:67-79: Object of class `list` has no attribute `dtype` [missing-attribute]
- ERROR kornia/models/segmentation/base.py:183:40-59: Object of class `list` has no attribute `size` [missing-attribute]
scrapy (https://github.com/scrapy/scrapy)
- ERROR scrapy/http/headers.py:55:22-27: `Iterable[_RawValue] | bytes | int | str` is not assignable to variable `_value` with type `Iterable[_RawValue]` [bad-assignment]
+ ERROR scrapy/http/headers.py:55:22-27: `Iterable[_RawValue] | int` is not assignable to variable `_value` with type `Iterable[_RawValue]` [bad-assignment]
- ERROR scrapy/http/headers.py:57:22-29: `list[Iterable[_RawValue] | bytes | int | str]` is not assignable to variable `_value` with type `Iterable[_RawValue]` [bad-assignment]
+ ERROR scrapy/http/headers.py:57:22-29: `list[Iterable[_RawValue] | int]` is not assignable to variable `_value` with type `Iterable[_RawValue]` [bad-assignment]
spark (https://github.com/apache/spark)
- ERROR python/pyspark/sql/pandas/conversion.py:1028:59-70: Object of class `list` has no attribute `json` [missing-attribute]
- ERROR python/pyspark/testing/pandasutils.py:464:55-70: Object of class `Index` has no attribute `to_pandas` [missing-attribute]
Primer Diff Classification
✅ 5 improvement(s) | ➖ 4 neutral | 9 project(s) total | +6, -15 errors
5 improvement(s) across pandera, strawberry, static-frame, kornia, spark.
| Project | Verdict | Changes | Error Kinds | Root Cause |
|---|---|---|---|---|
| bokeh | ➖ Neutral | +1, -1 | bad-argument-type |
|
| pandera | ✅ Improvement | -1 | bad-index |
pyrefly/lib/alt/narrow.rs |
| ibis | ➖ Neutral | +2, -2 | bad-argument-type |
|
| strawberry | ✅ Improvement | -1 | missing-attribute |
pyrefly/lib/alt/narrow.rs |
| static-frame | ✅ Improvement | -1 | not-iterable |
narrow_is_not_instance() |
| werkzeug | ➖ Neutral | +1, -1 | invalid-yield |
|
| kornia | ✅ Improvement | -4 | missing-attribute |
pyrefly/lib/alt/narrow.rs |
| scrapy | ➖ Neutral | +2, -2 | bad-assignment |
|
| spark | ✅ Improvement | -2 | missing-attribute |
pyrefly/lib/alt/narrow.rs |
Detailed analysis
✅ Improvement (5)
pandera (-1)
This is a clear improvement. The PR fixes a bug in negative isinstance narrowing for tuple targets. The
isinstance(coerced_data, (GeometryArray, pd.DataFrame))check on line 142 should narrowcoerced_datain the else branch to exclude bothGeometryArrayandpd.DataFrame. Before the fix, the intersection-based approach could reintroduce one of these types, leading to a falsebad-indexerror on line 151. The error message specifically states 'Cannot index intoSeries' - this refers topd.Series, which is one of the types in theGeoPandasObjectunion. After the isinstance check on line 142 filters outGeometryArrayandpd.DataFrame, the remaining types in the else branch should bepd.Series | gpd.GeoSeries | gpd.GeoDataFrame. Thebad-indexerror onpd.Seriessuggests that the type checker's stubs forpd.Series.__getitem__don't accept the boolean Series/array being used as an index. With the fix, the sequential subtraction correctly removes bothGeometryArrayandpd.DataFramefrom the type, and the type checker no longer reports the false positive - likely because the remaining types' indexing signatures are properly recognized, or because the narrowing now works correctly to determine the actual types that reach line 151. Attribution: The change tonarrow_is_not_instanceinpyrefly/lib/alt/narrow.rsfixed the sequential subtraction of isinstance tuple targets. Previously, when checkingisinstance(coerced_data, (GeometryArray, pd.DataFrame)), the negative branch (else/after) would incorrectly reintroduce types that overlap with multiple tuple elements due to the intersection-based approach. The new sequential subtraction correctly removes bothGeometryArrayandpd.DataFramefrom the type ofcoerced_datain the negative branch, which means at line 151 the type is properly narrowed to types that support boolean indexing, eliminating the falsebad-indexerror.
strawberry (-1)
This is a clear improvement. The PR fixes a bug in negative narrowing for
isinstancechecks with tuple targets. Inmask_errors.py, line 58 checksisinstance(result, (GraphQLExecutionResult, StrawberryExecutionResult)). The else branch (line 60-61) should haveresultnarrowed to exclude those two types. Before the fix, the intersection-based approach innarrow_is_not_instancewas reintroducing types that overlapped with multiple tuple elements, causingresultin the else branch to still includeExecutionResulttypes that lackinitial_result. The fix correctly subtracts each tuple element sequentially from each union member, so the else branch now properly excludes the isinstance-matched types, and theinitial_resultattribute access is valid on the remaining type. Attribution: The change tonarrow_is_not_instancelogic inpyrefly/lib/alt/narrow.rsfixed the sequential subtraction of tuple elements in isinstance checks. Previously, the code computed per-target results and intersected them, which reintroduced union members that overlapped with multiple tuple elements. The fix walks each union member once and subtracts every tuple element sequentially. This directly fixed the narrowing in the else branch ofisinstance(result, (GraphQLExecutionResult, StrawberryExecutionResult))on line 58 ofmask_errors.py, which eliminated the falsemissing-attributeerror on line 61.
static-frame (-1)
This is a clear improvement. The PR fixes a bug in negative isinstance narrowing for tuple targets. The removed error was a false positive —
boolwas incorrectly remaining in the type union at line 2491 because the old intersection-based approach forisinstance(key, (str, bytes))andisinstance(key, INT_TYPES)negative branches was reintroducing subtracted types. With the fix, sequential subtraction correctly narrows the type, andbool(a subclass ofint) is properly removed by theisinstance(key, INT_TYPES)check's negative branch. Attribution: The change tonarrow_is_not_instance()inpyrefly/lib/alt/narrow.rsswitched from computing per-target subtraction results and intersecting them, to sequential per-element subtraction from each union member. This fixed the reintroduction of overlapping types (likeboolbeing a subclass ofintwhich overlaps with tuple elements), correctly removingboolfrom the type union before line 2491.
kornia (-4)
These were clear false positives caused by a bug in pyrefly's negative isinstance narrowing for tuple targets. The parameter
images: Union[torch.Tensor, list[torch.Tensor]]is checked withisinstance(images, (list, tuple))on lines 48-53. In the else branch (lines 62-64),imagesmust betorch.Tensor. The old code failed to subtractlistproperly when the isinstance target was a tuple, leavinglist[torch.Tensor]in the narrowed type. The same pattern applies tokornia/models/segmentation/base.py:183where.size()was incorrectly flagged onlist. The PR fix correctly handles sequential subtraction of tuple elements, resolving all four false positives. Attribution: The fix inpyrefly/lib/alt/narrow.rsin thenarrow_is_not_instancelogic changed from computing per-target subtraction results and intersecting them, to sequentially subtracting each tuple element from each union member. Previously,intersect_impl(list[torch.Tensor], list)would reintroducelist[torch.Tensor]through subset-based intersection when the isinstance target was a tuple(list, tuple). The sequential subtraction approach correctly removes bothlistandtuplefrom the union, leaving onlytorch.Tensor.
spark (-2)
Both errors were false positives caused by a bug in pyrefly's isinstance negative narrowing for tuple targets. When
isinstance(schema, (list, tuple))was in the negative branch, the old code failed to fully subtractlistfromUnion[StructType, List[str]], leavinglistin the type at line 1028. Similarly,isinstance(right, (pd.DataFrame, pd.Index, pd.Series))in the negative branch failed to subtract all three pandas types, leavingpd.Index(which lacksto_pandas()) in the type at line 464. The PR's fix to sequential subtraction correctly resolves both cases. Attribution: The change tonarrow_is_not_instanceinpyrefly/lib/alt/narrow.rsreplaced the per-target-intersect approach with sequential per-element subtraction. This fixed the negative narrowing forisinstance(x, (A, B, ...))tuple targets, which previously reintroduced subtracted types through subset-based intersection. This directly eliminated both false positive missing-attribute errors.
➖ Neutral (4)
bokeh (+1, -1)
Same errors at same locations with same error kinds — message wording changed, no behavioral impact.
ibis (+2, -2)
Same errors at same locations with same error kinds — message wording changed, no behavioral impact.
werkzeug (+1, -1)
Same errors at same locations with same error kinds — message wording changed, no behavioral impact.
scrapy (+2, -2)
Same errors at same locations with same error kinds — message wording changed, no behavioral impact.
Was this helpful? React with 👍 or 👎
Classification by primer-classifier (4 heuristic, 5 LLM)
@yangdanny97 has imported this pull request. If you are a Meta employee, you can view this in D105741367.
@yangdanny97 merged this pull request in facebook/pyrefly@92413ec83a599d50cd2aa1dfd8e94a22ba8a56a9.