8000 Non overlapping inclusion check for literal types · Issue #11313 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Non overlapping inclusion check for literal types #11313

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

Open
sami-evangelista opened this issue Oct 11, 2021 · 4 comments
Open

Non overlapping inclusion check for literal types #11313

sami-evangelista opened this issue Oct 11, 2021 · 4 comments
Labels

Comments

@sami-evangelista
Copy link

Right now mypy spots an error on the first if condition but it would be great if it could also complain on the second one which seems a better expression of the first test:

from typing import Literal

literal_t = Literal['a', 'b', 'c']
x: literal_t = 'b'
if x == 'a' or x == 'd': ... 
if x in ('a', 'd'): ...

mypy version:

$ mypy --version
mypy 0.920+dev.bbea3b56297d3d966b9a98900c218df6616fec85
@sobolevn
Copy link
Member

in check is not support right now, as far as I remember. It could be a cool feature. And quite easy one.
We can just translate x in ('a', 'b') expression into x == 'a' or x == 'b'. And everything should work the same way.

I will look into it.

@sobolevn
Copy link
Member
sobolevn commented Nov 9, 2021

Turned out it is not as easy. Simple "translate" did not w 8000 ork. Several reasons:

  1. Because of how type narrowing is done in mypy
  2. Because in is not identical to two ==. There might be __contains__ defined, but not __eq__. Or they might be incompatible

@JelleZijlstra JelleZijlstra added topic-literal-types topic-overlap Overlapping equality check labels Mar 19, 2022
@intgr
Copy link
Contributor
intgr commented Aug 19, 2022

This issue title is misleading, should be renamed to mention in operator, nothing to do with literal types.

@sterliakov
Copy link
Collaborator

Similar/duplicate of #3229?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants
0