-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
StrEnum.__getitem__
raises TypeError
#128659
Comments
>>> Foo.__getitem__
<bound method EnumType.__getitem__ of <enum 'Foo'>>
>>> Bar.__getitem__
<slot wrapper '__getitem__' of 'str' objects> They work implicitly but not explicitly: >>> Bar['one']
<Bar.one: 'one'>
>>> Bar.__getitem__('one')
Traceback (most recent call last):
File "<python-input-19>", line 1, in <module>
Bar.__getitem__('one')
~~~~~~~~~~~~~~~^^^^^^^
TypeError: expected 1 argument, got 0
>>> Bar.__getitem__(Bar, 'one')
Traceback (most recent call last):
File "<python-input-20>", line 1, in <module>
Bar.__getitem__(Bar, 'one')
~~~~~~~~~~~~~~~^^^^^^^^^^^^
TypeError: descriptor '__getitem__' requires a 'str' object but received a 'EnumType' Because >>> Bar.__contains__
<slot wrapper '__contains__' of 'str' objects>
>>> Bar.__len__
<slot wrapper '__len__' of 'str' objects>
>>> len(Bar)
2
>>> Bar.__len__()
Traceback (most recent call last):
File "<python-input-22>", line 1, in <module>
Bar.__len__()
~~~~~~~~~~~^^
TypeError: descriptor '__len__' of 'str' object needs an argument
>>> Bar.__len__(Bar)
Traceback (most recent call last):
File "<python-input-23>", line 1, in <module>
Bar.__len__(Bar)
~~~~~~~~~~~^^^^^
TypeError: descriptor '__len__' requires a 'str' object but received a 'EnumType'
>>> Bar.__len__('abc')
3 Quick hack fix, add the following to # re-override methods overridden by str, see gh-128659
__contains__ = classmethod(EnumType.__contains__)
__getitem__ = classmethod(EnumType.__getitem__)
__iter__ = classmethod(EnumType.__iter__)
__len__ = classmethod(EnumType.__len__) Before: >>> Foo.__getitem__
<bound method EnumType.__getitem__ of <enum 'Foo'>>
>>> Bar.__getitem__
<slot wrapper '__getitem__' of 'str' objects> After: >>> Foo.__getitem__
<bound method EnumType.__getitem__ of <enum 'Foo'>>
>>> Bar.__getitem__
<bound method EnumType.__getitem__ of <enum 'Bar'>>
>>> Bar.__getitem__('one')
<Bar.one: 'one'> |
Then what |
It will not act as a |
I'll need to get some tests written for the expected behavior of those dunders. |
We need different, well defined behavior for As for the dunder methods, we have precedents. For example, >>> int.__or__
<slot wrapper '__or__' of 'int' objects>
>>> int.__or__(1, 2)
3
>>> str.__or__
<method-wrapper '__or__' of type object at 0x563a4b54f960>
>>> str.__or__(int)
str | int So I think we should not change anything in StrEnum, at least not before we change the larger image. |
Bug report
Bug description:
CPython versions tested on:
3.12, 3.13
Operating systems tested on:
macOS
The text was updated successfully, but these errors were encountered: