Skip to content

Commit 4379073

Browse files
author
Jose Rodriguez
committed
typing: improves typing
Also remove useles return statements in arg parsing
1 parent 7e7bebf commit 4379073

4 files changed

Lines changed: 25 additions & 22 deletions

File tree

src/api/global_.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@
88
# This program is Free Software and is released under the terms of
99
# the GNU General License
1010
# ----------------------------------------------------------------------
11-
from typing import Dict, Final, List, NamedTuple, Optional, Set
11+
from __future__ import annotations
12+
13+
from typing import TYPE_CHECKING, Dict, Final, List, NamedTuple, Optional, Set
1214

1315
from src.api.constants import TYPE, LoopType
1416
from src.api.opcodestemps import OpcodesTemps
1517

18+
if TYPE_CHECKING:
19+
from src.symbols.id_ import SymbolID
20+
21+
1622
# ----------------------------------------------------------------------
1723
# Simple global container for internal constants.
1824
# Internal constants might be architecture dependant. They're set
@@ -86,7 +92,7 @@ class LoopInfo(NamedTuple):
8692
# Function calls pending to check
8793
# Each scope pushes (prepends) an empty list
8894
# ----------------------------------------------------------------------
89-
FUNCTION_CALLS = []
95+
FUNCTION_CALLS: list[SymbolID] = []
9096

9197
# ----------------------------------------------------------------------
9298
# Function level entry ID in which scope we are in. If the list
@@ -102,7 +108,7 @@ class LoopInfo(NamedTuple):
102108
# ----------------------------------------------------------------------
103109
# FUNCTIONS pending to translate after parsing stage
104110
# ----------------------------------------------------------------------
105-
FUNCTIONS = []
111+
FUNCTIONS: list[SymbolID] = []
106112

107113
# ----------------------------------------------------------------------
108114
# Parameter alignment. Must be set by arch.<arch>.__init__
@@ -112,7 +118,7 @@ class LoopInfo(NamedTuple):
112118
# ----------------------------------------------------------------------
113119
# Data type used for array boundaries. Must be an integral
114120
# ----------------------------------------------------------------------
115-
BOUND_TYPE = None # Set to None, so if not set will raise error
121+
BOUND_TYPE: TYPE # Unset, so if not set will raise error
116122

117123
# ----------------------------------------------------------------------
118124
# Data type used for elements size. Must be an integral type
@@ -155,7 +161,7 @@ class LoopInfo(NamedTuple):
155161
# ----------------------------------------------------------------------
156162
# Type used internally for pointer and memory addresses
157163
# ----------------------------------------------------------------------
158-
PTR_TYPE = None
164+
PTR_TYPE: TYPE # Unset, so if not set will raise error
159165

160166
# ----------------------------------------------------------------------
161167
# Character used for name mangling. Usually '_' or '.'

src/ast/tree.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
# -*- coding: utf-8 -*-
33

44
import collections.abc
5-
from typing import Iterable, List, Optional, Union
5+
from typing import Iterable, Optional, Union
66

77
from src.api.exception import Error
88

9-
__all__ = ["NotAnAstError", "Tree", "ChildrenList"]
9+
__all__ = "NotAnAstError", "Tree", "ChildrenList"
1010

1111

1212
class NotAnAstError(Error):
@@ -81,7 +81,7 @@ class ChildrenList:
8181
def __init__(self, node: Tree):
8282
assert isinstance(node, Tree)
8383
self.owner = node # Node having this children
84-
self._children: List[Tree] = []
84+
self._children: list[Tree | None] = []
8585

8686
def __getitem__(self, key: Union[int, slice]):
8787
if isinstance(key, int):

src/zxbc/args_config.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
#!/usr/bin/env python3
2-
2+
from __future__ import annotations
33
import os
4-
from typing import List
4+
from typing import TYPE_CHECKING
55

66
import src.api.config
77
import src.api.global_ as gl
88
from src import arch
99
from src.api import debug, errmsg
1010
from src.api.config import OPTIONS
1111
from src.api.utils import open_file
12-
from src.zxbc import args_parser, zxbparser
12+
from src.zxbc import args_parser
13+
14+
if TYPE_CHECKING:
15+
from argparse import Namespace
1316

1417
__all__ = ["FileType", "parse_options", "set_option_defines"]
1518

@@ -21,7 +24,7 @@ class FileType:
2124
TZX = "tzx"
2225

2326

24-
def parse_options(args: List[str] = None):
27+
def parse_options(args: list[str] | None = None) -> Namespace:
2528
"""Parses command line options and setup global Options container"""
2629
parser = args_parser.parser()
2730
options = parser.parse_args(args=args)
@@ -56,7 +59,6 @@ def parse_options(args: List[str] = None):
5659

5760
if options.arch not in arch.AVAILABLE_ARCHITECTURES:
5861
parser.error(f"Invalid architecture '{options.arch}'")
59-
return 2
6062

6163
OPTIONS.architecture = options.arch
6264

@@ -67,7 +69,6 @@ def parse_options(args: List[str] = None):
6769

6870
if duplicated_options:
6971
parser.error(f"Warning(s) {', '.join(duplicated_options)} cannot be enabled " f"and disabled simultaneously")
70-
return 2
7172

7273
for warn_code in enabled_warnings:
7374
errmsg.enable_warning(warn_code)
@@ -103,15 +104,12 @@ def parse_options(args: List[str] = None):
103104

104105
if options.basic and not options.tzx and not options.tap:
105106
parser.error("Option --BASIC and --autorun requires --tzx or tap format")
106-
return 4
107107

108108
if options.append_binary and not options.tzx and not options.tap:
109109
parser.error("Option --append-binary needs either --tap or --tzx")
110-
return 5
111110

112111
if options.asm and options.memory_map:
113112
parser.error("Option --asm and --mmap cannot be used together")
114-
return 6
115113

116114
OPTIONS.use_basic_loader = options.basic
117115
OPTIONS.autorun = options.autorun
@@ -128,12 +126,11 @@ def parse_options(args: List[str] = None):
128126
args = [options.PROGRAM]
129127
if not os.path.exists(options.PROGRAM):
130128
parser.error("No such file or directory: '%s'" % args[0])
131-
return 2
132129

133130
set_option_defines()
134131

135132
OPTIONS.include_path = options.include_path
136-
OPTIONS.input_filename = zxbparser.FILENAME = os.path.basename(args[0])
133+
OPTIONS.input_filename = os.path.basename(args[0])
137134

138135
if not OPTIONS.output_filename:
139136
OPTIONS.output_filename = (
@@ -146,7 +143,7 @@ def parse_options(args: List[str] = None):
146143
return options
147144

148145

149-
def set_option_defines():
146+
def set_option_defines() -> None:
150147
"""Sets some macros automatically, according to options"""
151148
if OPTIONS.memory_check:
152149
OPTIONS.__DEFINES["__MEMORY_CHECK__"] = ""

src/zxbc/zxbparser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from math import pi as PI
1616

1717
# typings
18-
from typing import List, NamedTuple, Optional
18+
from typing import NamedTuple, Optional
1919

2020
import src.api.config
2121
import src.api.dataref
@@ -64,7 +64,7 @@
6464
# Function level entry ID in which scope we are into. If the list
6565
# is empty, we are at global scope
6666
# ----------------------------------------------------------------------
67-
FUNCTION_LEVEL: List[SymbolID] = gl.FUNCTION_LEVEL
67+
FUNCTION_LEVEL: list[SymbolID] = gl.FUNCTION_LEVEL
6868

6969
# ----------------------------------------------------------------------
7070
# Function calls pending to check

0 commit comments

Comments
 (0)