[basic] cast to the expected types before conversion

This commit is contained in:
Jan Petykiewicz 2026-02-15 16:25:21 -08:00
commit 5b42cdf609

View file

@ -2,7 +2,7 @@
This module contains all datatypes and parsing/writing functions for This module contains all datatypes and parsing/writing functions for
all abstractions below the 'record' or 'block' level. all abstractions below the 'record' or 'block' level.
""" """
from typing import Any, IO, Union from typing import Any, IO, Union, TYPE_CHECKING
from collections.abc import Sequence from collections.abc import Sequence
from fractions import Fraction from fractions import Fraction
from enum import Enum from enum import Enum
@ -250,6 +250,7 @@ def write_uint(stream: IO[bytes], n: int) -> int:
Raises: Raises:
SignedError: if `n` is negative. SignedError: if `n` is negative.
""" """
n = int(n)
if n < 0: if n < 0:
raise SignedError(f'uint must be positive: {n}') raise SignedError(f'uint must be positive: {n}')
@ -295,6 +296,7 @@ def encode_sint(sint: int) -> int:
Returns: Returns:
Unsigned integer encoding for the input. Unsigned integer encoding for the input.
""" """
sint = int(sint)
return (abs(sint) << 1) | (sint < 0) return (abs(sint) << 1) | (sint < 0)
@ -1595,7 +1597,7 @@ def read_point_list(
def write_point_list( def write_point_list(
stream: IO[bytes], stream: IO[bytes],
points: list[Sequence[int]], points: 'list[Sequence[int]] | NDArray',
fast: bool = False, fast: bool = False,
implicit_closed: bool = True implicit_closed: bool = True
) -> int: ) -> int:
@ -1618,6 +1620,8 @@ def write_point_list(
Number of bytes written. Number of bytes written.
""" """
# If we're in a hurry, just write the points as arbitrary Deltas # If we're in a hurry, just write the points as arbitrary Deltas
if _USE_NUMPY:
points = numpy.asarray(points, dtype=int)
if fast: if fast:
size = write_uint(stream, 4) size = write_uint(stream, 4)
size += write_uint(stream, len(points)) size += write_uint(stream, len(points))