Skip to content

Fix attribute writes after impossible narrowing - #4512

Open
cakeni wants to merge 2 commits into
facebook:mainfrom
cakeni:fix/attribute-write-narrowing
Open

Fix attribute writes after impossible narrowing#4512
cakeni wants to merge 2 commits into
facebook:mainfrom
cakeni:fix/attribute-write-narrowing

Conversation

@cakeni

@cakeni cakeni commented Aug 11, 2026

Copy link
Copy Markdown

Summary

Fix attribute writes in impossible narrowing branches to fall back to the receiver's unnarrowed declared path when the flow receiver has become Never. Normal receiver narrowing is preserved, and the assignment continues to invalidate stale attribute facets.

The regression test verifies both sides: a Container write is accepted and an incompatible Literal[1] write is still rejected against the declared Container type.

Fixes #4478

Test Plan

  • cargo +stable-x86_64-pc-windows-gnu test -p pyrefly --lib test_attr_assignment_uses_declared_type_after_impossible_narrow -- --nocapture
  • python test.py --no-test --no-tensor-shapes --no-conformance --no-jsonschema
  • cargo fmt --all -- --check
  • git diff --check

@meta-codesync

meta-codesync Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

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

@shobhitmehro shobhitmehro self-assigned this Aug 11, 2026
@github-actions

This comment has been minimized.

@shobhitmehro

Copy link
Copy Markdown
Contributor

Thanks for the PR! Could you review the mypy_primer diff above and confirm the new diagnostics are expected?

@shobhitmehro
shobhitmehro self-requested a review August 12, 2026 22:23
@github-actions github-actions Bot added size/m and removed size/s labels Aug 13, 2026
@github-actions

Copy link
Copy Markdown

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

============================================================
SUMMARY
============================================================
Total: +12 new errors, -46 fixed errors

Projects with changes (21):
  cloud-init: +1 -3
  dulwich: +0 -3
  steam.py: +0 -1
  bokeh: +0 -1
  spark: +0 -1
  prefect: +0 -1
  colour: +2 -1
  freqtrade: +0 -2
  setuptools: +0 -6
  apprise: +0 -1
  manticore: +0 -2
  beartype: +0 -1
  pandera: +0 -1
  ibis: +6 -6
  hydpy: +0 -1
  meson: +0 -5
  werkzeug: +0 -1
  core: +1 -1
  paasta: +0 -2
  sphinx: +1 -5
  poetry: +1 -1
============================================================

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

