You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Cases within match statement do not exhaustively handle all values
Unhandled type: "CodegenChunk"
If exhaustive handling is not intended, add "case _: pass"Pylance[reportMatchNotExhaustive](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportMatchNotExhaustive)
Notes
This issue not present on pyright
The text was updated successfully, but these errors were encountered:
It looks like you have the reportMatchNotExhaustive check enabled. This check isn't enabled by default. It's considered a "strict" type checking option, and it's a bit opinionated in how it works.
Exhaustion detection is based on type narrowing. You're using a pattern here that pyright (the type checker that underlies pylance) does not narrow. Mypy (another Python static type checker) also doesn't support this pattern. What you're doing here is pretty atypical. The more typical way of handling this problem is to match on the discriminating key only. Both pyright and mypy support this.
If you change your code as follows, it will work as expected.
defsomething(data: Any) ->None:
result=cast(CodegenChunk, json.loads(data["message"]))
matchresult:
case {"type": "message"}:
print("message", result["message"])
case {"type": "result"}:
print("result", result["sandbox_url"])
I don't know how this function is intended to be used, but if the JSON data is coming from some external source (i.e. its contents haven't been generated from within your program or already validated by some other code), then it's not safe to assume that its contents will match one of these two patterns. In that case, I recommend that you stick with the original code but include a fallback pattern:
matchresult:
case {"type": "message", "message": text}:
print("message", text)
case {"type": "result", "sandbox_url": sandbox_url}:
print("result", sandbox_url)
case_:
print("None of the above")
In summary, I don't consider this a bug in pyright. It could be a feature request, but we're unlikely to add support for this particular type narrowing pattern unless/until we get signal from enough other pyright users that it's a commonly-used pattern.
Environment data
Code Snippet
Repro Steps
Run VS code on that file with pylance
Expected behavior
No Error
Actual behavior
Notes
This issue not present on pyright
The text was updated successfully, but these errors were encountered: