Skip to content

Commit 4957d68

Browse files
committed
added enum.show_flag_values for compat
1 parent aa42a55 commit 4957d68

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

docs/source/pcapkit/utilities/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,5 @@ following objects and functions:
116116
- ≥ 3.8
117117
* - :func:`decimal.localcontext(ctx=None, **kwargs) <decimal.localcontext>`
118118
- ≥ 3.11
119+
* - :func:`enum.show_flag_values`
120+
- ≥ 3.11

pcapkit/utilities/compat.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
from typing import TYPE_CHECKING
55

66
if TYPE_CHECKING:
7-
from typing import Any, Callable, Generator, Optional, Type, Union
7+
from typing import Any, Callable, Iterator, Optional, Type, Union
88

99
__all__ = [
1010
# functions
11-
'final', 'localcontext',
11+
'final', 'localcontext', 'show_flag_values',
1212

1313
# exceptions
1414
'ModuleNotFoundError',
@@ -164,3 +164,26 @@ def localcontext(ctx: 'Optional[Context]' = None, **kwargs: 'Any') -> '_ContextM
164164
yield lc
165165
else:
166166
from decimal import localcontext
167+
168+
if sys.version_info < (3, 11):
169+
from enum import Enum
170+
171+
if TYPE_CHECKING:
172+
from enum import IntFlag
173+
174+
def _iter_bits_lsb(num: 'int') -> 'Iterator[int]':
175+
# num must be a positive integer
176+
original = num
177+
if isinstance(num, Enum):
178+
num = num.value
179+
if num < 0:
180+
raise ValueError('%r is not a positive integer' % original)
181+
while num:
182+
b = num & (~num + 1)
183+
yield b
184+
num ^= b
185+
186+
def show_flag_values(value: 'IntFlag') -> 'list[int]':
187+
return list(_iter_bits_lsb(value))
188+
else:
189+
from enum import show_flag_values # type: ignore[attr-defined]

0 commit comments

Comments
 (0)