Skip to content

fix Pyrefly loses track of "is not None" information for a variable and-ed in a return condition #4919 - #4983

Open
asukaminato0721 wants to merge 1 commit into
facebook:mainfrom
asukaminato0721:4919
Open

asukaminato0721 wants to merge 1 commit into
facebook:mainfrom
asukaminato0721:4919

Conversation

@asukaminato0721

Copy link
Copy Markdown
Collaborator

Summary

Fixes #4919

Saved boolean conditions now preserve narrowing through compound early-return guards.

Test Plan

add test

@meta-codesync

meta-codesync Bot commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D120731861. (Because this pull request was imported automatically, there will not be any future comments.)

@github-actions

Copy link
Copy Markdown

Diff from mypy_primer, showing the effect of this PR on open source code:

============================================================
SUMMARY
============================================================
Total: +21 new errors, -20 fixed errors
By preset: +8/-9 (default), +8/-9 (strict)

Projects with changes (11):
  cloud-init: +2 -0
  spark: +2 -1
  jax: +2 -5
  colour: +0 -4
  scikit-learn: +1 -2
  streamlit: +1 -1
  hydra-zen: +1 -0
  static-frame: +0 -1
  pandas: +5 -6
  xarray: +6 -0
  PyGithub: +1 -0
============================================================

FULL DIFF DETAILS
------------------------------------------------------------

