Skip to content

Commit

Permalink
typing: improves typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jose Rodriguez committed Aug 27, 2023
1 parent 78a9e4c commit 55672a6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/api/optimize.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import symtable
from typing import NamedTuple, Optional, Set
from typing import Any, Generator, NamedTuple, Optional, Set

import src.api.check as chk
import src.api.global_ as gl
Expand All @@ -13,7 +13,7 @@
from src.api.constants import CLASS, CONVENTION, SCOPE, TYPE
from src.api.debug import __DEBUG__
from src.api.errmsg import warning_not_used
from src.ast import NodeVisitor
from src.ast import Ast, NodeVisitor
from src.symbols import sym as symbols
from src.symbols.id_ import ref

Expand Down Expand Up @@ -63,7 +63,7 @@ def _visit(self, node: ToVisit):
meth = getattr(self, f"visit_{node.obj.token}", self.generic_visit)
return meth(node.obj)

def generic_visit(self, node: symbols.SYMBOL):
def generic_visit(self, node: Ast) -> Generator[Ast | None, Any, None]:
for i, child in enumerate(node.children):
node.children[i] = yield self.visit(child)

Expand Down
3 changes: 1 addition & 2 deletions src/ast/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ def _visit(self, node):
meth = getattr(self, f"visit_{node.token}", self.generic_visit)
return meth(node)

@staticmethod
def generic_visit(node: Ast):
def generic_visit(self, node: Ast):
raise RuntimeError(f"No visit_{node.token}() method defined")

def filter_inorder(
Expand Down
48 changes: 29 additions & 19 deletions src/ast/tree.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import annotations

import collections.abc
from typing import Iterable, Optional, Union
from typing import Iterable, Optional, Union, Any, Iterator

from src.api.exception import Error

Expand All @@ -27,9 +28,9 @@ class Tree:

__slots__ = "_children", "parent"

def __init__(self):
def __init__(self) -> None:
self._children = ChildrenList(self)
self.parent: Optional["Tree"] = None
self.parent: Optional[Tree] = None

@property
def children(self):
Expand Down Expand Up @@ -66,61 +67,69 @@ def postorder(self):

yield self

def append_child(self, node: "Tree"):
def append_child(self, node: Tree) -> None:
"""Appends the given node to the current children list"""
self.children.append(node)

def prepend_child(self, node: "Tree"):
def prepend_child(self, node: Tree) -> None:
"""Inserts the given node at the beginning of the children list"""
self.children.insert(0, node)


class ChildrenList:
owner: Tree

def __init__(self, node: Tree):
def __init__(self, node: Tree) -> None:
assert isinstance(node, Tree)
self.owner = node # Node having this children
self._children: list[Tree | None] = []

def __getitem__(self, key: Union[int, slice]):
def __getitem__(self, key: int | slice) -> Tree | ChildrenList | None:
if isinstance(key, int):
return self._children[key]

result = ChildrenList(self.owner)
for x in self._children[key]:
result.append(x)

return result

def __setitem__(self, key, value: Optional[Tree]):
def __setitem__(self, key: int, value: Tree | None) -> None:
assert value is None or isinstance(value, Tree)
if value is not None:
value.parent = self.owner
self._children[key] = value

def __delitem__(self, key):
self._children[key].parent = None
def __delitem__(self, key: int) -> None:
child = self._children[key]
if child is not None:
child.parent = None
del self._children[key]

def append(self, value: Tree):
assert isinstance(value, Tree)
value.parent = self.owner
def __iter__(self) -> Iterator[Tree | None]:
return iter(self._children)

def append(self, value: Tree | None) -> None:
if value is not None:
value.parent = self.owner
self._children.append(value)

def insert(self, pos: int, value: Tree):
def insert(self, pos: int, value: Tree) -> None:
assert isinstance(value, Tree)
value.parent = self.owner
self._children.insert(pos, value)

def pop(self, pos: int = -1):
def pop(self, pos: int = -1) -> Tree | None:
result = self._children.pop(pos)
result.parent = None
if result is not None:
result.parent = None

return result

def __len__(self):
def __len__(self) -> int:
return len(self._children)

def __add__(self, other):
def __add__(self, other: Any) -> ChildrenList:
if not isinstance(other, ChildrenList):
assert isinstance(other, collections.abc.Container)

Expand All @@ -129,7 +138,8 @@ def __add__(self, other):
result.append(x)
for x in other:
result.append(x)

return result

def __repr__(self):
def __repr__(self) -> str:
return f"{repr(self.owner)}:{str([repr(x) for x in self._children])}"
1 change: 1 addition & 0 deletions src/zxbc/args_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3
from __future__ import annotations

import os
from typing import TYPE_CHECKING

Expand Down

0 comments on commit 55672a6

Please sign in to comment.