Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes most mypy errors and run it on CI #82

Merged
merged 15 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,26 @@ jobs:
shell: bash -l {0}
run: |
coverage report

mypy:
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 mypy pyyaml graphviz
pip install types-PyYAML

- name: Run mypy
shell: bash -l {0}
run: |
mypy .
kc611 marked this conversation as resolved.
Show resolved Hide resolved
11 changes: 11 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Global options:

[mypy]
warn_unused_configs = True
follow_imports = silent
show_error_context = True
namespace_packages = True
files =
numba_rvsdg
exclude =
numba_rvsdg/tests|conf.py
32 changes: 16 additions & 16 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 dataclasses import dataclass, replace, field

from numba_rvsdg.core.utils import _next_inst_offset

Expand All @@ -25,9 +25,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 @@ -57,7 +57,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 @@ -152,9 +152,9 @@ class PythonBytecodeBlock(BasicBlock):
The bytecode offset immediately after the last bytecode of the block.
"""

begin: int = None
begin: int = 0

end: int = None
end: int = 0
kc611 marked this conversation as resolved.
Show resolved Hide resolved

def get_instructions(
self, bcmap: Dict[int, dis.Instruction]
Expand Down Expand Up @@ -233,8 +233,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 @@ -247,7 +247,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 @@ -265,8 +265,8 @@ 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":
"""Replaces jump targets of this block by the given tuple.
Expand Down Expand Up @@ -359,11 +359,11 @@ 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: str = ""
parent_region: "RegionBlock" = None # type: ignore
header: str = ""
subregion: "SCFG" = None # type: ignore # noqa
exiting: str = ""

def replace_header(self, new_header):
"""This method performs a inplace replacement of the header block.
Expand Down
62 changes: 32 additions & 30 deletions numba_rvsdg/core/datastructures/scfg.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dis
import yaml
from textwrap import dedent
from typing import Set, Tuple, Dict, List, Iterator
from typing import Set, Tuple, Dict, List, Iterator, Optional
from dataclasses import dataclass, field
from collections import deque
from collections.abc import Mapping
Expand Down Expand Up @@ -323,7 +323,7 @@ def __iter__(self):

def find_headers_and_entries(
self, subgraph: Set[str]
) -> Tuple[Set[str], Set[str]]:
) -> Tuple[List[str], List[str]]:
"""Finds entries and headers in a given subgraph.