cloud-init (https://github.com/canonical/cloud-init)
+ ERROR cloudinit/config/cc_write_files.py:154:17-20: The type of this argument is unknown [unknown-argument-type]
+ ERROR cloudinit/config/cc_write_files.py:165:17-20: The type of this argument is unknown [unknown-argument-type]

spark (https://github.com/apache/spark)
- ERROR python/pyspark/sql/connect/dataframe.py:2168:42-50: Implicit conversion of `bool | str | None` to `bool` is not allowed [implicit-bool]
+ ERROR python/pyspark/sql/connect/dataframe.py:2170:32-43: Redundant cast: `str` is the same type as `str` [redundant-cast]
+ ERROR python/pyspark/sql/connect/dataframe.py:2172:32-47: Redundant cast: `str` is the same type as `str` [redundant-cast]

jax (https://github.com/google/jax)
- ERROR jax/_src/pallas/mosaic_gpu/lowering.py:2588:37-46: The type of this argument is unknown [unknown-argument-type]
- ERROR jax/_src/pallas/mosaic_gpu/lowering.py:2601:35-44: The type of this argument is unknown [unknown-argument-type]
- ERROR jax/_src/pallas/mosaic_gpu/lowering.py:2602:35-44: The type of this argument is unknown [unknown-argument-type]
- ERROR jax/_src/state/primitives.py:927:20-57: The type of `out_bdim` is unknown; it is inferred as an implicit `Any` [unknown-variable-type]
+ ERROR jax/_src/state/primitives.py:927:20-57: `-` is not supported between `None` and `int` [unsupported-operation]
- ERROR jax/_src/state/primitives.py:956:45-52: The type of this argument is unknown [unknown-argument-type]
+ ERROR jax/_src/state/primitives.py:956:45-52: Argument `Unknown | None` is not assignable to parameter `dst` with type `Sequence[int] | int` in function `jax._src.util.moveaxis` [bad-argument-type]

colour (https://github.com/colour-science/colour)
- ERROR colour/quality/cqs.py:304:31-59: `MultiSpectralDistributions | SpectralDistribution` is not assignable to upper bound `MultiSpectralDistributions` of type variable `TypeMultiSpectralDistributions` [bad-specialization]
- ERROR colour/quality/cqs.py:311:29-57: `MultiSpectralDistributions | SpectralDistribution` is not assignable to upper bound `SpectralDistribution` of type variable `TypeSpectralDistribution` [bad-specialization]
- ERROR colour/quality/cri.py:254:31-59: `MultiSpectralDistributions | SpectralDistribution` is not assignable to upper bound `MultiSpectralDistributions` of type variable `TypeMultiSpectralDistributions` [bad-specialization]
- ERROR colour/quality/cri.py:261:29-57: `MultiSpectralDistributions | SpectralDistribution` is not assignable to upper bound `SpectralDistribution` of type variable `TypeSpectralDistribution` [bad-specialization]

scikit-learn (https://github.com/scikit-learn/scikit-learn)
- ERROR sklearn/neighbors/_nearest_centroid.py:252:29-43: The type of `dataset_centroid_` is unknown; it is inferred as an implicit `Any` [unknown-variable-type]
+ ERROR sklearn/neighbors/_nearest_centroid.py:252:29-35: Object of class `SparseABC` has no attribute `mean` [missing-attribute]
- ERROR sklearn/preprocessing/_data.py:2975:30-31: The type of this argument is unknown [unknown-argument-type]

streamlit (https://github.com/streamlit/streamlit)
- ERROR lib/streamlit/elements/widgets/slider.py:961:50-84: `Integral | Real | date | datetime | list[Integral | Real | date | datetime | list[Unknown] | time | tuple[Unknown, ...]] | list[Unknown] | time | tuple[Unknown, ...]` is not assignable to `Sequence[SliderScalar]` [bad-assignment]
+ ERROR lib/streamlit/elements/widgets/slider.py:961:50-84: `list[Integral | Real | date | datetime | time] | list[Unknown] | tuple[Unknown, ...]` is not assignable to `Sequence[SliderScalar]` [bad-assignment]

hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
+ ERROR src/hydra_zen/structured_configs/_implementations.py:2426:50-63: Unnecessary `bool()` call; argument is already of type `bool` [unnecessary-type-conversion]

static-frame (https://github.com/static-frame/static-frame)
- ERROR static_frame/core/util.py:1446:34-54: No matching overload found for function `numpy._core.multiarray.empty` called with arguments: (tuple[int, int | Unknown] | tuple[int, int] | None, dtype=dtype | None) [no-matching-overload]

pandas (https://github.com/pandas-dev/pandas)
+ ERROR pandas/core/apply.py:633:30-68: The type of `series` is unknown; it is inferred as an implicit `Any` [unknown-variable-type]
+ ERROR pandas/core/apply.py:634:44-50: The type of this argument is unknown [unknown-argument-type]
+ ERROR pandas/core/apply.py:638:34-71: The type of `series` is unknown; it is inferred as an implicit `Any` [unknown-variable-type]
+ ERROR pandas/core/apply.py:639:48-54: The type of this argument is unknown [unknown-argument-type]
- ERROR pandas/core/arrays/categorical.py:180:53-58: Argument `Hashable | Unknown` is not assignable to parameter `right` with type `ExtensionArray | Interval[Unknown] | NAType | Timedelta | Timestamp | bool | bytes | complex | complexfloating | date | datetime | datetime64 | float | floating | int | integer | list[Unknown] | ndarray | range | str | timedelta | timedelta64` in function `pandas.core.ops.invalid.invalid_comparison` [bad-argument-type]
+ ERROR pandas/core/arrays/categorical.py:180:53-58: Argument `Hashable` is not assignable to parameter `right` with type `ExtensionArray | Interval[Unknown] | NAType | Timedelta | Timestamp | bool | bytes | complex | complexfloating | date | datetime | datetime64 | float | floating | int | integer | list[Unknown] | ndarray | range | str | timedelta | timedelta64` in function `pandas.core.ops.invalid.invalid_comparison` [bad-argument-type]
- ERROR pandas/core/arrays/period.py:1554:20-30: Object of type `NaTType | Period | None` has no attribute `freq` [missing-attribute]
- ERROR pandas/core/arrays/period.py:1556:20-28: Object of type `NaTType | Period | None` has no attribute `freq` [missing-attribute]
- ERROR pandas/core/ops/missing.py:56:17-23: The type of `ymask` is unknown; it is inferred as an implicit `Any` [unknown-variable-type]
- ERROR pandas/tests/tools/test_to_datetime.py:4640:34-43: The type of this argument is unknown [unknown-argument-type]
- ERROR pandas/tests/tools/test_to_datetime.py:4640:57-66: The type of this argument is unknown [unknown-argument-type]

xarray (https://github.com/pydata/xarray)
+ ERROR xarray/coding/variables.py:299:77-79: The type of this argument is unknown [unknown-argument-type]
+ ERROR xarray/coding/variables.py:299:81-83: The type of this argument is unknown [unknown-argument-type]
+ ERROR xarray/coding/variables.py:308:76-78: The type of this argument is unknown [unknown-argument-type]
+ ERROR xarray/coding/variables.py:312:42-44: The type of this argument is unknown [unknown-argument-type]
+ ERROR xarray/coding/variables.py:322:21-329:22: The type of this argument is unknown [unknown-argument-type]
+ ERROR xarray/coding/variables.py:322:55-57: The type of this argument is unknown [unknown-argument-type]

PyGithub (https://github.com/PyGithub/PyGithub)
+ ERROR github/Repository.py:4115:91-92: Unused `# type: ignore` comment [unused-type-ignore]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pyrefly loses track of "is not None" information for a variable and-ed in a return condition

2 participants