Skip to content

gh-115539: Allow enum.Flag to have None members #115636

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

Merged
merged 2 commits into from
Feb 19, 2024

Conversation

Jason-Y-Z
Copy link
Contributor

@Jason-Y-Z Jason-Y-Z commented Feb 18, 2024

This PR addresses the issue mentioned in Issue #115539 , where having a None member in enum.Flag causes an Exception.
The approach taken is simply to add a isinstance(_, int) check before using the member's value, at 2 relevant places.
This is a small addition so probably can skip-news.

Copy link
Member

@ethanfurman ethanfurman left a comment

Choose a reason for hiding this comment

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

Looks good so far, but we need to also handle cases where the None flag is combined with other flags. Currently it looks like:

>>> Huh.A | Huh.Z                           # A = 1, Z = None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    Huh.A | Huh.Z
    ~~~~~~^~~~~~~
  File "/source/python/cpython/Lib/enum.py", line 1571, in __or__
    return self.__class__(value | other)
                          ~~~~~~^~~~~~~
TypeError: unsupported operand type(s) for |: 'int' and 'NoneType'

I think a TypeError is fine, but it needs a better error message.

@bedevere-app
Copy link

bedevere-app bot commented Feb 19, 2024

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@Jason-Y-Z
Copy link
Contributor Author

I have made the requested changes; please review again

@Jason-Y-Z
Copy link
Contributor Author

@ethanfurman Thanks for the quick review! Shall I add a News entry or shall we skip news?

@ethanfurman ethanfurman self-assigned this Feb 19, 2024
@ethanfurman ethanfurman added type-bug An unexpected behavior, bug, or error skip news stdlib Python modules in the Lib dir 3.11 only security fixes 3.12 only security fixes needs backport to 3.11 only security fixes 3.13 bugs and security fixes needs backport to 3.12 only security fixes labels Feb 19, 2024
@ethanfurman
Copy link
Member

We'll skip news and backport.

@ethanfurman ethanfurman merged commit c2cb31b into python:main Feb 19, 2024
@miss-islington-app
Copy link

Thanks @Jason-Y-Z for the PR, and @ethanfurman for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11, 3.12.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Feb 19, 2024
@bedevere-app
Copy link

bedevere-app bot commented Feb 19, 2024

GH-115694 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 only security fixes label Feb 19, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Feb 19, 2024
@bedevere-app
Copy link

bedevere-app bot commented Feb 19, 2024

GH-115695 is a backport of this pull request to the 3.11 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.11 only security fixes label Feb 19, 2024
@Jason-Y-Z Jason-Y-Z deleted the fix-issue-115539 branch February 19, 2024 22:47
ethanfurman pushed a commit that referenced this pull request Feb 20, 2024
…H-115695)

gh-115539: Allow enum.Flag to have None members (GH-115636)
(cherry picked from commit c2cb31b)

Co-authored-by: Jason Zhang <[email protected]>
ethanfurman pushed a commit that referenced this pull request Feb 20, 2024
…H-115694)

gh-115539: Allow enum.Flag to have None members (GH-115636)
(cherry picked from commit c2cb31b)

Co-authored-by: Jason Zhang <[email protected]>
woodruffw pushed a commit to woodruffw-forks/cpython that referenced this pull request Mar 4, 2024
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
LukasWoodtli pushed a commit to LukasWoodtli/cpython that referenced this pull request Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 only security fixes 3.13 bugs and security fixes skip news stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants