FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Narrow literals in the negative case even with custom equality by A5rocks · Pull Request #18574 · python/mypy · GitHub

Repository navigation

Narrow literals in the negative case even with custom equality - #18574

Closed
A5rocks wants to merge 3 commits into
python:masterfrom
A5rocks:negative-narrowing-with-custom-eq
Closed

A5rocks wants to merge 3 commits into
python:masterfrom
A5rocks:negative-narrowing-with-custom-eq

Conversation

A5rocks commented Jan 30, 2025 •
edited
Loading

Copy link
Copy Markdown
Collaborator

Fixes #16465
Fixes #18029
Fixes #17229

Generally, people cannot replace a literal's equality. So negative narrowing is still safe. (not the case for enum literals, so those don't get this treatment)

Comment thread mypy/checker.py
if should_narrow_by_identity:
return True
else:
return not p_t.fallback.type.is_enum

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Alternative: I could try has_no_custom_eq_checks(p_t.fallback) maybe?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I don't know any other literals that could possibly have custom __eq__, amd enums are complex enough to have some unexpected corner cases - this check looks reasonable to me as-is. Maybe rewrite as return should_narrow_by_identity or not p_t.fallback.type.is_enum to make it more clear than if/else? And maybe even the whole ladder as just return isinstance(p_t, LiteralType) and (... or ...)?

This comment has been minimized.

This comment has been minimized.

Comment thread mypy/checker.py
if should_narrow_by_identity:
return True
else:
return not p_t.fallback.type.is_enum

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I don't know any other literals that could possibly have custom __eq__, amd enums are complex enough to have some unexpected corner cases - this check looks reasonable to me as-is. Maybe rewrite as return should_narrow_by_identity or not p_t.fallback.type.is_enum to make it more clear than if/else? And maybe even the whole ladder as just return isinstance(p_t, LiteralType) and (... or ...)?

@@ -845,7 +845,7 @@ x1: Union[Custom, Literal[1], Literal[2]]
if x1 == 1:
reveal_type(x1) # N: Revealed type is "Union[__main__.Custom, Literal[1], Literal[2]]"

sterliakov Mar 31, 2025 •
edited
Loading

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Why isn't Literal[2] filtered out in positive case? If x1 is that literal, it certainly won't be equal to 1, so narrowing is safe, isn't it? Do we check for custom __eq__ on the whole union?

A5rocks May 19, 2025 •
edited
Loading

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Yeah we note that custom equality exists and if so give up on narrowing currently (iirc, it's been a few months...). I guess it could be fine grained, this PR just notes that Literal[1] wouldn't fail checks.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Actually, iterating over every component type of a union and applying this logic might make sense, and would also fix this bug. But that sounds more complicated than just this.

Copy link
Copy Markdown
Collaborator

Do you still have energy to work on this? This negative narrowing looks quite helpful

A5rocks commented May 19, 2025

Copy link
Copy Markdown
Collaborator Author

Yeah, I just typically put things off and then end up not doing them!

twavv commented Jul 18, 2025

Copy link
Copy Markdown

This effects our codebase. Would love to have it merged. :^)

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants


Back | FazBrowse Home | New Git URL