GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Allow narrowing enum values using == by hauntsaninja · Pull Request #11521 · python/mypy

Conversation

@hauntsaninja

@hauntsaninja hauntsaninja commented Nov 11, 2021

Copy link
Copy Markdown
Collaborator

Resolves #10915, resolves #9786

See the discussion in #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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
hauntsaninja
Allow narrowing enum values using ==
db11aa5
Resolves 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.
@github-actions

github-actions Bot commented Nov 11, 2021

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

porcupine (https://github.com/Akuli/porcupine.git)
+ porcupine/pluginloader.py:350: error: Statement is unreachable  [unreachable]
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JukkaL

JukkaL commented Nov 11, 2021

Copy link
Copy Markdown
Collaborator

My gut feeling is that == and is should be treated the same. Treating them differently sounds confusing.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Michael0x2a

Michael0x2a commented Nov 11, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

IIRC, treating == and is differently was intended to be a workaround for tests that mutate state and repeatedly do something like assert wrapper.state == StateEnum.foo then later assert wrapper.state == StateEnum.bar.

That latter assert would end up narrowing away wrapper.state to nothing, causing all subsequent code to silently become unreachable unless you opt-in to using the --warn-unreachable flag.

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, assert unconditionally narrows. But maybe instead it might be better to have it only narrow types and refrain from narrowing literals?

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 isinstance(...). But people normally don't change the types of their fields in the same way they do literals, so our heuristics end up being correct most of the time.

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.

hauntsaninja reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pranavrajpal pranavrajpal mentioned this pull request Dec 29, 2021
Incorrect errors caused by boolean narrowing on attributes #11853
Closed
@AlexWaygood AlexWaygood mentioned this pull request Mar 23, 2022
Tagged unions don't work with Enums #12432
Closed
@hauntsaninja hauntsaninja mentioned this pull request Feb 9, 2023
support narrowing enum values using == and != #10915
Closed
@hauntsaninja

hauntsaninja commented Apr 24, 2023

Copy link
Copy Markdown
Collaborator Author

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 is, so it's not like we're stopping them from doing it. There is some risk that more code will go effectively unchecked, but if the most common use of this pattern is in tests, it's not the worst thing in the world.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hauntsaninja
hauntsaninja merged commit 0061d6e into python:master Apr 24, 2023
@hauntsaninja
hauntsaninja deleted the enumeq branch April 24, 2023 06:47
This was referenced Apr 24, 2023
Enum exhaustiveness checking only works using "is" comparisons, not "==", conflicting with docs #14823
Closed
Exhaustive Matching on Enums / Literals #6366
Closed
@hauntsaninja hauntsaninja mentioned this pull request May 21, 2023
Nested tagged unions stopped working #10764
Closed
@hauntsaninja hauntsaninja mentioned this pull request Aug 13, 2023
Incorrect union narrowing in chained conditionals #12208
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

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

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@hauntsaninja @JukkaL @Michael0x2a
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.