Skip to content

Commit

Permalink
partial Merge bitcoin#20451: lint: run mypy over contrib/devtools
Browse files Browse the repository at this point in the history
1ef2138 lint: run mypy over contrib/devtools (fanquake)

Pull request description:

  wumpus mentioned on IRC that we don't currently run `mypy` over the `contrib/devtools` directory, and that it would likely be worthwhile given bitcoin#20434. This just adds that dir to the linter, as well as some missing annotations to fix existing errors. Note that now we require Python 3.6 we can make use of variable annotations.

  master (patched to check contrib devtools):
  ```bash
  test/lint/lint-python.sh
  contrib/devtools/symbol-check.py:154: error: Incompatible types in assignment (expression has type "List[str]", variable has type "str")
  contrib/devtools/circular-dependencies.py:35: error: Need type annotation for 'deps' (hint: "deps: Dict[<type>, <type>] = ...")
  contrib/devtools/circular-dependencies.py:67: error: Need type annotation for 'closure' (hint: "closure: Dict[<type>, <type>] = ...")
  Found 4 errors in 3 files (checked 187 source files)
  ```

  I haven't quite gone as far as to add annotations like
  ```python
  CHECKS: Dict[str, List[Tuple[str, Callable[[Any], bool]]]] = {...
  ```
  to `symbol-check.py`.

ACKs for top commit:
  laanwj:
    ACK 1ef2138

Tree-SHA512: a58c2ece588c640289dc1d35dad5b1b8732788272daa0965d6bf44ee8a7f7c8e8585f94d233ac41c84b9ffcfc97841a00fe2c9acba41f58fd164f01de4b6512b
  • Loading branch information
laanwj authored and knst committed Mar 26, 2024
1 parent 3231ad2 commit ab430bd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
7 changes: 4 additions & 3 deletions contrib/devtools/circular-dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys
import re
from multiprocess import Pool
from typing import Dict, List, Set

MAPPING = {
'core_read.cpp': 'core_io.cpp',
Expand Down Expand Up @@ -34,7 +35,7 @@ def module_name(path):

if __name__=="__main__":
files = dict()
deps = dict()
deps: Dict[str, Set[str]] = dict()

RE = re.compile("^#include <(.*)>")

Expand All @@ -48,7 +49,7 @@ def handle_module(arg):

def handle_module2(module):
# Build the transitive closure of dependencies of module
closure = dict()
closure: Dict[str, List[str]] = dict()
for dep in deps[module]:
closure[dep] = []
while True:
Expand Down Expand Up @@ -90,7 +91,7 @@ def build_list_direct(arg):
# Loop to find the shortest (remaining) circular dependency

def shortest_c_dep():
have_cycle = False
have_cycle: bool = False

sorted_keys = None

Expand Down
16 changes: 8 additions & 8 deletions contrib/devtools/symbol-check.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
find ../path/to/binaries -type f -executable | xargs python3 contrib/devtools/symbol-check.py
'''
import sys
from typing import Dict
from typing import Dict, List

import lief

Expand Down Expand Up @@ -173,7 +173,7 @@ def check_version(max_versions, version, arch) -> bool:
return ver <= max_versions[lib][arch]

def check_imported_symbols(binary) -> bool:
ok = True
ok: bool = True

for symbol in binary.imported_symbols:
if not symbol.imported:
Expand All @@ -189,7 +189,7 @@ def check_imported_symbols(binary) -> bool:
return ok

def check_exported_symbols(binary) -> bool:
ok = True
ok: bool = True

for symbol in binary.dynamic_symbols:
if not symbol.exported:
Expand All @@ -202,15 +202,15 @@ def check_exported_symbols(binary) -> bool:
return ok

def check_ELF_libraries(binary) -> bool:
ok = True
ok: bool = True
for library in binary.libraries:
if library not in ELF_ALLOWED_LIBRARIES:
print(f'{filename}: {library} is not in ALLOWED_LIBRARIES!')
ok = False
return ok

def check_MACHO_libraries(binary) -> bool:
ok = True
ok: bool = True
for dylib in binary.libraries:
split = dylib.name.split('/')
if split[-1] not in MACHO_ALLOWED_LIBRARIES:
Expand All @@ -229,7 +229,7 @@ def check_MACHO_sdk(binary) -> bool:
return False

def check_PE_libraries(binary) -> bool:
ok = True
ok: bool = True
for dylib in binary.libraries:
if dylib not in PE_ALLOWED_LIBRARIES:
print(f'{dylib} is not in ALLOWED_LIBRARIES!')
Expand Down Expand Up @@ -267,7 +267,7 @@ def check_ELF_interpreter(binary) -> bool:
}

if __name__ == '__main__':
retval = 0
retval: int = 0
for filename in sys.argv[1:]:
try:
binary = lief.parse(filename)
Expand All @@ -277,7 +277,7 @@ def check_ELF_interpreter(binary) -> bool:
retval = 1
continue

failed = []
failed: List[str] = []
for (name, func) in CHECKS[etype]:
if not func(binary):
failed.append(name)
Expand Down

0 comments on commit ab430bd

Please sign in to comment.