Skip to content

Commit 1d213ab

Browse files
author
Jose Rodriguez
committed
typing: improves typing
1 parent 78a9e4c commit 1d213ab

4 files changed

Lines changed: 34 additions & 24 deletions

File tree

src/api/optimize.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33
import symtable
4-
from typing import NamedTuple, Optional, Set
4+
from typing import Any, Generator, NamedTuple, Optional, Set
55

66
import src.api.check as chk
77
import src.api.global_ as gl
@@ -13,7 +13,7 @@
1313
from src.api.constants import CLASS, CONVENTION, SCOPE, TYPE
1414
from src.api.debug import __DEBUG__
1515
from src.api.errmsg import warning_not_used
16-
from src.ast import NodeVisitor
16+
from src.ast import Ast, NodeVisitor
1717
from src.symbols import sym as symbols
1818
from src.symbols.id_ import ref
1919

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

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

src/ast/ast.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ def _visit(self, node):
5454
meth = getattr(self, f"visit_{node.token}", self.generic_visit)
5555
return meth(node)
5656

57-
@staticmethod
58-
def generic_visit(node: Ast):
57+
def generic_visit(self, node: Ast):
5958
raise RuntimeError(f"No visit_{node.token}() method defined")
6059

6160
def filter_inorder(

src/ast/tree.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
3+
from __future__ import annotations
34

45
import collections.abc
5-
from typing import Iterable, Optional, Union
6+
from typing import Any, Iterable, Iterator, Optional
67

78
from src.api.exception import Error
89

@@ -27,9 +28,9 @@ class Tree:
2728

2829
__slots__ = "_children", "parent"
2930

30-
def __init__(self):
31+
def __init__(self) -> None:
3132
self._children = ChildrenList(self)
32-
self.parent: Optional["Tree"] = None
33+
self.parent: Optional[Tree] = None
3334

3435
@property
3536
def children(self):
@@ -66,61 +67,69 @@ def postorder(self):
6667

6768
yield self
6869

69-
def append_child(self, node: "Tree"):
70+
def append_child(self, node: Tree) -> None:
7071
"""Appends the given node to the current children list"""
7172
self.children.append(node)
7273

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

7778

7879
class ChildrenList:
7980
owner: Tree
8081

81-
def __init__(self, node: Tree):
82+
def __init__(self, node: Tree) -> None:
8283
assert isinstance(node, Tree)
8384
self.owner = node # Node having this children
8485
self._children: list[Tree | None] = []
8586

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

9091
result = ChildrenList(self.owner)
9192
for x in self._children[key]:
9293
result.append(x)
94+
9395
return result
9496

95-
def __setitem__(self, key, value: Optional[Tree]):
97+
def __setitem__(self, key: int, value: Tree | None) -> None:
9698
assert value is None or isinstance(value, Tree)
9799
if value is not None:
98100
value.parent = self.owner
99101
self._children[key] = value
100102

101-
def __delitem__(self, key):
102-
self._children[key].parent = None
103+
def __delitem__(self, key: int) -> None:
104+
child = self._children[key]
105+
if child is not None:
106+
child.parent = None
103107
del self._children[key]
104108

105-
def append(self, value: Tree):
106-
assert isinstance(value, Tree)
107-
value.parent = self.owner
109+
def __iter__(self) -> Iterator[Tree | None]:
110+
return iter(self._children)
111+
112+
def append(self, value: Tree | None) -> None:
113+
if value is not None:
114+
value.parent = self.owner
108115
self._children.append(value)
109116

110-
def insert(self, pos: int, value: Tree):
117+
def insert(self, pos: int, value: Tree) -> None:
111118
assert isinstance(value, Tree)
112119
value.parent = self.owner
113120
self._children.insert(pos, value)
114121

115-
def pop(self, pos: int = -1):
122+
def pop(self, pos: int = -1) -> Tree | None:
116123
result = self._children.pop(pos)
117-
result.parent = None
124+
if result is not None:
125+
result.parent = None
126+
118127
return result
119128

120-
def __len__(self):
129+
def __len__(self) -> int:
121130
return len(self._children)
122131

123-
def __add__(self, other):
132+
def __add__(self, other: Any) -> ChildrenList:
124133
if not isinstance(other, ChildrenList):
125134
assert isinstance(other, collections.abc.Container)
126135

@@ -129,7 +138,8 @@ def __add__(self, other):
129138
result.append(x)
130139
for x in other:
131140
result.append(x)
141+
132142
return result
133143

134-
def __repr__(self):
144+
def __repr__(self) -> str:
135145
return f"{repr(self.owner)}:{str([repr(x) for x in self._children])}"

src/zxbc/args_config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python3
22
from __future__ import annotations
3+
34
import os
45
from typing import TYPE_CHECKING
56

0 commit comments

Comments
 (0)