modernize type annotations and improve handling of int scalars

This commit is contained in:
Jan Petykiewicz 2024-07-28 23:20:14 -07:00
parent f12a1c6421
commit 15af9078f0
4 changed files with 20 additions and 16 deletions

View File

@ -1,7 +1,8 @@
"""
Functionality for encoding/decoding basic datatypes
"""
from typing import Sequence, IO
from typing import IO
from collections.abc import Sequence
import struct
import logging
from datetime import datetime

View File

@ -2,7 +2,7 @@
Functionality for reading/writing elements (geometry, text labels,
structure references) and associated properties.
"""
from typing import Optional, IO, TypeVar, Type, Union
from typing import IO, TypeVar
from abc import ABCMeta, abstractmethod
from dataclasses import dataclass
@ -78,7 +78,7 @@ class Element(metaclass=ABCMeta):
"""
@classmethod
@abstractmethod
def read(cls: Type[E], stream: IO[bytes]) -> E:
def read(cls: type[E], stream: IO[bytes]) -> E:
"""
Read from a stream to construct this object.
Consumes up to (and including) the ENDEL record.
@ -151,7 +151,7 @@ class Reference(Element):
""" Properties associated with this reference. """
@classmethod
def read(cls: Type[R], stream: IO[bytes]) -> R:
def read(cls: type[R], stream: IO[bytes]) -> R:
invert_y = False
mag = 1
angle_deg = 0
@ -233,7 +233,7 @@ class Boundary(Element):
""" Properties for the element. """
@classmethod
def read(cls: Type[B], stream: IO[bytes]) -> B:
def read(cls: type[B], stream: IO[bytes]) -> B:
layer = LAYER.skip_and_read(stream)[0]
dtype = DATATYPE.read(stream)[0]
xy = XY.read(stream).reshape(-1, 2)
@ -279,7 +279,7 @@ class Path(Element):
""" Properties for the element. """
@classmethod
def read(cls: Type[P], stream: IO[bytes]) -> P:
def read(cls: type[P], stream: IO[bytes]) -> P:
path_type = 0
width = 0
bgn_ext = 0
@ -344,7 +344,7 @@ class Box(Element):
""" Properties for the element. """
@classmethod
def read(cls: Type[X], stream: IO[bytes]) -> X:
def read(cls: type[X], stream: IO[bytes]) -> X:
layer = LAYER.skip_and_read(stream)[0]
dtype = BOXTYPE.read(stream)[0]
xy = XY.read(stream).reshape(-1, 2)
@ -378,7 +378,7 @@ class Node(Element):
""" Properties for the element. """
@classmethod
def read(cls: Type[N], stream: IO[bytes]) -> N:
def read(cls: type[N], stream: IO[bytes]) -> N:
layer = LAYER.skip_and_read(stream)[0]
dtype = NODETYPE.read(stream)[0]
xy = XY.read(stream).reshape(-1, 2)
@ -438,7 +438,7 @@ class Text(Element):
""" Properties for the element. """
@classmethod
def read(cls: Type[T], stream: IO[bytes]) -> T:
def read(cls: type[T], stream: IO[bytes]) -> T:
path_type = 0
presentation = 0
invert_y = False

View File

@ -1,7 +1,7 @@
"""
File-level read/write functionality.
"""
from typing import IO, TypeVar, Type, MutableMapping
from typing import IO, Self, TYPE_CHECKING
import io
from datetime import datetime
from dataclasses import dataclass
@ -15,8 +15,8 @@ from .records import BGNSTR, STRNAME, ENDSTR, SNAME, COLROW, ENDEL
from .records import BOX, BOUNDARY, NODE, PATH, TEXT, SREF, AREF
from .elements import Element, Reference, Text, Box, Boundary, Path, Node
FH = TypeVar('FH', bound='FileHeader')
if TYPE_CHECKING:
from collections.abc import MutableMapping
@dataclass
@ -45,7 +45,7 @@ class FileHeader:
""" Last-accessed time """
@classmethod
def read(cls: Type[FH], stream: IO[bytes]) -> FH:
def read(cls: type[Self], stream: IO[bytes]) -> Self:
"""
Read and construct a header from the provided stream.

View File

@ -1,7 +1,10 @@
"""
Record type and tag definitions
"""
from typing import Sequence
from typing import Self
from collections.abc import Sequence, Sized
import numpy
from numpy.typing import NDArray
from .record import NoDataRecord, BitArrayRecord, Int2Record, Int4Record, Real8Record
from .record import ASCIIRecord, DateTimeRecord
@ -169,7 +172,7 @@ class GENERATIONS(Int2Record):
@classmethod
def check_data(cls: type[Self], data: NDArray[numpy.integer] | Sequence[int] | int) -> None:
if len(data) != 1:
if not isinstance(data, Sized) or len(data) != 1:
raise Exception(f'Expected exactly one integer, got {data}')
@ -267,7 +270,7 @@ class FORMAT(Int2Record):
@classmethod
def check_data(cls: type[Self], data: NDArray[numpy.integer] | Sequence[int] | int) -> None:
if len(data) != 1:
if not isinstance(data, Sized) or len(data) != 1:
raise Exception(f'Expected exactly one integer, got {data}')