Skip to content

Commit ba461f8

Browse files
fkglrrumpelsepp
authored andcommitted
Hacky way of removing the need to type Literal for AutoLiteral
1 parent 1ac32d3 commit ba461f8

3 files changed

Lines changed: 21 additions & 17 deletions

File tree

src/gallia/command/config.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import binascii
66
import os
77
import tomllib
8+
import typing
89
from abc import ABC
910
from collections.abc import Callable
1011
from enum import Enum
@@ -116,7 +117,6 @@ def _process_ranges(value: Any) -> Any:
116117

117118
T = TypeVar("T")
118119
EnumType = TypeVar("EnumType", bound=Enum)
119-
LiteralType = TypeVar("LiteralType")
120120

121121

122122
def auto_enum(x: str, enum_type: type[EnumType]) -> EnumType:
@@ -137,7 +137,12 @@ def auto_enum(x: str, enum_type: type[EnumType]) -> EnumType:
137137
if TYPE_CHECKING:
138138
InitializeIdempotent: TypeAlias = Annotated[T, ""]
139139
EnumArg: TypeAlias = Annotated[EnumType, ""]
140-
AutoLiteral: TypeAlias = Annotated[LiteralType, ""]
140+
141+
# For mypy to accept AutoLiteral as a direct alias to Literal, using TyeAliases seems to not work
142+
# Exporting literal under a new name seems to be the only way this works
143+
from typing import Literal as AutoLiteral
144+
145+
__all__ = ["AutoLiteral"]
141146
else:
142147

143148
class _TrickType:
@@ -201,11 +206,14 @@ def try_auto_literal(value: Any):
201206

202207
return Annotated[cls, BeforeValidator(try_auto_literal)]
203208

204-
AutoLiteral = _TrickType(auto_literal)
209+
class AutoLiteral:
210+
def __class_getitem__(cls, literals) -> type[T]:
211+
return _TrickType(auto_literal)[typing.Literal[literals]]
212+
205213
"""
206214
Wrapper for Literal fields to provide automatic handling of enum, int and bytes parsing for values defined in Literals similar to EnumArg, AutoInt and HexBytes.
207215
208-
Usage: x: AutoLiteral[Literal[1, 2, 3]] = ...
216+
Usage: x: AutoLiteral[1, 2, 3] = ...
209217
"""
210218

211219

src/gallia/commands/fuzz/uds/pdu.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import asyncio
66
import random
77
import sys
8-
from typing import Literal
98

109
assert sys.platform.startswith("linux"), "unsupported platform"
1110

@@ -26,11 +25,11 @@
2625

2726
class PDUFuzzerConfig(UDSScannerConfig):
2827
sessions: Ranges = Field([1], description="Set list of sessions to be tested; 0x01 if None")
29-
service: AutoLiteral[
30-
Literal[UDSIsoServices.WriteDataByIdentifier, UDSIsoServices.RoutineControl]
31-
] = Field(
32-
UDSIsoServices.WriteDataByIdentifier,
33-
description="Service ID to create payload for; defaults to 0x2e WriteDataByIdentifier;\ncurrently supported:\n0x2e WriteDataByIdentifier, 0x31 RoutineControl (startRoutine)\n",
28+
service: AutoLiteral[UDSIsoServices.WriteDataByIdentifier, UDSIsoServices.RoutineControl] = (
29+
Field(
30+
UDSIsoServices.WriteDataByIdentifier,
31+
description="Service ID to create payload for; defaults to 0x2e WriteDataByIdentifier;\ncurrently supported:\n0x2e WriteDataByIdentifier, 0x31 RoutineControl (startRoutine)\n",
32+
)
3433
)
3534
max_length: AutoInt = Field(42, description="maximum length of the payload")
3635
min_length: AutoInt = Field(1, description="minimum length of the payload")

src/gallia/commands/scan/uds/memory.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
import sys
6-
from typing import Literal
76

87
from gallia.command import UDSScanner
98
from gallia.command.config import AutoInt, AutoLiteral, Field, HexBytes
@@ -24,12 +23,10 @@ class MemoryFunctionsScannerConfig(UDSScannerConfig):
2423
const=1,
2524
)
2625
service: AutoLiteral[
27-
Literal[
28-
Services.ReadMemoryByAddress,
29-
Services.WriteMemoryByAddress,
30-
Services.RequestDownload,
31-
Services.RequestUpload,
32-
]
26+
Services.ReadMemoryByAddress,
27+
Services.WriteMemoryByAddress,
28+
Services.RequestDownload,
29+
Services.RequestUpload,
3330
] = Field(
3431
description="Choose between 0x23 ReadMemoryByAddress 0x3d WriteMemoryByAddress, 0x34 RequestDownload and 0x35 RequestUpload"
3532
)

0 commit comments

Comments
 (0)