cloud-init (https://github.com/canonical/cloud-init)
- ERROR cloudinit/sources/DataSourceHetzner.py:204:36-49: `Literal['_unset']` is not assignable to attribute `_network_config` with type `Never` [bad-assignment]
- ERROR cloudinit/sources/DataSourceScaleway.py:320:36-49: `Literal['_unset']` is not assignable to attribute `_network_config` with type `Never` [bad-assignment]
- ERROR cloudinit/sources/DataSourceSmartOS.py:205:34-38: `None` is not assignable to attribute `md_client` with type `Never` [bad-assignment]
+ ERROR cloudinit/sources/DataSourceSmartOS.py:205:34-38: `None` is not assignable to attribute `md_client` with type `str` [bad-assignment]

dulwich (https://github.com/dulwich/dulwich)
- ERROR dulwich/repo.py:1677:42-1679:22: `ObjectFormat` is not assignable to attribute `object_format` with type `Never` [bad-assignment]
- ERROR dulwich/repo.py:1681:42-63: `ObjectFormat` is not assignable to attribute `object_format` with type `Never` [bad-assignment]
- ERROR dulwich/repo.py:1683:38-59: `ObjectFormat` is not assignable to attribute `object_format` with type `Never` [bad-assignment]

steam.py (https://github.com/Gobot1234/steam.py)
- ERROR steam/ext/commands/commands.py:248:49-60: `dict[str, Any]` is not assignable to attribute `__annotations__` with type `Never` [bad-assignment]

bokeh (https://github.com/bokeh/bokeh)
- ERROR src/bokeh/layouts.py:679:32-43: `Literal['fixed', 'inherit', 'scale_both', 'scale_height', 'scale_width', 'stretch_both', 'stretch_height', 'stretch_width']` is not assignable to attribute `sizing_mode` with type `Never` [bad-assignment]

spark (https://github.com/apache/spark)
- ERROR python/pyspark/ml/connect/pipeline.py:78:27-33: `list[Unknown]` is not assignable to attribute `stages` with type `Never` [bad-assignment]

prefect (https://github.com/PrefectHQ/prefect)
- ERROR src/integrations/prefect-gcp/prefect_gcp/workers/cloud_run_v2.py:302:30-38: `str` is not assignable to attribute `_job_name` with type `Never` [bad-assignment]

colour (https://github.com/colour-science/colour)
- ERROR colour/io/fichet2021.py:843:36-845:10: `list[Image_Specification_Attribute]` is not assignable to attribute `attributes` with type `Never` [bad-assignment]
+ ERROR colour/io/fichet2021.py:843:36-845:10: `list[Image_Specification_Attribute]` is not assignable to attribute `attributes` with type `tuple[Unknown, ...]` [bad-assignment]
+ ERROR colour/io/fichet2021.py:848:13-44: Object of class `tuple` has no attribute `extend` [missing-attribute]

freqtrade (https://github.com/freqtrade/freqtrade)
- ERROR freqtrade/freqtradebot.py:1933:46-94: `str` is not assignable to attribute `ft_cancel_reason` with type `Never` [bad-assignment]
- ERROR freqtrade/freqtradebot.py:2027:46-94: `str` is not assignable to attribute `ft_cancel_reason` with type `Never` [bad-assignment]

setuptools (https://github.com/pypa/setuptools)
- ERROR setuptools/_distutils/command/build_ext.py:198:33-69: `list[@_]` is not assignable to attribute `include_dirs` with type `Never` [bad-assignment]
- ERROR setuptools/_distutils/command/build_ext.py:219:30-32: `list[@_]` is not assignable to attribute `libraries` with type `Never` [bad-assignment]
- ERROR setuptools/_distutils/command/build_ext.py:221:33-35: `list[@_]` is not assignable to attribute `library_dirs` with type `Never` [bad-assignment]
- ERROR setuptools/_distutils/command/build_ext.py:226:26-28: `list[@_]` is not assignable to attribute `rpath` with type `Never` [bad-assignment]
- ERROR setuptools/_distutils/command/build_ext.py:293:30-32: `list[@_]` is not assignable to attribute `swig_opts` with type `Never` [bad-assignment]
- ERROR setuptools/_vendor/wheel/_bdist_wheel.py:272:30-63: `LiteralString` is not assignable to attribute `bdist_dir` with type `Never` [bad-assignment]

apprise (https://github.com/caronc/apprise)
- ERROR tests/test_config_http.py:311:26-54: `int` is not assignable to attribute `max_buffer_size` with type `Never` [bad-assignment]

manticore (https://github.com/trailofbits/manticore)
- ERROR tests/native/test_cpu_manual.py:195:18-24: `Literal[43690]` is not assignable to attribute `SI` with type `Never` [bad-assignment]
- ERROR tests/native/test_cpu_manual.py:472:18-22: `Literal[221]` is not assignable to attribute `CL` with type `Never` [bad-assignment]

beartype (https://github.com/beartype/beartype)
- ERROR beartype/_check/code/codemain.py:1469:40-1472:14: `str` is not assignable to attribute `func_curr_code` with type `Never` [bad-assignment]

pandera (https://github.com/pandera-dev/pandera)
- ERROR tests/base/test_config.py:49:35-70: `ValidationDepth | None` is not assignable to attribute `validation_depth` with type `Never` [bad-assignment]

ibis (https://github.com/ibis-project/ibis)
- ERROR ibis/common/tests/test_grounds.py:302:11-12: `Literal[3]` is not assignable to attribute `a` with type `Never` [bad-assignment]
+ ERROR ibis/common/tests/test_grounds.py:302:11-12: `Literal[3]` is not assignable to attribute `a` with type `InstanceOf` [bad-assignment]
- ERROR ibis/common/tests/test_grounds.py:701:15-16: `Literal[1]` is not assignable to attribute `a` with type `Never` [bad-assignment]
+ ERROR ibis/common/tests/test_grounds.py:701:15-16: `Literal[1]` is not assignable to attribute `a` with type `Attribute` [bad-assignment]
- ERROR ibis/common/tests/test_grounds.py:869:11-12: `Literal[2]` is not assignable to attribute `j` with type `Never` [bad-assignment]
+ ERROR ibis/common/tests/test_grounds.py:869:11-12: `Literal[2]` is not assignable to attribute `j` with type `Attribute` [bad-assignment]
- ERROR ibis/common/tests/test_grounds.py:882:11-12: `Literal[2]` is not assignable to attribute `j` with type `Never` [bad-assignment]
+ ERROR ibis/common/tests/test_grounds.py:882:11-12: `Literal[2]` is not assignable to attribute `j` with type `Attribute` [bad-assignment]
- ERROR ibis/common/tests/test_grounds.py:943:19-20: `Literal[3]` is not assignable to attribute `double_a` with type `Never` [bad-assignment]
+ ERROR ibis/common/tests/test_grounds.py:943:19-20: `Literal[3]` is not assignable to attribute `double_a` with type `Attribute` [bad-assignment]
- ERROR ibis/common/tests/test_grounds.py:1034:21-23: `Literal[11]` is not assignable to attribute `value` with type `Never` [bad-assignment]
+ ERROR ibis/common/tests/test_grounds.py:1034:21-23: `Literal[11]` is not assignable to attribute `value` with type `InstanceOf` [bad-assignment]

hydpy (https://github.com/hydpy-dev/hydpy)
- ERROR hydpy/core/importtools.py:821:35-50: `dict[str, Any]` is not assignable to attribute `_namespace` with type `Never` [bad-assignment]

meson (https://github.com/mesonbuild/meson)
- ERROR mesonbuild/build.py:2145:44-46: `list[@_]` is not assignable to attribute `extra_args` with type `Never` [bad-assignment]
- ERROR mesonbuild/build.py:2151:57-59: `list[@_]` is not assignable to attribute `extra_depends` with type `Never` [bad-assignment]
- ERROR mesonbuild/build.py:2750:27-33: `str` is not assignable to attribute `prefix` with type `Never` [bad-assignment]
- ERROR mesonbuild/build.py:2752:27-33: `str` is not assignable to attribute `suffix` with type `Never` [bad-assignment]
- ERROR mesonbuild/build.py:3620:27-76: `list[@_]` is not assignable to attribute `rename` with type `Never` [bad-assignment]

werkzeug (https://github.com/pallets/werkzeug)
- ERROR tests/test_utils.py:173:14-48: `datetime` is not assignable to attribute `date` with type `Never` [bad-assignment]

core (https://github.com/home-assistant/core)
- ERROR homeassistant/helpers/entity.py:1033:42-46: `Literal[True]` is not assignable to attribute `_no_platform_reported` with type `Never` [bad-assignment]
+ ERROR homeassistant/helpers/entity_platform.py:887:73-74: Unused `# type: ignore` comment [unused-type-ignore]

paasta (https://github.com/yelp/paasta)
- ERROR paasta_tools/paasta_service_config_loader.py:86:30-89: `list[str]` is not assignable to attribute `_clusters` with type `Never` [bad-assignment]
- ERROR paasta_tools/paasta_service_config_loader.py:146:38-148:14: `DeploymentsJsonV2` is not assignable to attribute `_deployments_json` with type `Never` [bad-assignment]

sphinx (https://github.com/sphinx-doc/sphinx)
- ERROR sphinx/directives/patches.py:54:32-44: `int` is not assignable to attribute `line` with type `Never` [bad-assignment]
- ERROR sphinx/environment/__init__.py:278:28-280:14: `_DomainsContainer` is not assignable to attribute `domains` with type `Never` [bad-assignment]
- ERROR sphinx/util/nodes.py:184:27-48: `str` is not assignable to attribute `source` with type `Never` [bad-assignment]
- ERROR sphinx/util/nodes.py:778:32-58: `str` is not assignable to attribute `source` with type `Never` [bad-assignment]
- ERROR sphinx/util/nodes.py:780:30-54: `str` is not assignable to attribute `line` with type `Never` [bad-assignment]
+ ERROR sphinx/util/nodes.py:780:30-54: `str` is not assignable to attribute `line` with type `int` [bad-assignment]

poetry (https://github.com/python-poetry/poetry)
- ERROR src/poetry/console/commands/init.py:548:26-42: `RepositoryPool` is not assignable to attribute `_pool` with type `Never` [bad-assignment]
+ ERROR src/poetry/console/commands/init.py:550:13-75: Result of call expression is of type `RepositoryPool` and is not used; assign to `_` if this is intentional [unused-call-result]

@cakeni

cakeni commented Aug 13, 2026

Copy link
Copy Markdown
Author

Thanks for the PR! Could you review the mypy_primer diff above and confirm the new diagnostics are expected?

I checked the latest mypy_primer artifact. It's now +12 new errors, -46 fixed errors; the older +14/-46 result is stale, and the bokeh and steam.py issues are gone.
I also went through the remaining 12 diagnostics. They all look expected, so I don't think any further changes are needed here.

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.

Writes to attributes should not use the narrowed type

2 participants