Skip to content

Commit

Permalink
typing: improves typing
Browse files Browse the repository at this point in the history
Also remove useles return statements in arg parsing
  • Loading branch information
Jose Rodriguez committed Aug 27, 2023
1 parent 7e7bebf commit 4379073
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
16 changes: 11 additions & 5 deletions src/api/global_.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@
# This program is Free Software and is released under the terms of
# the GNU General License
# ----------------------------------------------------------------------
from typing import Dict, Final, List, NamedTuple, Optional, Set
from __future__ import annotations

from typing import TYPE_CHECKING, Dict, Final, List, NamedTuple, Optional, Set

from src.api.constants import TYPE, LoopType
from src.api.opcodestemps import OpcodesTemps

if TYPE_CHECKING:
from src.symbols.id_ import SymbolID


# ----------------------------------------------------------------------
# Simple global container for internal constants.
# Internal constants might be architecture dependant. They're set
Expand Down Expand Up @@ -86,7 +92,7 @@ class LoopInfo(NamedTuple):
# Function calls pending to check
# Each scope pushes (prepends) an empty list
# ----------------------------------------------------------------------
FUNCTION_CALLS = []
FUNCTION_CALLS: list[SymbolID] = []

# ----------------------------------------------------------------------
# Function level entry ID in which scope we are in. If the list
Expand All @@ -102,7 +108,7 @@ class LoopInfo(NamedTuple):
# ----------------------------------------------------------------------
# FUNCTIONS pending to translate after parsing stage
# ----------------------------------------------------------------------
FUNCTIONS = []
FUNCTIONS: list[SymbolID] = []

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

# ----------------------------------------------------------------------
# Data type used for elements size. Must be an integral type
Expand Down Expand Up @@ -155,7 +161,7 @@ class LoopInfo(NamedTuple):
# ----------------------------------------------------------------------
# Type used internally for pointer and memory addresses
# ----------------------------------------------------------------------
PTR_TYPE = None
PTR_TYPE: TYPE # Unset, so if not set will raise error

# ----------------------------------------------------------------------
# Character used for name mangling. Usually '_' or '.'
Expand Down
6 changes: 3 additions & 3 deletions src/ast/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# -*- coding: utf-8 -*-

import collections.abc
from typing import Iterable, List, Optional, Union
from typing import Iterable, Optional, Union

from src.api.exception import Error

__all__ = ["NotAnAstError", "Tree", "ChildrenList"]
__all__ = "NotAnAstError", "Tree", "ChildrenList"


class NotAnAstError(Error):
Expand Down Expand Up @@ -81,7 +81,7 @@ class ChildrenList:
def __init__(self, node: Tree):
assert isinstance(node, Tree)
self.owner = node # Node having this children
self._children: List[Tree] = []
self._children: list[Tree | None] = []

def __getitem__(self, key: Union[int, slice]):
if isinstance(key, int):
Expand Down
21 changes: 9 additions & 12 deletions src/zxbc/args_config.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#!/usr/bin/env python3

from __future__ import annotations
import os
from typing import List
from typing import TYPE_CHECKING

import src.api.config
import src.api.global_ as gl
from src import arch
from src.api import debug, errmsg
from src.api.config import OPTIONS
from src.api.utils import open_file
from src.zxbc import args_parser, zxbparser
from src.zxbc import args_parser

if TYPE_CHECKING:
from argparse import Namespace

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

Expand All @@ -21,7 +24,7 @@ class FileType:
TZX = "tzx"


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

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

OPTIONS.architecture = options.arch

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

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

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

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

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

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

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

set_option_defines()

OPTIONS.include_path = options.include_path
OPTIONS.input_filename = zxbparser.FILENAME = os.path.basename(args[0])
OPTIONS.input_filename = os.path.basename(args[0])

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


def set_option_defines():
def set_option_defines() -> None:
"""Sets some macros automatically, according to options"""
if OPTIONS.memory_check:
OPTIONS.__DEFINES["__MEMORY_CHECK__"] = ""
Expand Down
4 changes: 2 additions & 2 deletions src/zxbc/zxbparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from math import pi as PI

# typings
from typing import List, NamedTuple, Optional
from typing import NamedTuple, Optional

import src.api.config
import src.api.dataref
Expand Down Expand Up @@ -64,7 +64,7 @@
# Function level entry ID in which scope we are into. If the list
# is empty, we are at global scope
# ----------------------------------------------------------------------
FUNCTION_LEVEL: List[SymbolID] = gl.FUNCTION_LEVEL
FUNCTION_LEVEL: list[SymbolID] = gl.FUNCTION_LEVEL

# ----------------------------------------------------------------------
# Function calls pending to check
Expand Down

0 comments on commit 4379073

Please sign in to comment.