Skip to content

Commit d5e65aa

Browse files
Aleksey Noginclaude
andcommitted
Restore Generic[K,V] to UnpackAndVerifyTestCase with mypy workaround
Workaround for python/mypy#12633, fixed in mypy 0.950. Explicit type redeclarations in subclasses can be removed once using a newer mypy. Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 15a64ea commit d5e65aa

4 files changed

Lines changed: 39 additions & 13 deletions

File tree

‎pytest_ofrak/src/pytest_ofrak/patterns/basic_block_unpacker.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"""
88
import os
99
from dataclasses import dataclass
10-
from typing import Dict, List, Union, Tuple
10+
from typing import Dict, List, Set, Union, Tuple
1111

1212
import pytest
1313
from ofrak.core.filesystem import File
@@ -31,10 +31,16 @@
3131

3232

3333
@dataclass
34-
class BasicBlockUnpackerTestCase(UnpackAndVerifyTestCase):
34+
class BasicBlockUnpackerTestCase(
35+
UnpackAndVerifyTestCase[int, List[ExpectedBasicBlockUnpackResult]]
36+
):
3537
binary_filename: str
3638
binary_md5_digest: str
3739
basic_block_data_ranges_in_root: Dict[int, Range] # Used when created basic blocks manually
40+
# Workaround for https://github.com/python/mypy/issues/12633, fixed in mypy 0.950.
41+
# Remove these two lines once using a newer mypy.
42+
expected_results: Dict[int, List[ExpectedBasicBlockUnpackResult]]
43+
optional_results: Set[int]
3844

3945

4046
BASIC_BLOCK_UNPACKER_TEST_CASES: List[BasicBlockUnpackerTestCase] = [

‎pytest_ofrak/src/pytest_ofrak/patterns/code_region_unpacker.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"""
88
import os.path
99
from dataclasses import dataclass
10-
from typing import List, Dict
10+
from typing import Dict, List, Set
1111

1212
import pytest
1313

@@ -27,9 +27,13 @@
2727

2828

2929
@dataclass
30-
class CodeRegionUnpackerTestCase(UnpackAndVerifyTestCase):
30+
class CodeRegionUnpackerTestCase(UnpackAndVerifyTestCase[int, List[int]]):
3131
binary_filename: str
3232
binary_md5_digest: str
33+
# Workaround for https://github.com/python/mypy/issues/12633, fixed in mypy 0.950.
34+
# Remove these two lines once using a newer mypy.
35+
expected_results: Dict[int, List[int]]
36+
optional_results: Set[int]
3337

3438

3539
CODE_REGION_UNPACKER_TEST_CASES: List[CodeRegionUnpackerTestCase] = [

‎pytest_ofrak/src/pytest_ofrak/patterns/complex_block_unpacker.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"""
88
import os
99
from dataclasses import dataclass
10-
from typing import Dict, List, Union
10+
from typing import Dict, List, Set, Union
1111

1212
import pytest
1313

@@ -28,9 +28,13 @@
2828

2929

3030
@dataclass
31-
class ComplexBlockUnpackerTestCase(UnpackAndVerifyTestCase):
31+
class ComplexBlockUnpackerTestCase(UnpackAndVerifyTestCase[int, List[Union[BasicBlock, DataWord]]]):
3232
binary_filename: str
3333
binary_md5_digest: str
34+
# Workaround for https://github.com/python/mypy/issues/12633, fixed in mypy 0.950.
35+
# Remove these two lines once using a newer mypy.
36+
expected_results: Dict[int, List[Union[BasicBlock, DataWord]]]
37+
optional_results: Set[int]
3438

3539

3640
COMPLEX_BLOCK_UNPACKER_TEST_CASES: List[ComplexBlockUnpackerTestCase] = [

‎pytest_ofrak/src/pytest_ofrak/patterns/unpack_verify.py‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
import warnings
99
from abc import abstractmethod, ABC
1010
from dataclasses import dataclass
11-
from typing import Any, Dict, Set, Iterable
11+
from typing import Any, Dict, Generic, TypeVar, Set, Iterable
1212

1313
import pytest
1414

1515
from ofrak import OFRAKContext
1616
from ofrak.resource import Resource
1717

18+
K = TypeVar("K")
19+
V = TypeVar("V")
20+
1821

1922
def _hexlify(x):
2023
if isinstance(x, Iterable):
@@ -23,7 +26,7 @@ def _hexlify(x):
2326

2427

2528
@dataclass
26-
class UnpackAndVerifyTestCase:
29+
class UnpackAndVerifyTestCase(Generic[K, V]):
2730
"""
2831
Container for test cases which will be fed to an UnpackAndVerifyPattern implementation.
2932
Extend this class, adding some fields to hold the information needed to create the root
@@ -35,11 +38,18 @@ class UnpackAndVerifyTestCase:
3538
- Optional results may or may not exist in the unpacker output. The lack of an optional
3639
result generates a warning.
3740
- Results which aren't specified in either 'expected' or 'optional' sets fail the test.
41+
42+
:param K: The key type of the expected and optional results. The unpacked resources will each be assigned
43+
a key of this same type, allowing the test to match actual unpacked resources with expected
44+
values.
45+
:param V: The value type of the expected and optional results. This can be anything, as long as it
46+
contains the information needed to verify that the actual unpacked resource matches what is
47+
expected.
3848
"""
3949

4050
label: str
41-
expected_results: Dict[Any, Any]
42-
optional_results: Set[Any]
51+
expected_results: Dict[K, V]
52+
optional_results: Set[K]
4353

4454

4555
class UnpackAndVerifyPattern(ABC):
@@ -161,8 +171,8 @@ async def root_resource(
161171

162172
@pytest.fixture
163173
async def expected_results(
164-
self, unpack_verify_test_case: UnpackAndVerifyTestCase
165-
) -> Dict[Any, Any]:
174+
self, unpack_verify_test_case: UnpackAndVerifyTestCase[K, V]
175+
) -> Dict[K, V]:
166176
"""
167177
Extract the expected results from the test case.
168178
@@ -173,7 +183,9 @@ async def expected_results(
173183
return unpack_verify_test_case.expected_results
174184

175185
@pytest.fixture
176-
async def optional_results(self, unpack_verify_test_case: UnpackAndVerifyTestCase) -> Set[Any]:
186+
async def optional_results(
187+
self, unpack_verify_test_case: UnpackAndVerifyTestCase[K, V]
188+
) -> Set[K]:
177189
"""
178190
Extract the optional results from the test case.
179191

0 commit comments

Comments
 (0)