Skip to content

Commit 7be0a22

Browse files
committed
style: fix some wrong type annotations in the codebase
1 parent 0290e5b commit 7be0a22

17 files changed

Lines changed: 41 additions & 33 deletions

File tree

libdestruct/backing/fake_resolver.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77
from __future__ import annotations
88

9+
from typing import Literal
10+
911
from libdestruct.backing.resolver import Resolver
1012

1113

1214
class FakeResolver(Resolver):
1315
"""A class that can resolve elements in a simulated memory storage."""
1416

15-
def __init__(self: FakeResolver, memory: dict | None = None, address: int | None = 0, endianness: str = "little") -> None:
17+
def __init__(self: FakeResolver, memory: dict | None = None, address: int | None = 0, endianness: Literal["little", "big"] = "little") -> None:
1618
"""Initializes a basic fake resolver."""
1719
self.memory = memory if memory is not None else {}
1820
self.address = address

libdestruct/backing/memory_resolver.py

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

77
from __future__ import annotations
88

9-
from typing import TYPE_CHECKING
9+
from typing import TYPE_CHECKING, Literal
1010

1111
from libdestruct.backing.resolver import Resolver
1212

@@ -17,7 +17,7 @@
1717
class MemoryResolver(Resolver):
1818
"""A class that can resolve itself to a value in a referenced memory storage."""
1919

20-
def __init__(self: MemoryResolver, memory: MutableSequence, address: int | None, endianness: str = "little") -> None:
20+
def __init__(self: MemoryResolver, memory: MutableSequence, address: int | None, endianness: Literal["little", "big"] = "little") -> None:
2121
"""Initializes a basic memory resolver."""
2222
self.memory = memory
2323
self.address = address

libdestruct/backing/resolver.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from __future__ import annotations
88

99
from abc import ABC, abstractmethod
10+
from typing import Literal
1011

1112
from typing_extensions import Self
1213

@@ -16,7 +17,7 @@ class Resolver(ABC):
1617

1718
parent: Self
1819

19-
endianness: str = "little"
20+
endianness: Literal["little", "big"] = "little"
2021
"""The endianness of the data this resolver accesses."""
2122

2223
@abstractmethod

libdestruct/c/c_integer_types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def get(self: _c_integer) -> int:
2828
def to_bytes(self: _c_integer) -> bytes:
2929
"""Return the serialized representation of the object."""
3030
if self._frozen:
31+
assert isinstance(self._frozen_value, int)
3132
return self._frozen_value.to_bytes(self.size, self.endianness, signed=self.signed)
3233

3334
return self.resolver.resolve(self.size, 0)

libdestruct/c/c_str.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from __future__ import annotations
88

9+
from collections.abc import Iterator
10+
911
from libdestruct.common.array.array import array
1012

1113

@@ -54,7 +56,7 @@ def __setitem__(self: c_str, index: int, value: bytes) -> None:
5456
"""Set the character at the given index to the given value."""
5557
self._set(value, index)
5658

57-
def __iter__(self: c_str) -> iter:
59+
def __iter__(self: c_str) -> Iterator:
5860
"""Return an iterator over the string."""
5961
for i in range(self.count()):
6062
yield self.get(i)

libdestruct/common/array/array.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from __future__ import annotations
88

99
from abc import abstractmethod
10+
from collections.abc import Iterator
1011
from types import GenericAlias
1112

1213
from libdestruct.common.obj import obj
@@ -42,7 +43,7 @@ def __setitem__(self: array, index: int, value: object) -> None:
4243
self.set(index, value)
4344

4445
@abstractmethod
45-
def __iter__(self: array) -> iter:
46+
def __iter__(self: array) -> Iterator:
4647
"""Return an iterator over the array."""
4748

4849
def __contains__(self: array, value: object) -> bool:

libdestruct/common/enum/enum.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def __class_getitem__(cls, params: tuple) -> GenericAlias:
3030
python_enum: type[Enum]
3131
"""The backing Python enum."""
3232

33-
_backing_type: type[obj]
34-
"""The backing type."""
33+
_backing_type: obj
34+
"""The inflated backing instance."""
3535

3636
lenient: bool
3737
"""Whether the conversion is lenient or not."""

libdestruct/common/enum/int_enum_field.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def __init__(
5858
case _:
5959
raise ValueError("The size of the field must be a power of 2.")
6060

61-
def inflate(self: IntEnumField, resolver: Resolver) -> int:
61+
def inflate(self: IntEnumField, resolver: Resolver) -> enum:
6262
"""Inflate the field.
6363
6464
Args:

libdestruct/common/flags/flags.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def __class_getitem__(cls, params: tuple) -> GenericAlias:
3030
python_flag: type[IntFlag]
3131
"""The backing Python IntFlag."""
3232

33-
_backing_type: type[obj]
34-
"""The backing type."""
33+
_backing_type: obj
34+
"""The inflated backing instance."""
3535

3636
lenient: bool
3737
"""Whether the conversion is lenient or not."""

libdestruct/common/inflater.py

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

77
from __future__ import annotations
88

9-
from typing import TYPE_CHECKING
9+
from typing import TYPE_CHECKING, Literal
1010

1111
from libdestruct.backing.memory_resolver import MemoryResolver
1212
from libdestruct.common.type_registry import TypeRegistry
@@ -21,7 +21,7 @@
2121
class Inflater:
2222
"""The memory manager, which inflates any memory-referencing type."""
2323

24-
def __init__(self: Inflater, memory: MutableSequence, endianness: str = "little") -> None:
24+
def __init__(self: Inflater, memory: MutableSequence, endianness: Literal["little", "big"] = "little") -> None:
2525
"""Initialize the memory manager."""
2626
self.memory = memory
2727
self.endianness = endianness

0 commit comments

Comments
 (0)