Skip to content

Commit 638d5d4

Browse files
committed
refact: Clean up annotations
1 parent 11fc771 commit 638d5d4

1 file changed

Lines changed: 24 additions & 23 deletions

File tree

src/api/constants.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
# See the file CONTRIBUTORS.md for copyright details.
55
# See https://www.gnu.org/licenses/agpl-3.0.html for details.
66
# --------------------------------------------------------------------
7+
from __future__ import annotations
78

89
import enum
910
import os
1011
from enum import StrEnum
11-
from typing import Final, Optional, Union
12+
from typing import Final
1213

1314
from .decorator import classproperty
1415

@@ -41,18 +42,18 @@ class CLASS(StrEnum):
4142
type = "type" # 7 # type
4243

4344
@classproperty
44-
def classes(cls):
45+
def classes(cls) -> tuple[CLASS, ...]:
4546
return cls.unknown, cls.var, cls.array, cls.function, cls.sub, cls.const, cls.label
4647

4748
@classmethod
48-
def is_valid(cls, class_: Union[str, "CLASS"]):
49+
def is_valid(cls, class_: str | CLASS) -> bool:
4950
"""Whether the given class is
5051
valid or not.
5152
"""
5253
return class_ in set(CLASS)
5354

5455
@classmethod
55-
def to_string(cls, class_: "CLASS"):
56+
def to_string(cls, class_: CLASS):
5657
assert cls.is_valid(class_)
5758
return class_.value
5859

@@ -69,7 +70,7 @@ class ARRAY:
6970
class TYPE(enum.IntEnum):
7071
"""Enums primary type constants"""
7172

72-
unknown = 0
73+
unknown = 0 # Denotes a type that is not yet known
7374
byte = 1
7475
ubyte = 2
7576
integer = 3
@@ -82,7 +83,7 @@ class TYPE(enum.IntEnum):
8283
boolean = 10
8384

8485
@classmethod
85-
def type_size(cls, type_: "TYPE") -> int:
86+
def type_size(cls, type_: TYPE) -> int:
8687
type_sizes = {
8788
cls.boolean: 1,
8889
cls.byte: 1,
@@ -99,50 +100,50 @@ def type_size(cls, type_: "TYPE") -> int:
99100
return type_sizes[type_]
100101

101102
@classproperty
102-
def types(cls) -> set["TYPE"]:
103+
def types(cls) -> set[TYPE]:
103104
return set(TYPE)
104105

105106
@classmethod
106-
def size(cls, type_: "TYPE") -> int:
107+
def size(cls, type_: TYPE) -> int:
107108
return cls.type_size(type_)
108109

109110
@classproperty
110-
def integral(cls) -> set["TYPE"]:
111+
def integral(cls) -> set[TYPE]:
111112
return {cls.boolean, cls.byte, cls.ubyte, cls.integer, cls.uinteger, cls.long, cls.ulong}
112113

113114
@classproperty
114-
def signed(cls) -> set["TYPE"]:
115+
def signed(cls) -> set[TYPE]:
115116
return {cls.byte, cls.integer, cls.long, cls.fixed, cls.float}
116117

117118
@classproperty
118-
def unsigned(cls) -> set["TYPE"]:
119+
def unsigned(cls) -> set[TYPE]:
119120
return {cls.boolean, cls.ubyte, cls.uinteger, cls.ulong}
120121

121122
@classproperty
122-
def decimals(cls) -> set["TYPE"]:
123+
def decimals(cls) -> set[TYPE]:
123124
return {cls.fixed, cls.float}
124125

125126
@classproperty
126-
def numbers(cls) -> set["TYPE"]:
127+
def numbers(cls) -> set[TYPE]:
127128
return cls.integral | cls.decimals
128129

129130
@classmethod
130-
def is_valid(cls, type_: "TYPE") -> bool:
131+
def is_valid(cls, type_: TYPE) -> bool:
131132
"""Whether the given type is
132133
valid or not.
133134
"""
134135
return type_ in cls.types
135136

136137
@classmethod
137-
def is_signed(cls, type_: "TYPE") -> bool:
138+
def is_signed(cls, type_: TYPE) -> bool:
138139
return type_ in cls.signed
139140

140141
@classmethod
141-
def is_unsigned(cls, type_: "TYPE") -> bool:
142+
def is_unsigned(cls, type_: TYPE) -> bool:
142143
return type_ in cls.unsigned
143144

144145
@classmethod
145-
def to_signed(cls, type_: "TYPE") -> "TYPE":
146+
def to_signed(cls, type_: TYPE) -> TYPE:
146147
"""Return signed type or equivalent"""
147148
if type_ in cls.unsigned:
148149
return {
@@ -158,12 +159,12 @@ def to_signed(cls, type_: "TYPE") -> "TYPE":
158159
return cls.unknown
159160

160161
@staticmethod
161-
def to_string(type_: "TYPE") -> str:
162+
def to_string(type_: TYPE) -> str:
162163
"""Return ID representation (string) of a type"""
163164
return type_.name
164165

165166
@staticmethod
166-
def to_type(typename: str) -> Optional["TYPE"]:
167+
def to_type(typename: str) -> TYPE | None:
167168
"""Converts a type ID to name. On error returns None"""
168169
for t in TYPE:
169170
if t.name == typename:
@@ -181,11 +182,11 @@ class SCOPE(str, enum.Enum):
181182
parameter = "parameter"
182183

183184
@staticmethod
184-
def is_valid(scope: Union[str, "SCOPE"]) -> bool:
185+
def is_valid(scope: str | SCOPE) -> bool:
185186
return scope in set(SCOPE)
186187

187188
@staticmethod
188-
def to_string(scope: "SCOPE") -> str:
189+
def to_string(scope: SCOPE) -> str:
189190
assert SCOPE.is_valid(scope)
190191
return scope.value
191192

@@ -197,11 +198,11 @@ class CONVENTION(str, enum.Enum):
197198
stdcall = "__stdcall__"
198199

199200
@staticmethod
200-
def is_valid(convention: Union[str, "CONVENTION"]):
201+
def is_valid(convention: str | CONVENTION):
201202
return convention in set(CONVENTION)
202203

203204
@staticmethod
204-
def to_string(convention: "CONVENTION"):
205+
def to_string(convention: CONVENTION):
205206
assert CONVENTION.is_valid(convention)
206207
return convention.value
207208

0 commit comments

Comments
 (0)