| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
There was a problem hiding this comment.
Nice!
Does your fix address any of the other match statement issues?
https://github.com/python/mypy/issues?q=sort%3Aupdated-desc+is%3Aopen+label%3Atopic-match-statement
Sorry, something went wrong.
Co-authored-by: Hashem <Hnasar@users.noreply.github.com>
From reading through the issues, I believe it should at least solve these:
I notice there are a bunch of match exhaustiveness issues with narrowing of the type passed on to the next match branches, which this PR does not fix. |
Sorry, something went wrong.
I don't think so, as I return the "current type" as the "rest type", which means the remainder/rest type (which is what gets passed on to the next match case) doesn't get narrowed properly yet. I have yet to figure out how to do this. |
Sorry, something went wrong.
for more information, see https://pre-commit.ci
|
Hi @bergkvist @Hnasar are there any blockers for landing this PR? |
Sorry, something went wrong.
|
I don't think this is solving the issue in my use case involving TypedDicts: $ uvx --from 'mypy @ git+https://github.com/bergkvist/mypy@tobias/match-union-narrowing' mypy
Updated https://github.com/bergkvist/mypy (92d3c91aaf2757706f9ef923650eb342d19af2fb)
Built mypy @ git+https://github.com/bergkvist/mypy@92d3c91aaf2757706f9ef923650eb342d19af2fb
Installed 3 packages in 17ms
src/pack.py:150: error: "object" has no attribute "encode" [attr-defined]
src/pack.py:151: error: "object" has no attribute "encode" [attr-defined]
I have: match node:
case {"type": ("assign" | "cond_assign") as op, "key": key, "value": value}:
kbs = key.encode()
vbs = value.encode()
...where reveal_type(node) shows src/pack.py:148: note: Revealed type is "Union[TypedDict('propsast.AssignmentOperation', {'type': Union[Literal['assign'], Literal['cond_assign']], 'key': builtins.str, 'value': builtins.str, 'lineno': builtins.int}), TypedDict('propsast.IncludeOperation', {'type': Union[Literal['include'], Literal['tryinclude']], 'filename': builtins.str, 'lineno': builtins.int})]"
I tried removing the disjunction in the case statement and having a case branch for each literal value but that didn't help. |
Sorry, something went wrong.
|
@bergkvist Sorry for the late reply, but can you rebase this on the latest mypy, then we can ping one of the real maintainers to get someone with commit privileges to take a look. |
Sorry, something went wrong.
There was a problem hiding this comment.
Thanks for working on this! Added two comments, would also be great to add test cases that can detect the effect of those two suggestions
Sorry, something went wrong.
| typ, _, capture = self.accept(o, t) | ||
| if not is_uninhabited(get_proper_type(typ)): | ||
| union_items.append(typ) | ||
| union_captures.update(capture) |
There was a problem hiding this comment.
hmm shouldn't the values in union_captures be a union of all the things, instead of potentially clobbering?
Sorry, something went wrong.
| union_items.append(typ) | ||
| union_captures.update(capture) | ||
|
|
||
| rest_items: list[Type] = [] |
There was a problem hiding this comment.
iiuc this should use conditional_types_with_intersection
Sorry, something went wrong.
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Sorry, something went wrong.
I realised there is a simpler and more complete approach than the one taken in python#19600. This adds the new "Step 2" to the original code. Best way to review the diff is probably to check it out and review it squashed with python#19600 , but really the net logic change is just "Step 2" Fixes python#15190 Fixes python#17549 Fixes python#17600 Mostly fixes python#18039 Helps with things in python#19081
| Back | FazBrowse Home | New Git URL |
Improve inference/narrowing support for union types in match statements.
Fixes #17549
Before:
After:
Related: