Skip to content

Commit

Permalink
Merge pull request #82 from guilhermeleobas/guilhermeleobas/mypy
Browse files Browse the repository at this point in the history
Fixes most mypy errors and run it on CI
  • Loading branch information
esc authored Aug 1, 2023
2 parents b10fb94 + 97bf2b5 commit cd671f0
Show file tree
Hide file tree
Showing 21 changed files with 353 additions and 200 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,25 @@ jobs:
shell: bash -l {0}
run: |
coverage report
pre-commit-hook:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup Miniconda
uses: conda-incubator/setup-miniconda@v2
with:
auto-update-conda: true
auto-activate-base: false

- name: Install dependencies
shell: bash -l {0}
run: |
conda install python=3.11 pre-commit pyyaml graphviz
- name: Run pre-commit
shell: bash -l {0}
run: |
pre-commit run -a
3 changes: 3 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,8 @@ repos:
hooks:
- id: mypy
additional_dependencies:
- types-pyyaml
- types-filelock
- types-setuptools
entry: mypy

12 changes: 12 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Global options:

[mypy]
warn_unused_configs = True
follow_imports = silent
show_error_context = True
namespace_packages = True
strict = True
files =
numba_rvsdg
exclude =
numba_rvsdg/tests|conf.py
48 changes: 26 additions & 22 deletions numba_rvsdg/core/datastructures/basic_block.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dis
from typing import Tuple, Dict, List
from dataclasses import dataclass, replace
from typing import Tuple, Dict, List, Optional
from dataclasses import dataclass, replace, field

from numba_rvsdg.core.utils import _next_inst_offset
from numba_rvsdg.core.datastructures import block_names
Expand All @@ -26,9 +26,9 @@ class BasicBlock:

name: str

_jump_targets: Tuple[str] = tuple()
_jump_targets: Tuple[str, ...] = tuple()

backedges: Tuple[str] = tuple()
backedges: Tuple[str, ...] = tuple()

@property
def is_exiting(self) -> bool:
Expand Down Expand Up @@ -58,7 +58,7 @@ def fallthrough(self) -> bool:
return len(self._jump_targets) == 1

@property
def jump_targets(self) -> Tuple[str]:
def jump_targets(self) -> Tuple[str, ...]:
"""Retrieves the jump targets for this block,
excluding any jump targets that are also backedges.
Expand Down Expand Up @@ -94,7 +94,9 @@ def declare_backedge(self, target: str) -> "BasicBlock":
return replace(self, backedges=(target,))
return self

def replace_jump_targets(self, jump_targets: Tuple) -> "BasicBlock":
def replace_jump_targets(
self, jump_targets: Tuple[str, ...]
) -> "BasicBlock":
"""Replaces jump targets of this block by the given tuple.
This method replaces the jump targets of the current BasicBlock.
Expand All @@ -118,7 +120,7 @@ def replace_jump_targets(self, jump_targets: Tuple) -> "BasicBlock":
"""
return replace(self, _jump_targets=jump_targets)

def replace_backedges(self, backedges: Tuple) -> "BasicBlock":
def replace_backedges(self, backedges: Tuple[str, ...]) -> "BasicBlock":
"""Replaces back edges of this block by the given tuple.
This method replaces the back edges of the current BasicBlock.
Expand Down Expand Up @@ -153,9 +155,9 @@ class PythonBytecodeBlock(BasicBlock):
The bytecode offset immediately after the last bytecode of the block.
"""

begin: int = None
begin: int = -1

end: int = None
end: int = -1

def get_instructions(
self, bcmap: Dict[int, dis.Instruction]
Expand Down Expand Up @@ -234,8 +236,8 @@ class SyntheticFill(SyntheticBlock):

@dataclass(frozen=True)
class SyntheticAssignment(SyntheticBlock):
"""The SyntheticAssignment class represents a artificially added assignment block
in a structured control flow graph (SCFG).
"""The SyntheticAssignment class represents a artificially added
assignment block in a structured control flow graph (SCFG).
This block is responsible for giving variables their values,
once the respective block is executed.
Expand All @@ -248,7 +250,7 @@ class SyntheticAssignment(SyntheticBlock):
the block is executed.
"""

variable_assignment: dict = None
variable_assignment: Dict[str, int] = field(default_factory=lambda: {})


