GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||
GeezerVison is showing github.com — Allow narrowing enum values using == by hauntsaninja · Pull Request #11521 · python/mypy Conversation
Collaborator
Resolves #10915, resolves #9786 See the discussion in #10915. I'm sympathetic to the difference between The old test cases are pretty explicit about their intentions and are
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactionsResolves python#10915, resolves python#9786 See the discussion in python#10915. I'm sympathetic to the difference between identity and equality here being surprising and that mypy doesn't usually make concessions to mutability when type checking. The old test cases are pretty explicit about their intentions and are worth reading. Curious to see what people (and mypy-primer) have to say about this.
Contributor
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Collaborator
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
|
There was an error while loading. Please reload this page.
|
IIRC, treating == and is differently was intended to be a workaround for tests that mutate state and repeatedly do something like That latter assert would end up narrowing away In retrospect, I'm wondering if a better solution might have been to instead overhaul our heuristics for when narrowing happens in general. At the moment, I think in practice the only time you'd want to narrow literals is in if/else statements, so sticking to just narrowing there might be good enough. Or maybe alternatively, allow literal-type narrowing to sometimes change the type instead of narrowing it? Higher-level, the problem is that unconditional narrowing combined with mutable state is fundamentally unsound: you can run into the exact same problem even with Unfortunately, this "always narrow" heuristic doesn't work as well for literal types, which is why I ended up adding a bunch of misc ad-hoc heuristics such as "narrow enums only when using 'is'" to try and patch things up. In any case, I do agree the heuristics I ended up picking out are unintuitive and probably ought to be changed. But IMO we should probably design and implement better heuristics first before landing this PR to avoid causing too large of a drop in type-checking coverage for users. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This keeps coming up in issues, so I'm going to merge this. The status quo is unintuitive and users can still narrow to the bottom type using |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
support narrowing enum values using== and !=
== Narrows the type of an Enum in an if-elif block when following an is
There was an error while loading. Please reload this page.