|
5 | 5 |
|
6 | 6 | Defines basic Flag and Flags data structures. |
7 | 7 | """ |
8 | | -import collections |
9 | 8 | from collections.abc import MutableSet |
| 9 | +from typing import NamedTuple, Iterable, Set, Iterator |
10 | 10 |
|
11 | | -Flag = collections.namedtuple("Flag", ["name", "bit"]) |
12 | 11 |
|
| 12 | +class Flag(NamedTuple): |
| 13 | + name: str |
| 14 | + bit: int |
13 | 15 |
|
14 | | -class Flags(MutableSet): |
| 16 | + |
| 17 | +class Flags(MutableSet): # type: ignore |
15 | 18 | """ |
16 | 19 | A simple MutableSet implementation that will only accept known flags as |
17 | 20 | elements. |
18 | 21 |
|
19 | 22 | Will behave like a regular set(), except that a ValueError will be thrown |
20 | 23 | when .add()ing unexpected flags. |
21 | 24 | """ |
22 | | - def __init__(self, defined_flags): |
| 25 | + def __init__(self, defined_flags: Iterable[Flag]): |
23 | 26 | self._valid_flags = set(flag.name for flag in defined_flags) |
24 | | - self._flags = set() |
| 27 | + self._flags: Set[str] = set() |
25 | 28 |
|
26 | | - def __repr__(self): |
| 29 | + def __repr__(self) -> str: |
27 | 30 | return repr(sorted(list(self._flags))) |
28 | 31 |
|
29 | | - def __contains__(self, x): |
| 32 | + def __contains__(self, x: object) -> bool: |
30 | 33 | return self._flags.__contains__(x) |
31 | 34 |
|
32 | | - def __iter__(self): |
| 35 | + def __iter__(self) -> Iterator[str]: |
33 | 36 | return self._flags.__iter__() |
34 | 37 |
|
35 | | - def __len__(self): |
| 38 | + def __len__(self) -> int: |
36 | 39 | return self._flags.__len__() |
37 | 40 |
|
38 | | - def discard(self, value): |
| 41 | + def discard(self, value: str) -> None: |
39 | 42 | return self._flags.discard(value) |
40 | 43 |
|
41 | | - def add(self, value): |
| 44 | + def add(self, value: str) -> None: |
42 | 45 | if value not in self._valid_flags: |
43 | 46 | raise ValueError( |
44 | 47 | "Unexpected flag: {}. Valid flags are: {}".format( |
|
0 commit comments