Skip to content

Commit 82e19fc

Browse files
alexfiklinducer
authored andcommitted
feat(typing): fix annotations in pytools.codegen
1 parent f7993ab commit 82e19fc

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

pytools/codegen.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@
3232
.. autofunction:: remove_common_indentation
3333
"""
3434

35-
from typing import Any
35+
from typing import TYPE_CHECKING
36+
37+
38+
if TYPE_CHECKING:
39+
import types
3640

3741

3842
# {{{ code generation
@@ -50,9 +54,15 @@ class CodeGenerator:
5054
.. automethod:: indent
5155
.. automethod:: dedent
5256
"""
57+
58+
preamble: list[str]
59+
code: list[str]
60+
level: int
61+
indent_amount: int
62+
5363
def __init__(self) -> None:
54-
self.preamble: list[str] = []
55-
self.code: list[str] = []
64+
self.preamble = []
65+
self.code = []
5666
self.level = 0
5767
self.indent_amount = 4
5868

@@ -91,17 +101,23 @@ def dedent(self) -> None:
91101
class Indentation:
92102
"""A context manager for indentation for use with :class:`CodeGenerator`.
93103
94-
.. attribute:: generator
104+
.. autoattribute:: generator
95105
.. automethod:: __enter__
96106
.. automethod:: __exit__
97107
"""
108+
109+
generator: CodeGenerator
110+
98111
def __init__(self, generator: CodeGenerator):
99112
self.generator = generator
100113

101114
def __enter__(self) -> None:
102115
self.generator.indent()
103116

104-
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
117+
def __exit__(self,
118+
exc_type: type[BaseException],
119+
exc_val: BaseException | None,
120+
exc_tb: types.TracebackType | None) -> None:
105121
self.generator.dedent()
106122

107123
# }}}
@@ -133,8 +149,8 @@ def remove_common_indentation(code: str, require_leading_newline: bool = True):
133149
while lines[-1].strip() == "":
134150
lines.pop(-1)
135151

152+
base_indent = 0
136153
if lines:
137-
base_indent = 0
138154
while lines[0][base_indent] in " \t":
139155
base_indent += 1
140156

0 commit comments

Comments
 (0)