From 203306fec61940ed70ce6a9bf5871310c08501de Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Thu, 19 May 2022 15:04:40 +0200 Subject: [PATCH] Fix circular import --- PyPDF2/_page.py | 8 ++++++-- PyPDF2/types.py | 8 +------- PyPDF2/utils.py | 16 ++++++++++++++-- tests/test_utils.py | 6 +++--- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/PyPDF2/_page.py b/PyPDF2/_page.py index e602d8b87..b377b2eaa 100644 --- a/PyPDF2/_page.py +++ b/PyPDF2/_page.py @@ -47,8 +47,12 @@ RectangleObject, TextStringObject, ) -from .types import CompressedTransformationMatrix, TransformationMatrixType -from .utils import b_, matrixMultiply +from .utils import ( + CompressedTransformationMatrix, + TransformationMatrixType, + b_, + matrixMultiply, +) def getRectangle(self: Any, name: str, defaults: Iterable[str]) -> RectangleObject: diff --git a/PyPDF2/types.py b/PyPDF2/types.py index 9430a6866..8d9eb9d46 100644 --- a/PyPDF2/types.py +++ b/PyPDF2/types.py @@ -1,6 +1,6 @@ """Helpers for working with PDF types.""" -from typing import List, Tuple, Union +from typing import List, Union try: # Python 3.8+: https://peps.python.org/pep-0586 @@ -31,12 +31,6 @@ # Those go with the FitType: They specify values for the fit ZoomArgType: TypeAlias = Union[NumberObject, NullObject] ZoomArgsType: TypeAlias = List[ZoomArgType] -TransformationMatrixType: TypeAlias = Tuple[ - Tuple[float, float, float], Tuple[float, float, float], Tuple[float, float, float] -] -CompressedTransformationMatrix: TypeAlias = Tuple[ - float, float, float, float, float, float -] # Recursive types are not yet supported by mypy: # OutlinesType = List[Union[Destination, "OutlinesType"]] diff --git a/PyPDF2/utils.py b/PyPDF2/utils.py index 8011c8c93..cae2025cb 100644 --- a/PyPDF2/utils.py +++ b/PyPDF2/utils.py @@ -33,10 +33,22 @@ from codecs import getencoder from io import BufferedReader, BufferedWriter, BytesIO, FileIO -from typing import Any, Dict, Optional, Union, overload +from typing import Any, Dict, Optional, Tuple, Union, overload + +try: + # Python 3.10+: https://www.python.org/dev/peps/pep-0484/ + from typing import TypeAlias # type: ignore[attr-defined] +except ImportError: + from typing_extensions import TypeAlias # type: ignore[misc] from .errors import STREAM_TRUNCATED_PREMATURELY, PdfStreamError -from .types import TransformationMatrixType + +TransformationMatrixType: TypeAlias = Tuple[ + Tuple[float, float, float], Tuple[float, float, float], Tuple[float, float, float] +] +CompressedTransformationMatrix: TypeAlias = Tuple[ + float, float, float, float, float, float +] bytes_type = type(bytes()) # Works the same in Python 2.X and 3.X StreamType = Union[BytesIO, BufferedReader, BufferedWriter, FileIO] diff --git a/tests/test_utils.py b/tests/test_utils.py index 8c4feb7e8..78fa68cb3 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -63,9 +63,9 @@ def test_readUntilRegex_premature_ending_name(): @pytest.mark.parametrize( ("a", "b", "expected"), [ - ([[3]], [[7]], [[21]]), - ([[3, 7]], [[5], [13]], [[3 * 5.0 + 7 * 13]]), - ([[3], [7]], [[5, 13]], [[3 * 5, 3 * 13], [7 * 5, 7 * 13]]), + (((3,),), ((7,),), ((21,),)), + (((3, 7),), ((5,), (13,)), ((3 * 5.0 + 7 * 13,),)), + (((3,), (7,)), ((5, 13),), ((3 * 5, 3 * 13), (7 * 5, 7 * 13))), ], ) def test_matrixMultiply(a, b, expected):