Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tagged unions not narrowed down correctly for TypedDicts #1240

Closed
klbostee opened this issue May 6, 2021 · 4 comments
Closed

Tagged unions not narrowed down correctly for TypedDicts #1240

klbostee opened this issue May 6, 2021 · 4 comments
Labels
enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@klbostee
Copy link

klbostee commented May 6, 2021

Environment data

  • Language Server version: 2021.5.0
  • OS and version: macOS 10.15.7
  • Python version (& distribution if applicable, e.g. Anaconda): CPython 2.7

Expected behaviour

The ability to discriminate between two TypedDicts using a Literal type, as explained in detail here: https://mypy.readthedocs.io/en/stable/literal_types.html#tagged-unions

Actual behaviour

The Union type doesn't get narrowed by a check on the Literal:

image

@github-actions github-actions bot added the triage label May 6, 2021
@klbostee klbostee changed the title Tagged unions not narrowed down correctly Tagged unions not narrowed down correctly for TypedDicts May 6, 2021
@erictraut
Copy link
Contributor

Thanks for the feature request.

Pylance already supports type narrowing for tagged unions of classes that have literal-typed fields. It doesn't currently support type narrowing for tagged unions of typed dicts with literal-typed values. Since mypy already supports this case, I think pyright should as well. I'll add it to the backlog.

@erictraut
Copy link
Contributor

I've added support for type narrowing of tagged unions of typed dicts for conditional expressions of the form x[K] == V and x[K] != V. This support will be in the next release.

Thanks again for the feature request.

@erictraut erictraut added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label May 24, 2021
@klbostee
Copy link
Author

For what it's worth: the specific use case I had in mind would also benefit from the ability to narrow with a conditional expression of the form x[K] in (V, W), but I'm already very happy that we'll have == and != soon of course! 🙂

@jakebailey
Copy link
Member

This issue has been fixed in version 2021.5.4, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202154-26-may-2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

3 participants