Skip to content

Commit

Permalink
Fix type hints issues and add Mypy to the CI (#195)
Browse files Browse the repository at this point in the history
* Fix type hint issues and add Mypy to the CI
  • Loading branch information
montyly authored Jun 29, 2021
1 parent 4a2574d commit bd638b2
Show file tree
Hide file tree
Showing 28 changed files with 277 additions and 180 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
name: Mypy

defaults:
run:
# To load bashrc
shell: bash -ieo pipefail {0}

on:
pull_request:
branches: [master, dev]

jobs:
build:
name: Mypy
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v2

- name: Set up Python 3.6
uses: actions/setup-python@v2
with:
python-version: 3.6

- name: Install dependencies
run: |
pip install .
pip install types-setuptools
mkdir -p .github/linters
cp mypy.ini .github/linters
- name: Mypy
uses: docker://github/super-linter:v3
if: always()
env:
# run linter on everything to catch preexisting problems
VALIDATE_ALL_CODEBASE: true
DEFAULT_BRANCH: master
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Run only black
VALIDATE_PYTHON_MYPY: true
PYTHON_MYPY_CONFIG_FILE: mypy.ini
4 changes: 3 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ To run them locally:

- `pylint crytic_compile --rcfile pyproject.toml`
- `black crytic_compile --config pyproject.toml`
- `mypy crytic_compile --config mypy.ini`

We use pylint `2.8.2` black `20.8b1`.

We use pylint `2.8.2`, black `20.8b1` and mypy `0.812`.

## Development Environment
Instructions for installing a development version of crytic-compile can be found in our [wiki](https://github.com/crytic/crytic-compile/wiki/Developer-installation).
16 changes: 11 additions & 5 deletions crytic_compile/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging
import os
import sys
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any, Optional

from pkg_resources import require

Expand All @@ -24,7 +24,7 @@
LOGGER.setLevel(logging.INFO)


def parse_args():
def parse_args() -> argparse.Namespace:
"""
Parse the arguments
Expand Down Expand Up @@ -144,13 +144,19 @@ class ShowPlatforms(argparse.Action): # pylint: disable=too-few-public-methods
See --supported-platforms
"""

def __call__(self, parser, args, values, option_string=None):
def __call__(
self,
parser: argparse.ArgumentParser,
args: Any,
values: Any,
option_string: Optional[str] = None,
) -> None:
platforms = get_platforms()
LOGGER.info("\n" + "\n".join([f"- {x.NAME}: {x.PROJECT_URL}" for x in platforms]))
parser.exit()


def _print_filenames(compilation: "CryticCompile"):
def _print_filenames(compilation: "CryticCompile") -> None:
printed_filenames = set()
for compilation_id, compilation_unit in compilation.compilation_units.items():
print(
Expand All @@ -167,7 +173,7 @@ def _print_filenames(compilation: "CryticCompile"):
printed_filenames.add(unique_id)


def main():
def main() -> None:
"""
Main function run from the cli
Expand Down
24 changes: 12 additions & 12 deletions crytic_compile/compilation_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(self, crytic_compile: "CryticCompile", unique_id: str):
self._unique_id = unique_id

@property
def unique_id(self):
def unique_id(self) -> str:
return self._unique_id

@property
Expand All @@ -71,7 +71,7 @@ def crytic_compile(self) -> "CryticCompile":
###################################################################################

@property
def natspec(self):
def natspec(self) -> Dict[str, Natspec]:
"""
Return the natspec of the contractse
Expand Down Expand Up @@ -166,7 +166,7 @@ def contracts_names(self) -> Set[str]:
return self._contracts_name

@contracts_names.setter
def contracts_names(self, names: Set[str]):
def contracts_names(self, names: Set[str]) -> None:
"""
Return the contracts names
Expand Down Expand Up @@ -232,7 +232,7 @@ def asts(self) -> Dict:
return self._asts

@asts.setter
def asts(self, value: Dict):
def asts(self, value: Dict) -> None:
self._asts = value

def ast(self, path: str) -> Union[Dict, None]:
Expand Down Expand Up @@ -266,7 +266,7 @@ def bytecodes_runtime(self) -> Dict[str, str]:
return self._runtime_bytecodes

@bytecodes_runtime.setter
def bytecodes_runtime(self, bytecodes: Dict[str, str]):
def bytecodes_runtime(self, bytecodes: Dict[str, str]) -> None:
"""
Return the init bytecodes
Expand All @@ -284,7 +284,7 @@ def bytecodes_init(self) -> Dict[str, str]:
return self._init_bytecodes

@bytecodes_init.setter
def bytecodes_init(self, bytecodes: Dict[str, str]):
def bytecodes_init(self, bytecodes: Dict[str, str]) -> None:
"""
Return the init bytecodes
Expand Down Expand Up @@ -521,7 +521,7 @@ def libraries_names(self, name: str) -> List[str]:
self._libraries[name] = [lib for lib in libraires if lib]
return [name for (name, pattern) in self._libraries[name]]

def libraries_names_and_patterns(self, name):
def libraries_names_and_patterns(self, name: str) -> List[Tuple[str, str]]:
"""
Return the name of the libraries used by the contract
Expand Down Expand Up @@ -552,7 +552,7 @@ def _update_bytecode_with_libraries(
if library_found in libraries:
bytecode = re.sub(
re.escape(library_found),
"{:040x}".format(libraries[library_found]),
"{:040x}".format(int(libraries[library_found])),
bytecode,
)
return bytecode
Expand All @@ -575,7 +575,7 @@ def hashes(self, name: str) -> Dict[str, int]:
self._compute_hashes(name)
return self._hashes[name]

def _compute_hashes(self, name: str):
def _compute_hashes(self, name: str) -> None:
self._hashes[name] = {}
for sig in self.abi(name):
if "type" in sig:
Expand Down Expand Up @@ -604,7 +604,7 @@ def events_topics(self, name: str) -> Dict[str, Tuple[int, List[bool]]]:
self._compute_topics_events(name)
return self._events[name]

def _compute_topics_events(self, name: str):
def _compute_topics_events(self, name: str) -> None:
self._events[name] = {}
for sig in self.abi(name):
if "type" in sig:
Expand All @@ -625,7 +625,7 @@ def _compute_topics_events(self, name: str):
###################################################################################
###################################################################################

def remove_metadata(self):
def remove_metadata(self) -> None:
"""
Init bytecode contains metadata that needs to be removed
see
Expand Down Expand Up @@ -658,5 +658,5 @@ def compiler_version(self) -> "CompilerVersion":
return self._compiler_version

@compiler_version.setter
def compiler_version(self, compiler):
def compiler_version(self, compiler: CompilerVersion) -> None:
self._compiler_version = compiler
18 changes: 9 additions & 9 deletions crytic_compile/crytic_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def filenames(self) -> Set[Filename]:
return self._filenames

@filenames.setter
def filenames(self, all_filenames: Set[Filename]):
def filenames(self, all_filenames: Set[Filename]) -> None:
self._filenames = all_filenames

def filename_lookup(self, filename: str) -> Filename:
Expand Down Expand Up @@ -210,10 +210,10 @@ def working_dir(self) -> Path:
return self._working_dir

@working_dir.setter
def working_dir(self, path: Path):
def working_dir(self, path: Path) -> None:
self._working_dir = path

def _get_cached_offset_to_line(self, file: Filename):
def _get_cached_offset_to_line(self, file: Filename) -> None:
if file not in self._cached_line_to_code:
self._get_cached_line_to_code(file)

Expand Down Expand Up @@ -243,7 +243,7 @@ def get_global_offset_from_line(self, file: Filename, line: int) -> int:

return self._cached_line_to_offset[file][line]

def _get_cached_line_to_code(self, file: Filename):
def _get_cached_line_to_code(self, file: Filename) -> None:
source_code = self.src_content[file.absolute]
source_code_encoded = source_code.encode("utf-8")
source_code_list = source_code_encoded.splitlines(True)
Expand Down Expand Up @@ -280,7 +280,7 @@ def src_content(self) -> Dict[str, str]:
return self._src_content

@src_content.setter
def src_content(self, src):
def src_content(self, src: Dict) -> None:
"""
Set the src_content
Expand Down Expand Up @@ -343,7 +343,7 @@ def bytecode_only(self) -> bool:
return self._bytecode_only

@bytecode_only.setter
def bytecode_only(self, bytecode):
def bytecode_only(self, bytecode: bool) -> None:
self._bytecode_only = bytecode

# endregion
Expand Down Expand Up @@ -419,7 +419,7 @@ def _init_platform(self, target: str, **kwargs: str) -> AbstractPlatform:

return platform

def _compile(self, **kwargs: str):
def _compile(self, **kwargs: str) -> None:
custom_build: Union[None, str] = kwargs.get("compile_custom_build", None)
if custom_build:
self._run_custom_build(custom_build)
Expand All @@ -433,7 +433,7 @@ def _compile(self, **kwargs: str):
compilation_unit.remove_metadata()

@staticmethod
def _run_custom_build(custom_build: str):
def _run_custom_build(custom_build: str) -> None:
cmd = custom_build.split(" ")

with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as process:
Expand Down Expand Up @@ -462,7 +462,7 @@ def package_name(self) -> Optional[str]:
return self._package

@package_name.setter
def package_name(self, name: Optional[str]):
def package_name(self, name: Optional[str]) -> None:
self._package = name


Expand Down
Loading

0 comments on commit bd638b2

Please sign in to comment.