lib.enum: honor enum.nonmember.
Use _EnumDict._member_names to determine which members to consider. This way we don't need to redo sunder/dunder checks, and `nonmember`s (introduced in py3.11) are correctly excluded. This is a defacto public API, given it remains usable from py3.8 until py3.12 inclusive. (_member_names changes from a list to a keys-only dict for performance reasons in py3.11, but they iterate the same.) In current Python main (i.e. what will most likely be 3.13), a "member_names" property is added which returns those keys.
This commit is contained in:
parent
890e099ec3
commit
c40cfc9fb5
2 changed files with 24 additions and 3 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import enum as py_enum
|
||||
import operator
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
from amaranth import *
|
||||
from amaranth.lib.enum import Enum, EnumMeta, Flag, IntEnum, EnumView, FlagView
|
||||
|
|
@ -288,3 +289,23 @@ class EnumTestCase(FHDLTestCase):
|
|||
B = 1
|
||||
a = Signal(EnumA)
|
||||
assert isinstance(a, CustomView)
|
||||
|
||||
@unittest.skipUnless(hasattr(py_enum, "nonmember"), "Python<3.11 lacks nonmember")
|
||||
def test_enum_member_nonmember(self):
|
||||
with self.assertRaisesRegex(
|
||||
TypeError, r"^Value \{\} of enumeration member 'x' must.*$"
|
||||
):
|
||||
class EnumA(IntEnum, shape=4):
|
||||
A = 1
|
||||
x = {}
|
||||
|
||||
empty = {}
|
||||
class EnumA(IntEnum, shape=4):
|
||||
A = 1
|
||||
x = py_enum.nonmember(empty)
|
||||
self.assertIs(empty, EnumA.x)
|
||||
|
||||
class EnumB(IntEnum, shape=4):
|
||||
A = 1
|
||||
B = py_enum.member(2)
|
||||
self.assertIs(2, EnumB.B.value)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue