Skip to content

Commit 996800f

Browse files
committed
use annotations instead of string literals for typing
1 parent 2d181de commit 996800f

5 files changed

Lines changed: 24 additions & 14 deletions

File tree

src/bytecode/bytecode.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import types
24
from abc import abstractmethod
35
from typing import (
@@ -48,7 +50,7 @@ def __init__(self) -> None:
4850
self.freevars: List[str] = []
4951
self._flags: CompilerFlags = CompilerFlags(0)
5052

51-
def _copy_attr_from(self, bytecode: "BaseBytecode") -> None:
53+
def _copy_attr_from(self, bytecode: BaseBytecode) -> None:
5254
self.argcount = bytecode.argcount
5355
self.posonlyargcount = bytecode.posonlyargcount
5456
self.kwonlyargcount = bytecode.kwonlyargcount
@@ -275,7 +277,7 @@ def from_code(
275277
code: types.CodeType,
276278
prune_caches: bool = True,
277279
conserve_exception_block_stackdepth: bool = False,
278-
) -> "Bytecode":
280+
) -> Bytecode:
279281
concrete = _bytecode.ConcreteBytecode.from_code(code)
280282
return concrete.to_bytecode(
281283
prune_caches=prune_caches,
@@ -317,7 +319,7 @@ def to_concrete_bytecode(
317319
self,
318320
compute_jumps_passes: Optional[int] = None,
319321
compute_exception_stack_depths: bool = True,
320-
) -> "_bytecode.ConcreteBytecode":
322+
) -> _bytecode.ConcreteBytecode:
321323
converter = _bytecode._ConvertBytecodeToConcrete(self)
322324
return converter.to_concrete_bytecode(
323325
compute_jumps_passes=compute_jumps_passes,

src/bytecode/cfg.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import types
24
from collections import defaultdict
35
from dataclasses import dataclass
@@ -36,7 +38,7 @@ def __init__(
3638
] = None,
3739
) -> None:
3840
# a BasicBlock object, or None
39-
self.next_block: Optional["BasicBlock"] = None
41+
self.next_block: Optional[BasicBlock] = None
4042
if instructions:
4143
super().__init__(instructions)
4244

@@ -129,7 +131,7 @@ def legalize(self, first_lineno: int) -> int:
129131

130132
return current_lineno
131133

132-
def get_jump(self) -> Optional["BasicBlock"]:
134+
def get_jump(self) -> Optional[BasicBlock]:
133135
if not self:
134136
return None
135137

@@ -243,7 +245,7 @@ def __init__(
243245
self.pending_try_begin = pending_try_begin
244246
self._current_try_begin = pending_try_begin
245247

246-
def run(self) -> Generator[Union["_StackSizeComputer", int], int, None]:
248+
def run(self) -> Generator[Union[_StackSizeComputer, int], int, None]:
247249
"""Iterate over the block instructions to compute stack usage."""
248250
# Blocks are not hashable but in this particular context we know we won't be
249251
# modifying blocks in place so we can safely use their id as hash rather than
@@ -430,7 +432,7 @@ def _update_size(self, pre_delta: int, post_delta: int) -> None:
430432

431433
def _compute_exception_handler_stack_usage(
432434
self, block: BasicBlock, push_lasti: bool
433-
) -> Generator[Union["_StackSizeComputer", int], int, None]:
435+
) -> Generator[Union[_StackSizeComputer, int], int, None]:
434436
b_id = id(block)
435437
if self.minsize < self.common.exception_block_startsize[b_id]:
436438
block_size = yield _StackSizeComputer(
@@ -741,7 +743,7 @@ def get_dead_blocks(self) -> List[BasicBlock]:
741743
return [b for b in self if id(b) not in seen_block_ids]
742744

743745
@staticmethod
744-
def from_bytecode(bytecode: _bytecode.Bytecode) -> "ControlFlowGraph":
746+
def from_bytecode(bytecode: _bytecode.Bytecode) -> ControlFlowGraph:
745747
# label => instruction index
746748
label_to_block_index = {}
747749
jumps = []

src/bytecode/concrete.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import dis
24
import inspect
35
import itertools
@@ -332,7 +334,7 @@ def __eq__(self, other: Any) -> bool:
332334
@staticmethod
333335
def from_code(
334336
code: types.CodeType, *, extended_arg: bool = False
335-
) -> "ConcreteBytecode":
337+
) -> ConcreteBytecode:
336338
instructions: MutableSequence[Union[SetLineno, ConcreteInstr]] = []
337339
for i in dis.get_instructions(code, show_caches=True):
338340
loc = InstrLocation.from_positions(i.positions) if i.positions else None

src/bytecode/instr.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import dis
24
import enum
35
import opcode as _opcode
@@ -267,7 +269,7 @@ class CommonConstant(enum.IntEnum):
267269
# This make type checking happy but means it won't catch attempt to manipulate an unset
268270
# statically. We would need guard on object attribute narrowed down through methods
269271
class _UNSET(int):
270-
instance: Optional["_UNSET"] = None
272+
instance: Optional[_UNSET] = None
271273

272274
def __new__(cls):
273275
if cls.instance is None:
@@ -611,7 +613,7 @@ def __init__(
611613
)
612614

613615
@classmethod
614-
def from_positions(cls, position: "dis.Positions") -> "InstrLocation": # type: ignore
616+
def from_positions(cls, position: dis.Positions) -> InstrLocation: # type: ignore
615617
return InstrLocation(
616618
position.lineno,
617619
position.end_lineno,
@@ -654,7 +656,7 @@ def __init__(
654656
self.push_lasti: bool = push_lasti
655657
self.stack_depth: int | _UNSET = stack_depth
656658

657-
def copy(self) -> "TryBegin":
659+
def copy(self) -> TryBegin:
658660
return TryBegin(self.target, self.push_lasti, self.stack_depth)
659661

660662

@@ -664,7 +666,7 @@ class TryEnd:
664666
def __init__(self, entry: TryBegin) -> None:
665667
self.entry: TryBegin = entry
666668

667-
def copy(self) -> "TryEnd":
669+
def copy(self) -> TryEnd:
668670
return TryEnd(self.entry)
669671

670672

tests/frameworks/module.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import abc
24
import sys
35
import typing as t
@@ -124,7 +126,7 @@ class BaseModuleWatchdog:
124126
Invokes ``after_import`` every time a new module is imported.
125127
"""
126128

127-
_instance: t.Optional["BaseModuleWatchdog"] = None
129+
_instance: t.Optional[BaseModuleWatchdog] = None
128130

129131
def __init__(self) -> None:
130132
self._finding: t.Set[str] = set()

0 commit comments

Comments
 (0)