Entries are blocks outside the subgraph that have an edge pointing to
Expand Down Expand Up @@ -370,7 +370,7 @@ def find_headers_and_entries(

def find_exiting_and_exits(
self, subgraph: Set[str]
) -> Tuple[Set[str], Set[str]]:
) -> Tuple[List[str], List[str]]:
"""Finds exiting and exit blocks in a given subgraph.

Existing blocks are blocks inside the subgraph that have edges to
Expand Down Expand Up @@ -466,9 +466,9 @@ def remove_blocks(self, names: Set[str]):
def insert_block(
self,
new_name: str,
predecessors: Set[str],
successors: Set[str],
block_type: SyntheticBlock,
predecessors: List[str],
successors: List[str],
block_type: type[SyntheticBlock],
):
"""Inserts a new synthetic block into the SCFG
between the given successors and predecessors.
Expand All @@ -485,19 +485,19 @@ def insert_block(
----------
new_name: str
The name of the newly created block.
predecessors: Set[str]
The set of names of BasicBlock that act as predecessors
predecessors: List[str]
The list of names of BasicBlock that act as predecessors
for the block to be inserted.
successors: Set[str]
The set of names of BasicBlock that act as successors
successors: List[str]
The list of names of BasicBlock that act as successors
for the block to be inserted.
block_type: SyntheticBlock
The type/class of the newly created block.
"""
# TODO: needs a diagram and documentaion
# initialize new block
new_block = block_type(
name=new_name, _jump_targets=successors, backedges=set()
name=new_name, _jump_targets=tuple(successors), backedges=tuple()
)
# add block to self
self.add_block(new_block)
Expand All @@ -520,8 +520,8 @@ def insert_block(
def insert_SyntheticExit(
self,
new_name: str,
predecessors: Set[str],
successors: Set[str],
predecessors: List[str],
successors: List[str],
):
"""Inserts a synthetic exit block into the SCFG.
Parameters same as insert_block method.
Expand All @@ -535,8 +535,8 @@ def insert_SyntheticExit(
def insert_SyntheticTail(
self,
new_name: str,
predecessors: Set[str],
successors: Set[str],
predecessors: List[str],
successors: List[str],
):
"""Inserts a synthetic tail block into the SCFG.
Parameters same as insert_block method.
Expand All @@ -550,8 +550,8 @@ def insert_SyntheticTail(
def insert_SyntheticReturn(
self,
new_name: str,
predecessors: Set[str],
successors: Set[str],
predecessors: List[str],
successors: List[str],
):
"""Inserts a synthetic return block into the SCFG.
Parameters same as insert_block method.
Expand All @@ -565,8 +565,8 @@ def insert_SyntheticReturn(
def insert_SyntheticFill(
self,
new_name: str,
predecessors: Set[str],
successors: Set[str],
predecessors: List[str],
successors: List[str],
):
"""Inserts a synthetic fill block into the SCFG.
Parameters same as insert_block method.
Expand All @@ -578,7 +578,7 @@ def insert_SyntheticFill(
self.insert_block(new_name, predecessors, successors, SyntheticFill)

def insert_block_and_control_blocks(
self, new_name: str, predecessors: Set[str], successors: Set[str]
self, new_name: str, predecessors: List[str], successors: List[str]
):
"""Inserts a new block along with control blocks into the SCFG.
This method is used for branching assignments.
Expand Down Expand Up @@ -633,7 +633,7 @@ def insert_block_and_control_blocks(
new_block = SyntheticHead(
name=new_name,
_jump_targets=tuple(successors),
backedges=set(),
backedges=tuple(),
variable=branch_variable,
branch_value_table=branch_value_table,
)
Expand All @@ -659,15 +659,15 @@ def join_returns(self):
return_solo_name, return_nodes, tuple()
)

def join_tails_and_exits(self, tails: Set[str], exits: Set[str]):
def join_tails_and_exits(self, tails: List[str], exits: List[str]):
"""Joins the tails and exits of the SCFG.

Parameters
----------
tails: Set[str]
The set of names of BasicBlock that act as tails in the SCFG.
exits: Set[str]
The set of names of BasicBlock that act as exits in the SCFG.
tails: List[str]
The list of names of BasicBlock that act as tails in the SCFG.
exits: List[str]
The list of names of BasicBlock that act as exits in the SCFG.

Return
------
Expand Down Expand Up @@ -709,7 +709,7 @@ def join_tails_and_exits(self, tails: Set[str], exits: Set[str]):
block_names.SYNTH_EXIT
)
self.insert_SyntheticTail(solo_tail_name, tails, exits)
self.insert_SyntheticExit(solo_exit_name, {solo_tail_name}, exits)
self.insert_SyntheticExit(solo_exit_name, [solo_tail_name], exits)
return solo_tail_name, solo_exit_name

@staticmethod
Expand Down Expand Up @@ -855,7 +855,7 @@ def to_dict(self):
graph_dict[key] = curr_dict
return graph_dict

def view(self, name: str = None):
def view(self, name: Optional[str] = None):
"""View the current SCFG as a external PDF file.

This method internally creates a SCFGRenderer corresponding to
Expand Down Expand Up @@ -934,7 +934,7 @@ class ConcealedRegionView(AbstractGraphView):
is based on.
"""

scfg: SCFG = None
scfg: SCFG

def __init__(self, scfg):
"""Initializes the ConcealedRegionView with the given SCFG.
Expand Down Expand Up @@ -973,7 +973,9 @@ def __iter__(self):
"""
return self.region_view_iterator()

def region_view_iterator(self, head: str = None) -> Iterator[str]:
def region_view_iterator(
self, head: Optional[str] = None
) -> Iterator[str]:
"""Region View Iterator.

This iterator is region aware, which means that regions are "concealed"
Expand Down
Loading