@dataclass(frozen=True)
Expand All @@ -266,10 +268,12 @@ class SyntheticBranch(SyntheticBlock):
to be executed on the basis of that value.
"""

variable: str = None
branch_value_table: dict = None
variable: str = ""
branch_value_table: Dict[int, str] = field(default_factory=lambda: {})

def replace_jump_targets(self, jump_targets: Tuple) -> "BasicBlock":
def replace_jump_targets(
self, jump_targets: Tuple[str, ...]
) -> "BasicBlock":
"""Replaces jump targets of this block by the given tuple.
This method replaces the jump targets of the current BasicBlock.
Expand Down Expand Up @@ -360,13 +364,13 @@ class RegionBlock(BasicBlock):
The exiting node of the region.
"""

kind: str = None
parent_region: "RegionBlock" = None
header: str = None
subregion: "SCFG" = None # noqa
exiting: str = None
kind: Optional[str] = None
parent_region: Optional["RegionBlock"] = None
header: Optional[str] = None
subregion: Optional["SCFG"] = None # type: ignore # noqa
exiting: Optional[str] = None

def replace_header(self, new_header):
def replace_header(self, new_header: str) -> None:
"""This method performs a inplace replacement of the header block.
Parameters
Expand All @@ -376,7 +380,7 @@ def replace_header(self, new_header):
"""
object.__setattr__(self, "header", new_header)

def replace_exiting(self, new_exiting):
def replace_exiting(self, new_exiting: str) -> None:
"""This method performs a inplace replacement of the header block.
Parameters
Expand Down
18 changes: 10 additions & 8 deletions numba_rvsdg/core/datastructures/byte_flow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dis
from copy import deepcopy
from dataclasses import dataclass
from typing import Generator, Callable

from numba_rvsdg.core.datastructures.scfg import SCFG
from numba_rvsdg.core.datastructures.basic_block import RegionBlock
Expand Down Expand Up @@ -30,10 +31,10 @@ class ByteFlow:
"""

bc: dis.Bytecode
scfg: "SCFG"
scfg: SCFG

@staticmethod
def from_bytecode(code) -> "ByteFlow":
def from_bytecode(code: Callable) -> "ByteFlow": # type: ignore
"""Creates a ByteFlow object from the given python
function.
Expand All @@ -54,13 +55,13 @@ def from_bytecode(code) -> "ByteFlow":
The resulting ByteFlow object.
"""
bc = dis.Bytecode(code)
_logger.debug("Bytecode\n%s", _LogWrap(lambda: bc.dis()))
_logger.debug("Bytecode\n%s", _LogWrap(lambda: bc.dis())) # type: ignore # noqa E501

flowinfo = FlowInfo.from_bytecode(bc)
scfg = flowinfo.build_basicblocks()
return ByteFlow(bc=bc, scfg=scfg)

def _join_returns(self):
def _join_returns(self) -> "ByteFlow":
"""Joins the return blocks within the corresponding SCFG.
This method creates a deep copy of the SCFG and performs
Expand All @@ -76,7 +77,7 @@ def _join_returns(self):
scfg.join_returns()
return ByteFlow(bc=self.bc, scfg=scfg)

def _restructure_loop(self):
def _restructure_loop(self) -> "ByteFlow":
"""Restructures the loops within the corresponding SCFG.
Creates a deep copy of the SCFG and performs the operation to
Expand All @@ -97,7 +98,7 @@ def _restructure_loop(self):
restructure_loop(region)
return ByteFlow(bc=self.bc, scfg=scfg)

def _restructure_branch(self):
def _restructure_branch(self) -> "ByteFlow":
"""Restructures the branches within the corresponding SCFG.
Creates a deep copy of the SCFG and performs the operation to
Expand All @@ -117,7 +118,7 @@ def _restructure_branch(self):
restructure_branch(region)
return ByteFlow(bc=self.bc, scfg=scfg)

def restructure(self):
def restructure(self) -> "ByteFlow":
"""Applies join_returns, restructure_loop and restructure_branch
in the respective order on the SCFG.
Expand Down Expand Up @@ -146,8 +147,9 @@ def restructure(self):
return ByteFlow(bc=self.bc, scfg=scfg)


def _iter_subregions(scfg: "SCFG"):
def _iter_subregions(scfg: SCFG) -> Generator[RegionBlock, SCFG, None]:
for node in scfg.graph.values():
if isinstance(node, RegionBlock):
yield node
assert node.subregion is not None
yield from _iter_subregions(node.subregion)
8 changes: 5 additions & 3 deletions numba_rvsdg/core/datastructures/flow_info.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dis

from typing import Set, Tuple, Dict, Sequence
from typing import Set, Tuple, Dict, Sequence, Optional
from dataclasses import dataclass, field

from numba_rvsdg.core.datastructures.basic_block import PythonBytecodeBlock
Expand Down Expand Up @@ -36,7 +36,7 @@ class FlowInfo:

last_offset: int = field(default=0)

def _add_jump_inst(self, offset: int, targets: Sequence[int]):
def _add_jump_inst(self, offset: int, targets: Sequence[int]) -> None:
"""Internal method to add a jump instruction to the FlowInfo.
This method adds the target offsets of the jump instruction
Expand Down Expand Up @@ -93,7 +93,9 @@ def from_bytecode(bc: dis.Bytecode) -> "FlowInfo":
flowinfo.last_offset = inst.offset
return flowinfo

def build_basicblocks(self: "FlowInfo", end_offset=None) -> "SCFG":
def build_basicblocks(
self: "FlowInfo", end_offset: Optional[int] = None
) -> "SCFG":
"""Builds a graph of basic blocks based on the flow information.
It creates a structured control flow graph (SCFG) object, assigns
Expand Down
Loading

0 comments on commit cd671f0

Please sign in to comment.