Add additional annotations to avoid typing issues (#92897)

BlendFileBlock attributes:

Explicit annotation on BlendFileBlock are needed because otherwise
e.g. `block.add_old` type was imprecisely inferred from the
assignments as `int | Any`, where `Any` comes from `.unpack` returning
`tuple[Any, ...]`.

Ideally unpack should be somehow connected to the returned types, but
this solution should work for now just to avoid typing errors.

dna_io - add some missing annotations:

Some annotations were needed to ensure `block.code` will be inferred
as `bytes` and not `bytes | Unknown`.

Reviewed-on: https://projects.blender.org/blender/blender-asset-tracer/pulls/92897
Reviewed-by: Sybren A. Stüvel <sybren@blender.org>
This commit is contained in:
Andrej730 2025-07-28 15:32:57 +02:00 committed by Sybren A. Stüvel
parent 899f361fcf
commit 79c8589bfc
2 changed files with 9 additions and 3 deletions

View File

@ -447,6 +447,12 @@ class BlendFileBlock:
old_structure = struct.Struct(b"4sI") old_structure = struct.Struct(b"4sI")
"""old blend files ENDB block structure""" """old blend files ENDB block structure"""
# Explicitly annotate to avoid `Any` from `.unpack()`.
size: int
addr_old: int
sdna_index: int
count: int
def __init__(self, bfile: BlendFile) -> None: def __init__(self, bfile: BlendFile) -> None:
self.bfile = bfile self.bfile = bfile

View File

@ -204,17 +204,17 @@ class EndianIO:
return fileobj.write(to_write) return fileobj.write(to_write)
@classmethod @classmethod
def read_bytes0(cls, fileobj, length): def read_bytes0(cls, fileobj: typing.IO[bytes], length: int) -> bytes:
data = fileobj.read(length) data = fileobj.read(length)
return cls.read_data0(data) return cls.read_data0(data)
@classmethod @classmethod
def read_data0_offset(cls, data, offset): def read_data0_offset(cls, data: bytes, offset: int) -> bytes:
add = data.find(b"\0", offset) - offset add = data.find(b"\0", offset) - offset
return data[offset : offset + add] return data[offset : offset + add]
@classmethod @classmethod
def read_data0(cls, data): def read_data0(cls, data: bytes) -> bytes:
add = data.find(b"\0") add = data.find(b"\0")
if add < 0: if add < 0:
return data return data