-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Minor follow-on to PR #1334 (Fix enum value's __int__ returning non-int when underlying type is bool or of char type) #3232
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# -*- coding: utf-8 -*- | ||
import pytest | ||
|
||
import env | ||
from pybind11_tests import enums as m | ||
|
||
|
||
|
@@ -238,20 +239,26 @@ def test_duplicate_enum_name(): | |
|
||
def test_char_underlying_enum(): # Issue #1331/PR #1334: | ||
assert type(m.ScopedCharEnum.Positive.__int__()) is int | ||
assert int(m.ScopedChar16Enum.Zero) == 0 # int() call should successfully return | ||
assert int(m.ScopedChar16Enum.Zero) == 0 | ||
assert hash(m.ScopedChar32Enum.Positive) == 1 | ||
assert m.ScopedCharEnum.Positive.__getstate__() == 1 # return type is long in py2.x | ||
if env.PY2: | ||
assert m.ScopedCharEnum.Positive.__getstate__() == 1 # long | ||
else: | ||
assert type(m.ScopedCharEnum.Positive.__getstate__()) is int | ||
assert m.ScopedWCharEnum(1) == m.ScopedWCharEnum.Positive | ||
with pytest.raises(TypeError): | ||
# Enum should construct with a int, even with char underlying type | ||
m.ScopedWCharEnum("0") | ||
# Even if the underlying type is char, only an int can be used to construct the enum: | ||
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. The intent of this test, I remembered, was that when enum's underlying type is char, then pybind11's enum not only returns a string, but also could only be constructed with a string. So I wrote a test here to expect it to throw a |
||
m.ScopedCharEnum("0") | ||
|
||
|
||
def test_bool_underlying_enum(): | ||
assert type(m.ScopedBoolEnum.TRUE.__int__()) is int | ||
assert int(m.ScopedBoolEnum.FALSE) == 0 | ||
assert hash(m.ScopedBoolEnum.TRUE) == 1 | ||
assert m.ScopedBoolEnum.TRUE.__getstate__() == 1 | ||
if env.PY2: | ||
assert m.ScopedBoolEnum.TRUE.__getstate__() == 1 # long | ||
else: | ||
assert type(m.ScopedBoolEnum.TRUE.__getstate__()) is int | ||
assert m.ScopedBoolEnum(1) == m.ScopedBoolEnum.TRUE | ||
# Enum could construct with a bool | ||
# (bool is a strict subclass of int, and False will be converted to 0) | ||
|
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.
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.
The intent of this test was that when
__int__()
returns a non-int,int()
call will throw a error:So I wrote this call in the test and expected it to successfully return.
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.
Thanks. I think that is clear just from the test code itself.