| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
When narrowing against a bool literal, `try_expanding_sum_type_to_union` expanded `bool` into `Literal[True] | Literal[False]` even if the type already had a `last_known_value`. For `match False:` this made `case True:` look matchable and left `Literal[False]` as the remaining type after `case False:`, so the match was not treated as exhaustive. Use the last known value instead of expanding in that case. Fixes python#22202.
|
Diff from mypy_primer, showing the effect of this PR on open source code: mypy (https://github.com/python/mypy)
+ mypy/server/mergecheck.py:59: error: Right operand of "and" is never evaluated [unreachable]
+ mypy/server/mergecheck.py:61: error: Statement is unreachable [unreachable]
+ mypy/server/mergecheck.py:61: note: See https://mypy.rtfd.io/en/stable/_refs.html#code-unreachable for more info
+ mypyc/codegen/emit.py:1436: error: Statement is unreachable [unreachable]
+ mypyc/codegen/emitfunc.py:1098: error: Statement is unreachable [unreachable]
+ mypyc/analysis/attrdefined.py:176: error: Statement is unreachable [unreachable]
+ mypy/semanal.py:8234: error: Right operand of "and" is never evaluated [unreachable]
+ mypy/semanal.py:8235: error: Statement is unreachable [unreachable]
+ mypy/build.py:1336: error: Statement is unreachable [unreachable]
+ mypy/main.py:180: error: Statement is unreachable [unreachable]
+ mypy/dmypy_server.py:543: error: Statement is unreachable [unreachable]
schema_salad (https://github.com/common-workflow-language/schema_salad)
+ src/schema_salad/java_codegen.py: note: In member "type_loader" of class "JavaCodeGen":
+ src/schema_salad/java_codegen.py:402:25: error: Statement is unreachable [unreachable]
+ src/schema_salad/java_codegen.py:535:25: error: Statement is unreachable [unreachable]
|
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Fixes #22202
A match with a bool literal subject (match False:) wasn't exhaustive under case True: / case False:. The subject's type is bool with last_known_value=Literal[False]. The singleton pattern narrows through conditional_types, which calls try_expanding_sum_type_to_union. That function expanded the type to Literal[True] | Literal[False] and threw away the known value. So case True: looked reachable and Literal[False] was left over after case False:.
With this change, try_expanding_sum_type_to_union returns the last_known_value for a bool instance that has one. It only expands to both literals when the value is unknown. Int and str literal subjects (match 1: / case 1:) were already exhaustive, because value patterns go through narrow_type_by_identity_equality, which coerces to literals.
As with match 1: case 2: today, case True: under match False: is now reported as unreachable with --warn-unreachable.
Test: testMatchBoolLiteralSubjectExhaustive in check-python310.test, using the cases from the issue.