From d83ef1ce2d275b1a063c7b45787c95622900a89e Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Mon, 29 Jul 2024 03:53:08 -0700 Subject: [PATCH] update type annotations --- fatamorgana/basic.py | 17 +++++++++-------- fatamorgana/main.py | 4 ++-- fatamorgana/records.py | 3 ++- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/fatamorgana/basic.py b/fatamorgana/basic.py index ea96528..57b3919 100644 --- a/fatamorgana/basic.py +++ b/fatamorgana/basic.py @@ -2,7 +2,8 @@ This module contains all datatypes and parsing/writing functions for all abstractions below the 'record' or 'block' level. """ -from typing import Type, Union, Any, Sequence, IO +from typing import Any, IO, Union +from collections.abc import Sequence from fractions import Fraction from enum import Enum import math @@ -20,7 +21,7 @@ except ImportError: ''' Type definitions ''' -real_t = Union[int, float, Fraction] +real_t = int | float | Fraction repetition_t = Union['ReuseRepetition', 'GridRepetition', 'ArbitraryRepetition'] property_value_t = Union[int, bytes, 'AString', 'NString', 'PropStringReference', float, Fraction] bytes_t = bytes @@ -184,7 +185,7 @@ if _USE_NUMPY: byte_arr = _read(stream, 1) return numpy.unpackbits(numpy.frombuffer(byte_arr, dtype=numpy.uint8)) - def _np_write_bool_byte(stream: IO[bytes], bits: tuple[Union[bool, int], ...]) -> int: + def _np_write_bool_byte(stream: IO[bytes], bits: tuple[bool | int, ...]) -> int: """ Pack 8 booleans into a byte, and write it to the stream. @@ -563,7 +564,7 @@ class NString: """ _string: str - def __init__(self, string_or_bytes: Union[bytes, str]) -> None: + def __init__(self, string_or_bytes: bytes | str) -> None: """ Args: string_or_bytes: Content of the `NString`. @@ -677,7 +678,7 @@ class AString: """ _string: str - def __init__(self, string_or_bytes: Union[bytes, str]) -> None: + def __init__(self, string_or_bytes: bytes | str) -> None: """ Args: string_or_bytes: Content of the AString. @@ -1722,10 +1723,10 @@ class PropStringReference: ref: int """ID of the target""" - reference_type: Type + reference_type: type """Type of the target: `bytes`, `NString`, or `AString`""" - def __init__(self, ref: int, ref_type: Type) -> None: + def __init__(self, ref: int, ref_type: type) -> None: """ :param ref: ID number of the target. :param ref_type: Type of the target. One of bytes, NString, AString. @@ -1767,7 +1768,7 @@ def read_property_value(stream: IO[bytes]) -> property_value_t: Raises: InvalidDataError: if an invalid type is read. """ - ref_type: Type + ref_type: type prop_type = read_uint(stream) if 0 <= prop_type <= 7: return read_real(stream, prop_type) diff --git a/fatamorgana/main.py b/fatamorgana/main.py index 1bb7258..978d842 100644 --- a/fatamorgana/main.py +++ b/fatamorgana/main.py @@ -3,7 +3,7 @@ This module contains data structures and functions for reading from and writing to whole OASIS layout files, and provides a few additional abstractions for the data contained inside them. """ -from typing import Type, IO +from typing import IO import io import logging @@ -528,7 +528,7 @@ class XName: # Mapping from record id to record class. -_GEOMETRY: dict[int, Type[records.geometry_t]] = { +_GEOMETRY: dict[int, type[records.geometry_t]] = { 19: records.Text, 20: records.Rectangle, 21: records.Polygon, diff --git a/fatamorgana/records.py b/fatamorgana/records.py index 774ea98..81364ca 100644 --- a/fatamorgana/records.py +++ b/fatamorgana/records.py @@ -10,7 +10,8 @@ Higher-level code (e.g. monitoring for combinations of records with parse, or code for dealing with nested records in a CBlock) should live in main.py instead. """ -from typing import Union, Sequence, Any, TypeVar, IO +from typing import Any, TypeVar, IO, Union +from collections.abc import Sequence from abc import ABCMeta, abstractmethod import copy import math