-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-116040: [Enum] fix by-value calls when second value is falsey; e.g. Cardinal(1, 0) #116072
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
Conversation
ethanfurman
commented
Feb 28, 2024
•
edited by bedevere-app
bot
Loading
edited by bedevere-app
bot
- Issue: Enum creation from values fails for certain values. #116040
Lib/test/test_enum.py
Outdated
Cardinal(1, 0) | ||
Cardinal(-1, 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you test that the result is correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
# no body? no data-type? possibly wrong usage | ||
raise TypeError( | ||
f"{cls} has no members; specify `names=()` if you meant to create a new, empty, enum" | ||
) | ||
return cls._create_( | ||
class_name=value, | ||
names=names, | ||
names=names or None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add tests for something like:
Enum('empty_enum', '')
Enum('empty_enum', [])
Enum('empty_enum', {})
Enum('empty_enum', '', type=int)
Enum('empty_enum', [], type=int)
Enum('empty_enum', {}, type=int)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
def __bool__(self): | ||
return False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, or the or None
at line 730 will fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps it should not take the boolean value there at first place, but use is not _not_given
? I suspected that it can produce weird results if the second value is false and type
is given.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what you mean -- is "second value" the names parameter? If it's falsey and a type is given, a new empty enum is created (which is appropriate).
Thanks @ethanfurman for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12. |
Sorry, @ethanfurman, I could not cleanly backport this to
|
…y; e.g. Cardinal(1, 0) (pythonGH-116072) (cherry picked from commit 13ffd4b)
GH-116476 is a backport of this pull request to the 3.12 branch. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for not commenting at the time, but something looks suspicious in this change.
def __bool__(self): | ||
return False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps it should not take the boolean value there at first place, but use is not _not_given
? I suspected that it can produce weird results if the second value is false and type
is given.
): | ||
empty_enum = Enum('empty_enum', nothing, type=e_type) | ||
self.assertEqual(len(empty_enum), 0) | ||
self.assertRaises(TypeError, 'has no members', empty_enum, 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It raises a TypeError because you try to call 'has no members'(empty_enum, 0)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gah. Changed to assertRaisesRegex
.
for nothing, e_type in ( | ||
('', None), | ||
('', int), | ||
([], None), | ||
([], int), | ||
({}, None), | ||
({}, int), | ||
): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could perhaps be clearer with nested loops.
for nothing in '', [], {}:
for e_type in None, int:
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point.
@serhiy-storchaka, fixes in #116508. |
…y; e.g. Cardinal(1, 0) (pythonGH-116072)
…y; e.g. Cardinal(1, 0) (pythonGH-116072)