Title: 1.16 changed behavior of redefined variable to None · Issue #19280 · python/mypy · GitHub
Open Graph Title: 1.16 changed behavior of redefined variable to None · Issue #19280 · python/mypy
X Title: 1.16 changed behavior of redefined variable to None · Issue #19280 · python/mypy
Description: https://github.com/mesonbuild/meson/blob/master/mesonbuild/cmake/fileapi.py This file started triggering: mesonbuild/cmake/fileapi.py:81:20: error: "None" has no attribute "__iter__" (not iterable) [attr-defined] mesonbuild/cmake/fileapi...
Open Graph Description: https://github.com/mesonbuild/meson/blob/master/mesonbuild/cmake/fileapi.py This file started triggering: mesonbuild/cmake/fileapi.py:81:20: error: "None" has no attribute "__iter__" (not iterable)...
X Description: https://github.com/mesonbuild/meson/blob/master/mesonbuild/cmake/fileapi.py This file started triggering: mesonbuild/cmake/fileapi.py:81:20: error: "None" has no attribute "__iter__&...
Opengraph URL: https://github.com/python/mypy/issues/19280
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"1.16 changed behavior of redefined variable to None","articleBody":"https://github.com/mesonbuild/meson/blob/master/mesonbuild/cmake/fileapi.py\n\nThis file started triggering:\n```\nmesonbuild/cmake/fileapi.py:81:20: error: \"None\" has no attribute \"__iter__\" (not iterable) [attr-defined]\nmesonbuild/cmake/fileapi.py:82:20: error: Value of type \"None\" is not indexable [index]\nmesonbuild/cmake/fileapi.py:84:36: error: Value of type \"None\" is not indexable [index]\n```\ndue to reuse of `for i in ......:`\n\nReduction:\n\n```python\nimport typing as T\nfrom pathlib import Path\n\nfor i in Path().iterdir():\n T.reveal_type(i)\n break\n\ndata = T.cast('T.Dict[str, T.Any]', {'foo': [{'kind': 1, 'b': 'two'}]})\nfor i in data['foo']:\n T.reveal_type(i)\n assert isinstance(i, dict)\n T.reveal_type(i)\n assert 'kind' in i\n```\n\nWith mypy 1.16.0\n```\n$ mypy --no-strict-optional --warn-unreachable fileapi.py \nfileapi.py:5: note: Revealed type is \"pathlib.Path\"\nfileapi.py:10: note: Revealed type is \"Any\"\nfileapi.py:12: note: Revealed type is \"None\"\nfileapi.py:13: error: \"None\" has no attribute \"__iter__\" (not iterable) [attr-defined]\n\n```\nIt starts off as Path, gets redefined as Any (which works?), but then asserting the Any is a dict results in it migrating to None. With mypy 1.15, it never redefined to Any at all, so it became unreachable:\n\n```\n$ pip install mypy==1.15.* \n$ mypy --no-strict-optional --warn-unreachable fileapi.py \nfileapi.py:5: note: Revealed type is \"pathlib.Path\"\nfileapi.py:10: note: Revealed type is \"pathlib.Path\"\nfileapi.py:11: error: Subclass of \"Path\" and \"dict[Any, Any]\" cannot exist: would have incompatible method signatures [unreachable]\nfileapi.py:12: error: Statement is unreachable [unreachable]\n```\n\nI can't really say either behavior is *useful*. I want variables to be block-scoped and reset their expected value. But at least the 1.15 behavior was comprehensible to me. The new behavior only works if I delete the assert, which seems terribly counterproductive.","author":{"url":"https://github.com/eli-schwartz","@type":"Person","name":"eli-schwartz"},"datePublished":"2025-06-12T00:54:59Z","url":"https://github.com/python/mypy/issues/19280"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:3bbc0ab2-4d56-1db1-e05a-917c9acaced0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9A44:245A38:27EA60:363A68:6B0024E9 |
| html-safe-nonce | 62c5fc76f3cce93ac396efef78cadcc8b52bbee167033179ee381abdf0e47ffa |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QTQ0OjI0NUEzODoyN0VBNjA6MzYzQTY4OjZCMDAyNEU5IiwidmlzaXRvcl9pZCI6IjQwMDAwMDA1MzE3Mjc2NTYxNjkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 5e0ffadca62c76a652fa07b15812dbc195945720f1f0d62877b47e5efce9df1b |
| hovercard-subject-tag | issue:3138453181 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/19280/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b23135499787f56e753e232a9c5357ed32fcc38b35890e780488c300537f18e8/python/mypy/issues/19280 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b23135499787f56e753e232a9c5357ed32fcc38b35890e780488c300537f18e8/python/mypy/issues/19280 |
| og:image:alt | https://github.com/mesonbuild/meson/blob/master/mesonbuild/cmake/fileapi.py This file started triggering: mesonbuild/cmake/fileapi.py:81:20: error: "None" has no attribute "__iter__" (not iterable)... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | eli-schwartz |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width