-
Notifications
You must be signed in to change notification settings - Fork 2.2k
enum: Add iteration and simple type-based check, towards PEP 435 #2744
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
Draft
EricCousineau-TRI
wants to merge
4
commits into
pybind:master
Choose a base branch
from
EricCousineau-TRI:issue-2332-pep-435-enums-pr2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,44 @@ | ||
# -*- coding: utf-8 -*- | ||
import pytest | ||
|
||
import env # noqa: F401 | ||
|
||
from pybind11_tests import enums as m | ||
|
||
if env.PY2: | ||
enum = None | ||
else: | ||
import enum | ||
|
||
|
||
def is_enum(cls): | ||
"""Example showing how to recognize a class as pybind11 enum or a | ||
PEP 345 enum.""" | ||
if enum is not None: | ||
if issubclass(cls, enum.Enum): | ||
return True | ||
return getattr(cls, "is_pybind11_enum", False) | ||
|
||
|
||
def test_pep435(): | ||
# See #2332. | ||
cls = m.UnscopedEnum | ||
names = ("EOne", "ETwo", "EThree") | ||
values = (cls.EOne, cls.ETwo, cls.EThree) | ||
raw_values = (1, 2, 3) | ||
|
||
assert len(cls) == len(names) | ||
assert list(cls) == list(values) | ||
assert is_enum(cls) | ||
if enum: | ||
assert not issubclass(cls, enum.Enum) | ||
for name, value, raw_value in zip(names, values, raw_values): | ||
assert isinstance(value, cls) | ||
if enum: | ||
assert not isinstance(value, enum.Enum) | ||
assert value.name == name | ||
assert value.value == raw_value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Self-note: Should try to implement |
||
|
||
|
||
def test_unscoped_enum(): | ||
assert str(m.UnscopedEnum.EOne) == "UnscopedEnum.EOne" | ||
|
@@ -13,15 +50,24 @@ def test_unscoped_enum(): | |
|
||
# name property | ||
assert m.UnscopedEnum.EOne.name == "EOne" | ||
assert m.UnscopedEnum.EOne.value == 1 | ||
assert m.UnscopedEnum.ETwo.name == "ETwo" | ||
assert m.EOne.name == "EOne" | ||
# name readonly | ||
assert m.UnscopedEnum.ETwo.value == 2 | ||
assert m.EOne is m.UnscopedEnum.EOne | ||
# name, value readonly | ||
with pytest.raises(AttributeError): | ||
m.UnscopedEnum.EOne.name = "" | ||
# name returns a copy | ||
foo = m.UnscopedEnum.EOne.name | ||
foo = "bar" | ||
with pytest.raises(AttributeError): | ||
m.UnscopedEnum.EOne.value = 10 | ||
# name, value returns a copy | ||
# TODO: Neither the name nor value tests actually check against aliasing. | ||
# Use a mutable type that has reference semantics. | ||
nonaliased_name = m.UnscopedEnum.EOne.name | ||
nonaliased_name = "bar" # noqa: F841 | ||
assert m.UnscopedEnum.EOne.name == "EOne" | ||
nonaliased_value = m.UnscopedEnum.EOne.value | ||
nonaliased_value = 10 # noqa: F841 | ||
assert m.UnscopedEnum.EOne.value == 1 | ||
|
||
# __members__ property | ||
assert m.UnscopedEnum.__members__ == { | ||
|
@@ -33,8 +79,8 @@ def test_unscoped_enum(): | |
with pytest.raises(AttributeError): | ||
m.UnscopedEnum.__members__ = {} | ||
# __members__ returns a copy | ||
foo = m.UnscopedEnum.__members__ | ||
foo["bar"] = "baz" | ||
nonaliased_members = m.UnscopedEnum.__members__ | ||
nonaliased_members["bar"] = "baz" | ||
assert m.UnscopedEnum.__members__ == { | ||
"EOne": m.UnscopedEnum.EOne, | ||
"ETwo": m.UnscopedEnum.ETwo, | ||
|
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.
This comment was marked as resolved.
Sorry, something went wrong.
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, didn't realize this should be done in
enum.EnumMeta
, not here. 🤦Is there a way to do it without altering builtin classes?
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.
Not that I'm aware of :( I had gone the route of modifying builtin classes, but yuck:
master...a41944c
(I didn't get it fully working, hit some weird recursion thing, and gave up 'cause ewww)
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 agree, it does look too hacky and doesn't sound like right thing to do.
Actually pybind-enum is NOT
enum.Enum
, so maybe we shouldn't try to make this check return true? I think it would be more appropriate to introducecollections.abc.Enum
(e.g. in python 3.10) and makeisinstance(pybind_enum_value, colletions.abc.Enum)
recognize all PEP-complaining enum implementations? In meantime one probably have no choice other than stick to self-written check method (like one in tests in this PR).I think such doubtful hacks would greatly reduce the PR merge chances, so I would leave attempts to introduce them, at least for now.