Skip to content

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

Merged
merged 3 commits into from
Mar 7, 2024

Conversation

ethanfurman
Copy link
Member

@ethanfurman ethanfurman commented Feb 28, 2024

@ethanfurman ethanfurman added type-bug An unexpected behavior, bug, or error 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 28, 2024
@ethanfurman ethanfurman self-assigned this Feb 28, 2024
@ethanfurman ethanfurman removed 3.11 only security fixes needs backport to 3.11 only security fixes labels Feb 28, 2024
Comment on lines 3418 to 3419
Cardinal(1, 0)
Cardinal(-1, 0)
Copy link
Member

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?

Copy link
Member Author

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,
Copy link
Member

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)

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

Comment on lines +168 to +169
def __bool__(self):
return False
Copy link
Member

Choose a reason for hiding this comment

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

Is it needed?

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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).

@ethanfurman ethanfurman merged commit 13ffd4b into python:main Mar 7, 2024
@miss-islington-app
Copy link

Thanks @ethanfurman for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

@miss-islington-app
Copy link

Sorry, @ethanfurman, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 13ffd4bd9f529b6a5fe33741fbd57f14b4b80137 3.12

ethanfurman added a commit to ethanfurman/cpython that referenced this pull request Mar 7, 2024
@bedevere-app
Copy link

bedevere-app bot commented Mar 7, 2024

GH-116476 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 Mar 7, 2024
ethanfurman added a commit that referenced this pull request Mar 7, 2024
@ethanfurman ethanfurman deleted the enum-zero_not_none branch March 8, 2024 00:00
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a 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.

Comment on lines +168 to +169
def __bool__(self):
return False
Copy link
Member

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)
Copy link
Member

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).

Copy link
Member Author

Choose a reason for hiding this comment

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

Gah. Changed to assertRaisesRegex.

Comment on lines +3434 to +3441
for nothing, e_type in (
('', None),
('', int),
([], None),
([], int),
({}, None),
({}, int),
):
Copy link
Member

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:
        ...

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point.

@ethanfurman
Copy link
Member Author

@serhiy-storchaka, fixes in #116508.

adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 25, 2024
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 only security fixes 3.13 bugs and security fixes 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