Skip to content

Preserve missing local definitions when other branches end in expressions - #5323

Closed
asukaminato0721 wants to merge 1 commit into
facebook:mainfrom
asukaminato0721:fix-local-missing-path-termination
Closed

asukaminato0721 wants to merge 1 commit into
facebook:mainfrom
asukaminato0721:fix-local-missing-path-termination

Conversation

@asukaminato0721

@asukaminato0721 asukaminato0721 commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

A trailing expression in a for body can hide an uninitialized local when the iterable is empty. For example, for x in xs: y = x; time.sleep(1) can leave y undefined, but the expression incorrectly supplies termination evidence for the zero-iteration path.

Only count termination keys from branches that lack the variable. This also reports skipped elif walrus assignments and preserves the definite-uninitialized status of deleted exception targets.

This changes the default-enabled unbound-name diagnostic. It is independent of the attribute initialization check in #4326.

  • I am a human, and I fully understand the code I am submitting.

Test Plan

  • 479 tests passed across scope, loops, branching, and returns.
  • Formatting and lint checks passed. Clippy reports existing warnings in unchanged code.

@meta-codesync

meta-codesync Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D126824523. (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: +53 new errors, -2 fixed errors
By preset: +53/-0 (default), +53/-0 (strict)

Projects with changes (24):
  psycopg: +6 -0
  apprise: +1 -0
  pycryptodome: +1 -0
  black: +1 -0
  openlibrary: +2 -1
  isort: +1 -0
  jax: +1 -0
  scikit-learn: +3 -0
  prefect: +1 -0
  scikit-build-core: +1 -0
  freqtrade: +2 -0
  manticore: +3 -0
  cloud-init: +4 -0
  zulip: +6 -0
  static-frame: +1 -0
  mypy: +1 -0
  pandas: +5 -0
  meson: +1 -0
  pytensor: +1 -0
  sphinx: +2 -0
  core: +6 -1
  spack: +1 -0
  xarray: +1 -0
  optuna: +1 -0
============================================================

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

psycopg (https://github.com/psycopg/psycopg)
+ ERROR tests/test_connection.py:804:40-45: `value` may be uninitialized [unbound-name]
+ ERROR tests/test_connection.py:819:40-45: `value` may be uninitialized [unbound-name]
+ ERROR tests/test_connection_async.py:819:40-45: `value` may be uninitialized [unbound-name]
+ ERROR tests/test_connection_async.py:836:40-45: `value` may be uninitialized [unbound-name]
+ ERROR tests/test_transaction.py:613:16-19: `tx3` may be uninitialized [unbound-name]
+ ERROR tests/test_transaction_async.py:620:16-19: `tx3` may be uninitialized [unbound-name]

apprise (https://github.com/caronc/apprise)
+ ERROR apprise/plugins/pushsafer.py:711:59-67: `filename` may be uninitialized [unbound-name]

pycryptodome (https://github.com/Legrandin/pycryptodome)
+ ERROR lib/Crypto/IO/_PBES.py:693:46-66: `hmac_hash_module_oid` may be uninitialized [unbound-name]

black (https://github.com/psf/black)
+ ERROR src/blib2to3/pgen2/driver.py:75:20-25: `token` may be uninitialized [unbound-name]

openlibrary (https://github.com/internetarchive/openlibrary)
- ERROR openlibrary/core/lending.py:205:16-57: The type of `data` is unknown because it comes from an error [unknown-from-error]
+ ERROR openlibrary/core/lending.py:205:16-24: `response` may be uninitialized [unbound-name]
+ ERROR openlibrary/coverstore/archive.py:408:32-36: `item` may be uninitialized [unbound-name]

isort (https://github.com/pycqa/isort)
+ ERROR isort/api.py:202:33-45: `file_content` may be uninitialized [unbound-name]

jax (https://github.com/google/jax)
+ ERROR jax/_src/lax/control_flow/conditionals.py:755:42-50: `inst_out` may be uninitialized [unbound-name]

scikit-learn (https://github.com/scikit-learn/scikit-learn)
+ ERROR sklearn/gaussian_process/kernels.py:1899:77-81: `base` may be uninitialized [unbound-name]
+ ERROR sklearn/metrics/_plot/precision_recall_curve.py:914:23-33: `pos_label_` may be uninitialized [unbound-name]
+ ERROR sklearn/metrics/_plot/roc_curve.py:735:23-33: `pos_label_` may be uninitialized [unbound-name]

prefect (https://github.com/PrefectHQ/prefect)
+ ERROR src/integrations/prefect-gcp/prefect_gcp/workers/vertex.py:664:16-23: `job_run` may be uninitialized [unbound-name]

scikit-build-core (https://github.com/scikit-build/scikit-build-core)
+ ERROR src/scikit_build_core/build/wheel.py:523:13-17: `path` may be uninitialized [unbound-name]

freqtrade (https://github.com/freqtrade/freqtrade)
+ ERROR freqtrade/exchange/binance_public_data.py:94:38-43: `start` may be uninitialized [unbound-name]
+ ERROR freqtrade/exchange/binance_public_data.py:94:61-64: `end` may be uninitialized [unbound-name]

manticore (https://github.com/trailofbits/manticore)
+ ERROR tests/auto_generators/make_VMTests.py:217:57-68: `blockheader` may be uninitialized [unbound-name]
+ ERROR tests/ethereum/test_general.py:1020:49-66: `symbolic_address1` may be uninitialized [unbound-name]
+ ERROR tests/ethereum/test_general.py:1021:49-66: `symbolic_address2` may be uninitialized [unbound-name]

cloud-init (https://github.com/canonical/cloud-init)
+ ERROR cloudinit/net/freebsd.py:43:51-61: `net_config` may be uninitialized [unbound-name]
+ ERROR tests/unittests/config/test_cc_ntp.py:542:47-50: `cfg` may be uninitialized [unbound-name]
+ ERROR tests/unittests/config/test_cc_ntp.py:561:51-54: `cfg` may be uninitialized [unbound-name]
+ ERROR tests/unittests/config/test_cc_ntp.py:579:51-54: `cfg` may be uninitialized [unbound-name]

zulip (https://github.com/zulip/zulip)
+ ERROR zerver/lib/cache.py:118:12-18: `prefix` may be uninitialized [unbound-name]
+ ERROR zerver/tests/test_markdown.py:2604:26-42: `rendering_result` may be uninitialized [unbound-name]
+ ERROR zerver/tests/test_push_notifications.py:1233:9-18: `time_sent` may be uninitialized [unbound-name]
+ ERROR zerver/tests/test_push_notifications.py:1245:13-25: `remote_realm` may be uninitialized [unbound-name]
+ ERROR zerver/tests/test_push_notifications.py:1282:9-21: `remote_realm` may be uninitialized [unbound-name]
+ ERROR zilencer/management/commands/render_messages.py:21:43-49: `msg_id` may be uninitialized [unbound-name]

static-frame (https://github.com/static-frame/static-frame)
+ ERROR static_frame/test/unit/test_container.py:15:13-14: `c` may be uninitialized [unbound-name]

mypy (https://github.com/python/mypy)
+ ERROR mypy/semanal_main.py:173:5-10: `state` may be uninitialized [unbound-name]

pandas (https://github.com/pandas-dev/pandas)
+ ERROR pandas/io/formats/csvs.py:315:30-38: `col_line` may be uninitialized [unbound-name]
+ ERROR pandas/io/formats/style_render.py:1013:28-42: `row_body_cells` may be uninitialized [unbound-name]
+ ERROR pandas/io/parsers/python_parser.py:1343:29-41: `len_new_rows` may be uninitialized [unbound-name]
+ ERROR pandas/io/pytables.py:4170:64-70: `_start` may be uninitialized [unbound-name]
+ ERROR pandas/io/pytables.py:4170:77-82: `_stop` may be uninitialized [unbound-name]

meson (https://github.com/mesonbuild/meson)
+ ERROR mesonbuild/interpreterbase/interpreterbase.py:743:27-38: `prev_subdir` may be uninitialized [unbound-name]

pytensor (https://github.com/pymc-devs/pytensor)
+ ERROR pytensor/d3viz/formatting.py:225:69-76: `vparams` may be uninitialized [unbound-name]

sphinx (https://github.com/sphinx-doc/sphinx)
+ ERROR sphinx/ext/autodoc/importer.py:286:38-46: `qualname` may be uninitialized [unbound-name]
+ ERROR sphinx/ext/autodoc/importer.py:298:30-38: `qualname` may be uninitialized [unbound-name]

core (https://github.com/home-assistant/core)
- ERROR homeassistant/components/aws_s3/__init__.py:69:9-22: The type of this argument is unknown because it comes from an error [unknown-from-error]
+ ERROR homeassistant/components/aws_s3/__init__.py:69:16-22: `client` may be uninitialized [unbound-name]
+ ERROR homeassistant/components/cloudflare_r2/__init__.py:72:26-32: `client` may be uninitialized [unbound-name]
+ ERROR homeassistant/components/knx/config_flow.py:506:36-44: `_host_ip` may be uninitialized [unbound-name]
+ ERROR homeassistant/components/knx/config_flow.py:525:26-31: `_host` may be uninitialized [unbound-name]
+ ERROR homeassistant/components/sql/config_flow.py:185:31-52: `db_url_for_validation` may be uninitialized [unbound-name]
+ ERROR homeassistant/helpers/intent.py:1228:35-42: `service` may be uninitialized [unbound-name]

spack (https://github.com/spack/spack)
+ ERROR lib/spack/spack/test/util/filesystem.py:357:32-38: `backup` may be uninitialized [unbound-name]

xarray (https://github.com/pydata/xarray)
+ ERROR xarray/structure/concat.py:920:47-51: `name` may be uninitialized [unbound-name]

optuna (https://github.com/optuna/optuna)
+ ERROR tests/samplers_tests/tpe_tests/test_sampler.py:395:59-64: `trial` may be uninitialized [unbound-name]

@connernilsen connernilsen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks for splitting this out!

@meta-codesync meta-codesync Bot closed this in f95c8d9 Oct 23, 2026
@meta-codesync meta-codesync Bot added the Merged label Oct 23, 2026
@meta-codesync

meta-codesync Bot commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in f95c8d9.

@asukaminato0721
asukaminato0721 deleted the fix-local-missing-path-termination branch October 23, 2026 16:53
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.

3 participants