Skip to content

Commit 98b0acd

Browse files
committed
Merge bitcoin#28725: test: refactor: use built-in collection types for type hints (Python 3.9 / PEP 585)
a478c81 test: replace `Callable`/`Iterable` with their `collections.abc` alternative (PEP 585) (stickies-v) 4b9afb1 scripted-diff: use PEP 585 built-in collection types for verify-binary script (Sebastian Falbesoner) d516cf8 test: use built-in collection types for type hints (Python 3.9 / PEP 585) (Sebastian Falbesoner) Pull request description: With Python 3.9 / [PEP 585](https://peps.python.org/pep-0585/), [type hinting has become a little less awkward](https://docs.python.org/3.9/whatsnew/3.9.html#type-hinting-generics-in-standard-collections), as for collection types one doesn't need to import the corresponding capitalized types (`Dict`, `List`, `Set`, `Tuple`, ...) anymore, but can use the built-in types directly (see https://peps.python.org/pep-0585/#implementation for the full list). This PR applies the replacement for all Python scripts (i.e. in the contrib and test folders) for the basic types, i.e.: - typing.Dict -> dict - typing.List -> list - typing.Set -> set - typing.Tuple -> tuple For an additional check, I ran mypy 1.6.1 on both master and the PR branch via ``` $ mypy --ignore-missing-imports --explicit-package-bases $(git ls-files "*.py") ``` and verified that the output is identical -- (from the 22 identified problems, most look like false-positives, it's probably worth it to go deeper here and address them in a follow-up though). ACKs for top commit: stickies-v: ACK a478c81 fanquake: ACK a478c81 Tree-SHA512: 6948c905f6abd644d84f09fcb3661d7edb2742e8f2b28560008697d251d77a61a1146ab4b070e65b0d27acede7a5256703da7bf6eb1c7c3a897755478c76c6e8
2 parents 950af7c + a478c81 commit 98b0acd

19 files changed

+91
-101
lines changed

contrib/devtools/circular-dependencies.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import sys
77
import re
8-
from typing import Dict, List, Set
98

109
MAPPING = {
1110
'core_read.cpp': 'core_io.cpp',
@@ -33,7 +32,7 @@ def module_name(path):
3332
return None
3433

3534
files = dict()
36-
deps: Dict[str, Set[str]] = dict()
35+
deps: dict[str, set[str]] = dict()
3736

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

@@ -65,7 +64,7 @@ def module_name(path):
6564
shortest_cycle = None
6665
for module in sorted(deps.keys()):
6766
# Build the transitive closure of dependencies of module
68-
closure: Dict[str, List[str]] = dict()
67+
closure: dict[str, list[str]] = dict()
6968
for dep in deps[module]:
7069
closure[dep] = []
7170
while True:

contrib/devtools/security-check.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
Otherwise the exit status will be 1 and it will log which executables failed which checks.
99
'''
1010
import sys
11-
from typing import List
1211

1312
import lief
1413

@@ -255,7 +254,7 @@ def check_MACHO_control_flow(binary) -> bool:
255254
retval = 1
256255
continue
257256

258-
failed: List[str] = []
257+
failed: list[str] = []
259258
for (name, func) in CHECKS[etype][arch]:
260259
if not func(binary):
261260
failed.append(name)

contrib/devtools/symbol-check.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
find ../path/to/binaries -type f -executable | xargs python3 contrib/devtools/symbol-check.py
1212
'''
1313
import sys
14-
from typing import List, Dict
1514

1615
import lief
1716

@@ -53,7 +52,7 @@
5352

5453
# Expected linker-loader names can be found here:
5554
# https://sourceware.org/glibc/wiki/ABIList?action=recall&rev=16
56-
ELF_INTERPRETER_NAMES: Dict[lief.ELF.ARCH, Dict[lief.ENDIANNESS, str]] = {
55+
ELF_INTERPRETER_NAMES: dict[lief.ELF.ARCH, dict[lief.ENDIANNESS, str]] = {
5756
lief.ELF.ARCH.x86_64: {
5857
lief.ENDIANNESS.LITTLE: "/lib64/ld-linux-x86-64.so.2",
5958
},
@@ -72,7 +71,7 @@
7271
},
7372
}
7473

75-
ELF_ABIS: Dict[lief.ELF.ARCH, Dict[lief.ENDIANNESS, List[int]]] = {
74+
ELF_ABIS: dict[lief.ELF.ARCH, dict[lief.ENDIANNESS, list[int]]] = {
7675
lief.ELF.ARCH.x86_64: {
7776
lief.ENDIANNESS.LITTLE: [3,2,0],
7877
},
@@ -302,7 +301,7 @@ def check_ELF_ABI(binary) -> bool:
302301
retval = 1
303302
continue
304303

305-
failed: List[str] = []
304+
failed: list[str] = []
306305
for (name, func) in CHECKS[etype]:
307306
if not func(binary):
308307
failed.append(name)

contrib/devtools/test-security-check.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import lief
99
import os
1010
import subprocess
11-
from typing import List
1211
import unittest
1312

1413
from utils import determine_wellknown_cmd
@@ -34,7 +33,7 @@ def call_security_check(cc: str, source: str, executable: str, options) -> tuple
3433
#
3534
# See the definitions for ac_link in autoconf's lib/autoconf/c.m4 file for
3635
# reference.
37-
env_flags: List[str] = []
36+
env_flags: list[str] = []
3837
for var in ['CFLAGS', 'CPPFLAGS', 'LDFLAGS']:
3938
env_flags += filter(None, os.environ.get(var, '').split(' '))
4039

contrib/devtools/test-symbol-check.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@
77
'''
88
import os
99
import subprocess
10-
from typing import List
1110
import unittest
1211

1312
from utils import determine_wellknown_cmd
1413

15-
def call_symbol_check(cc: List[str], source, executable, options):
14+
def call_symbol_check(cc: list[str], source, executable, options):
1615
# This should behave the same as AC_TRY_LINK, so arrange well-known flags
1716
# in the same order as autoconf would.
1817
#
1918
# See the definitions for ac_link in autoconf's lib/autoconf/c.m4 file for
2019
# reference.
21-
env_flags: List[str] = []
20+
env_flags: list[str] = []
2221
for var in ['CFLAGS', 'CPPFLAGS', 'LDFLAGS']:
2322
env_flags += filter(None, os.environ.get(var, '').split(' '))
2423

@@ -28,7 +27,7 @@ def call_symbol_check(cc: List[str], source, executable, options):
2827
os.remove(executable)
2928
return (p.returncode, p.stdout.rstrip())
3029

31-
def get_machine(cc: List[str]):
30+
def get_machine(cc: list[str]):
3231
p = subprocess.run([*cc,'-dumpmachine'], stdout=subprocess.PIPE, text=True)
3332
return p.stdout.rstrip()
3433

contrib/devtools/utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
import shutil
99
import sys
1010
import os
11-
from typing import List
1211

1312

14-
def determine_wellknown_cmd(envvar, progname) -> List[str]:
13+
def determine_wellknown_cmd(envvar, progname) -> list[str]:
1514
maybe_env = os.getenv(envvar)
1615
maybe_which = shutil.which(progname)
1716
if maybe_env:

contrib/macdeploy/macdeployqtplus

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import sys, re, os, platform, shutil, stat, subprocess, os.path
2020
from argparse import ArgumentParser
2121
from pathlib import Path
2222
from subprocess import PIPE, run
23-
from typing import List, Optional
23+
from typing import Optional
2424

2525
# This is ported from the original macdeployqt with modifications
2626

@@ -181,7 +181,7 @@ class DeploymentInfo(object):
181181
return True
182182
return False
183183

184-
def getFrameworks(binaryPath: str, verbose: int) -> List[FrameworkInfo]:
184+
def getFrameworks(binaryPath: str, verbose: int) -> list[FrameworkInfo]:
185185
if verbose:
186186
print(f"Inspecting with otool: {binaryPath}")
187187
otoolbin=os.getenv("OTOOL", "otool")
@@ -285,7 +285,7 @@ def copyFramework(framework: FrameworkInfo, path: str, verbose: int) -> Optional
285285

286286
return toPath
287287

288-
def deployFrameworks(frameworks: List[FrameworkInfo], bundlePath: str, binaryPath: str, strip: bool, verbose: int, deploymentInfo: Optional[DeploymentInfo] = None) -> DeploymentInfo:
288+
def deployFrameworks(frameworks: list[FrameworkInfo], bundlePath: str, binaryPath: str, strip: bool, verbose: int, deploymentInfo: Optional[DeploymentInfo] = None) -> DeploymentInfo:
289289
if deploymentInfo is None:
290290
deploymentInfo = DeploymentInfo()
291291

contrib/message-capture/message-capture-parser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from io import BytesIO
1212
import json
1313
from pathlib import Path
14-
from typing import Any, List, Optional
14+
from typing import Any, Optional
1515

1616
sys.path.append(os.path.join(os.path.dirname(__file__), '../../test/functional'))
1717

@@ -92,7 +92,7 @@ def to_jsonable(obj: Any) -> Any:
9292
return obj
9393

9494

95-
def process_file(path: str, messages: List[Any], recv: bool, progress_bar: Optional[ProgressBar]) -> None:
95+
def process_file(path: str, messages: list[Any], recv: bool, progress_bar: Optional[ProgressBar]) -> None:
9696
with open(path, 'rb') as f_in:
9797
if progress_bar:
9898
bytes_read = 0
@@ -188,7 +188,7 @@ def main():
188188
output = Path.cwd() / Path(args.output) if args.output else False
189189
use_progress_bar = (not args.no_progress_bar) and sys.stdout.isatty()
190190

191-
messages = [] # type: List[Any]
191+
messages = [] # type: list[Any]
192192
if use_progress_bar:
193193
total_size = sum(capture.stat().st_size for capture in capturepaths)
194194
progress_bar = ProgressBar(total_size)

0 commit comments

Comments
 (0)