Skip to content

Commit a4af079

Browse files
committed
Refact SymbolTYPE and subclasses
Code cleanup and extra typing
1 parent 7dacfff commit a4af079

1 file changed

Lines changed: 19 additions & 20 deletions

File tree

src/symbols/type_.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@
1313
from src.api.constants import CLASS
1414
from src.api.config import OPTIONS
1515
from src.api.decorator import classproperty
16+
1617
from .symbol_ import Symbol
1718

1819

1920
class SymbolTYPE(Symbol):
2021
""" A Type definition. Defines a type,
2122
both user defined or basic ones.
2223
"""
23-
24-
def __init__(self, name, lineno, *children):
24+
def __init__(self, name: str, lineno: int, *children):
2525
# All children (if any) must be SymbolTYPE
2626
assert all(isinstance(x, SymbolTYPE) for x in children)
27-
super(SymbolTYPE, self).__init__(*children)
27+
super().__init__(*children)
2828
self.name = name # typename
2929
self.lineno = lineno # The line the type was defined. Line 0 = basic type
3030
self.final = self # self.final always return the original aliased type (if this type is an alias)
@@ -78,7 +78,7 @@ def is_alias(self):
7878
return False
7979

8080
def __eq__(self, other):
81-
assert isinstance(other, SymbolTYPE)
81+
assert isinstance(other, SymbolTYPE), f"Invalid operand '{other}':{type(other)}"
8282

8383
if self is not self.final:
8484
return self.final == other
@@ -117,14 +117,14 @@ class SymbolBASICTYPE(SymbolTYPE):
117117
If name is None or '', default typename from TYPES.to_string will be used.
118118
"""
119119

120-
def __init__(self, type_, name=None):
120+
def __init__(self, type_, name: str = None):
121121
""" type_ = Internal representation (e.g. TYPE.ubyte)
122122
"""
123123
assert TYPE.is_valid(type_)
124124
if not name:
125125
name = TYPE.to_string(type_)
126126

127-
super(SymbolBASICTYPE, self).__init__(name, 0)
127+
super().__init__(name, 0)
128128
self.type_ = type_
129129

130130
@property
@@ -175,10 +175,9 @@ def __bool__(self):
175175
class SymbolTYPEALIAS(SymbolTYPE):
176176
""" Defines a type which is alias of another
177177
"""
178-
179-
def __init__(self, name, lineno, alias):
178+
def __init__(self, name, lineno: int, alias: SymbolTYPE):
180179
assert isinstance(alias, SymbolTYPE)
181-
super(SymbolTYPEALIAS, self).__init__(name, lineno, alias)
180+
super().__init__(name, lineno, alias)
182181
self.final = alias.final
183182

184183
@property
@@ -215,9 +214,9 @@ class SymbolTYPEREF(SymbolTYPEALIAS):
215214
and if it was implicitly inferred or explicitly declared.
216215
"""
217216

218-
def __init__(self, type_, lineno, implicit=False):
217+
def __init__(self, type_: SymbolTYPE, lineno: int, implicit: bool = False):
219218
assert isinstance(type_, SymbolTYPE)
220-
super(SymbolTYPEREF, self).__init__(type_.name, lineno, type_)
219+
super().__init__(type_.name, lineno, type_)
221220
self.implicit = implicit
222221

223222
def to_signed(self):
@@ -255,12 +254,12 @@ class Type(object):
255254
_by_name = {x.name: x for x in types}
256255

257256
@staticmethod
258-
def size(t):
257+
def size(t: SymbolTYPE):
259258
assert isinstance(t, SymbolTYPE)
260259
return t.size
261260

262261
@staticmethod
263-
def to_string(t):
262+
def to_string(t: SymbolTYPE):
264263
assert isinstance(t, SymbolTYPE)
265264
return t.name
266265

@@ -292,37 +291,37 @@ def numbers(cls):
292291
return tuple(list(cls.integrals) + list(cls.decimals))
293292

294293
@classmethod
295-
def is_numeric(cls, t):
294+
def is_numeric(cls, t: SymbolTYPE):
296295
assert isinstance(t, SymbolTYPE)
297296
return t.final in cls.numbers
298297

299298
@classmethod
300-
def is_signed(cls, t):
299+
def is_signed(cls, t: SymbolTYPE):
301300
assert isinstance(t, SymbolTYPE)
302301
return t.final in cls.signed
303302

304303
@classmethod
305-
def is_unsigned(cls, t):
304+
def is_unsigned(cls, t: SymbolTYPE):
306305
assert isinstance(t, SymbolTYPE)
307306
return t.final in cls.unsigned
308307

309308
@classmethod
310-
def is_integral(cls, t):
309+
def is_integral(cls, t: SymbolTYPE):
311310
assert isinstance(t, SymbolTYPE)
312311
return t.final in cls.integrals
313312

314313
@classmethod
315-
def is_decimal(cls, t):
314+
def is_decimal(cls, t: SymbolTYPE):
316315
assert isinstance(t, SymbolTYPE)
317316
return t.final in cls.decimals
318317

319318
@classmethod
320-
def is_string(cls, t):
319+
def is_string(cls, t: SymbolTYPE):
321320
assert isinstance(t, SymbolTYPE)
322321
return t.final == cls.string
323322

324323
@classmethod
325-
def to_signed(cls, t):
324+
def to_signed(cls, t: SymbolTYPE):
326325
""" Return signed type or equivalent
327326
"""
328327
assert isinstance(t, SymbolTYPE)

0 commit comments

Comments
 (0)