Title: "if condition is always false [redundant-expr]" false positive when variable set in a loop after the conditional · Issue #14993 · python/mypy · GitHub
Open Graph Title: "if condition is always false [redundant-expr]" false positive when variable set in a loop after the conditional · Issue #14993 · python/mypy
X Title: "if condition is always false [redundant-expr]" false positive when variable set in a loop after the conditional · Issue #14993 · python/mypy
Description: Bug Report The redundant-expr gives a false positive if you conditionally read a variable for another conditional inside a loop. To Reproduce def mypytest() -> int | None: trigger_segment = None violations = None for segment in [1, 2]: i...
Open Graph Description: Bug Report The redundant-expr gives a false positive if you conditionally read a variable for another conditional inside a loop. To Reproduce def mypytest() -> int | None: trigger_segment = None vi...
X Description: Bug Report The redundant-expr gives a false positive if you conditionally read a variable for another conditional inside a loop. To Reproduce def mypytest() -> int | None: trigger_segment = None...
Opengraph URL: https://github.com/python/mypy/issues/14993
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"\"if condition is always false [redundant-expr]\" false positive when variable set in a loop after the conditional","articleBody":"\u003c!--\r\nIf you're not sure whether what you're experiencing is a mypy bug, please see the \"Question and Help\" form instead.\r\nPlease consider:\r\n\r\n- checking our common issues page: https://mypy.readthedocs.io/en/stable/common_issues.html\r\n- searching our issue tracker: https://github.com/python/mypy/issues to see if it's already been reported\r\n- asking on gitter chat: https://gitter.im/python/typing\r\n--\u003e\r\n\r\n**Bug Report**\r\n\r\n\u003c!--\r\nIf you're reporting a problem with a specific library function, the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues\r\n\r\nIf the project you encountered the issue in is open source, please provide a link to the project.\r\n--\u003e\r\n\r\nThe `redundant-expr` gives a false positive if you conditionally read a variable for another conditional inside a loop.\r\n\r\n**To Reproduce**\r\n\r\n```python\r\ndef mypytest() -\u003e int | None:\r\n trigger_segment = None\r\n violations = None\r\n for segment in [1, 2]:\r\n if segment == 2:\r\n violations = trigger_segment if trigger_segment else segment\r\n trigger_segment = segment\r\n return violations\r\n```\r\n\r\nThe rule will trigger with `error: If condition is always false [redundant-expr]` on the `if trigger_segment` part of the ternary.\r\n\r\n**Expected Behavior**\r\n\r\nMypy should not report anything because clearly the if condition is not always false: running `print(mypytest())` will print `1`.\r\n\r\n\u003c!--\r\nHow did you expect mypy to behave? It’s fine if you’re not sure your understanding is correct.\r\nWrite down what you thought would happen. If you expected no errors, delete this section.\r\n--\u003e\r\n\r\n**Actual Behavior**\r\n\r\nThe rule will trigger with `error: If condition is always false [redundant-expr]` on the `if trigger_segment` part of the ternary.\r\n\r\n\u003c!-- What went wrong? Paste mypy's output. --\u003e\r\n\r\n**Your Environment**\r\n\r\n\r\nMy configuration:\r\n```\r\n\r\n[tool.mypy]\r\nplugins = [\r\n \"pydantic.mypy\"\r\n]\r\nfiles = \"src/**/*.py,tests/**/*.py\"\r\nstrict = true\r\n# These are additional checks not covered by strict:\r\ndisallow_any_unimported = true\r\nenable_error_code = \"\"\"\\\r\n ignore-without-code,\\\r\n truthy-bool,\\\r\n truthy-iterable,\\\r\n redundant-expr,\\\r\n redundant-self,\\\r\n unused-awaitable\\\r\n \"\"\"\r\nwarn_unreachable = true\r\n```\r\n```% mypy --version\r\nmypy 1.1.1 (compiled: yes)\r\n% python --version\r\nPython 3.11.1\r\n```","author":{"url":"https://github.com/james-johnston-thumbtack","@type":"Person","name":"james-johnston-thumbtack"},"datePublished":"2023-04-01T15:24:17.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/14993/mypy/issues/14993"}
| 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:dc8ba5ff-1961-8026-78eb-14c9f03002d6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B2D2:3F51C2:AC8BC7:E43C02:6A5B74CF |
| html-safe-nonce | 3d02636b8386c01f0e6e1f5da4aa49115bb17907d7e2440a1f922510fe737185 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMkQyOjNGNTFDMjpBQzhCQzc6RTQzQzAyOjZBNUI3NENGIiwidmlzaXRvcl9pZCI6IjMzMDY1NzQ2NzMwNDg0NjY2NDAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 3981a52f9c50081882805db50d25b902dbd84fff77ead48171ae73324497bd66 |
| hovercard-subject-tag | issue:1650522556 |
| 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/14993/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f3220d10ad1fc2901f609c100b5a520e3df30a08c303d3161bbf9d6161826183/python/mypy/issues/14993 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f3220d10ad1fc2901f609c100b5a520e3df30a08c303d3161bbf9d6161826183/python/mypy/issues/14993 |
| og:image:alt | Bug Report The redundant-expr gives a false positive if you conditionally read a variable for another conditional inside a loop. To Reproduce def mypytest() -> int | None: trigger_segment = None vi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | james-johnston-thumbtack |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| 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 | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width