Skip to content

Commit 3fe87bd

Browse files
committed
refact: use Python's Enum for KIND
1 parent 54997da commit 3fe87bd

3 files changed

Lines changed: 15 additions & 22 deletions

File tree

src/api/constants.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -197,31 +197,24 @@ def to_string(scope: 'SCOPE'):
197197
return scope.value
198198

199199

200-
class KIND:
200+
@enum.unique
201+
class KIND(str, enum.Enum):
201202
""" Enum kind
202203
"""
203-
unknown = None
204+
unknown = 'unknown'
204205
var = 'var'
205206
function = 'function'
206207
sub = 'sub'
207-
type_ = 'type'
208-
209-
_NAMES = {
210-
unknown: '(unknown)',
211-
var: 'variable',
212-
function: 'function',
213-
sub: 'subroutine',
214-
type_: 'type'
215-
}
208+
type = 'type'
216209

217-
@classmethod
218-
def is_valid(cls, kind):
219-
return cls._NAMES.get(kind, None) is not None
210+
@staticmethod
211+
def is_valid(kind: Union[str, 'KIND']):
212+
return kind in set(KIND)
220213

221-
@classmethod
222-
def to_string(cls, kind):
223-
assert cls.is_valid(kind)
224-
return cls._NAMES.get(kind)
214+
@staticmethod
215+
def to_string(kind: 'KIND'):
216+
assert KIND.is_valid(kind)
217+
return kind.value
225218

226219

227220
class CONVENTION:

src/api/errmsg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def syntax_error_cant_convert_to_type(lineno: int, expr_str: str, type_: str):
270270
# Syntax error: is a SUB not a FUNCTION
271271
# ----------------------------------------
272272
def syntax_error_is_a_sub_not_a_func(lineno: int, name: str):
273-
error(lineno, "'%s' is SUBROUTINE not a FUNCTION" % name)
273+
error(lineno, "'%s' is a SUB not a FUNCTION" % name)
274274

275275

276276
# ----------------------------------------

tests/functional/test_errmsg.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ doloop2.bas:4: warning: Variable 'a' is never used
1515
dowhile1.bas:1: warning: Condition is always True
1616
dowhile1.bas:1: warning: Empty loop
1717
>>> process_file('subcall1.bas')
18-
subcall1.bas:6: error: 'test' is SUBROUTINE not a FUNCTION
18+
subcall1.bas:6: error: 'test' is a SUB not a FUNCTION
1919
>>> process_file('subcall2.bas')
20-
subcall2.bas:6: error: 'test' is a SUBROUTINE, not a FUNCTION
20+
subcall2.bas:6: error: 'test' is a SUB, not a FUNCTION
2121
>>> process_file('prepro05.bi')
2222
prepro05.bi:2: warning: [W510] "test" redefined (previous definition at prepro05.bi:2)
2323
>>> process_file('prepro07.bi')
@@ -86,7 +86,7 @@ strict.bas:4: error: strict mode: missing type declaration for 'a'
8686
>>> process_file('errletfunc.bas')
8787
errletfunc.bas:5: error: Cannot assign a value to 'x'. It's not a variable
8888
>>> process_file('read0.bas')
89-
read0.bas:12: error: 'x' is SUBROUTINE not a FUNCTION
89+
read0.bas:12: error: 'x' is a SUB not a FUNCTION
9090
>>> process_file('read1.bas')
9191
read1.bas:11: error: Cannot read 'x'. It's an array
9292
>>> process_file('read3.bas')

0 commit comments

Comments
 (0)