From 681a8cd9a30c1bba23aebcd9aad7854223d4f158 Mon Sep 17 00:00:00 2001 From: deathaxe Date: Thu, 24 Apr 2025 20:54:22 +0200 Subject: [PATCH 1/2] Fix typing related imports This commit ... 1. removes obsolete type imports such as `Dict`, `Optional`, ... . 2. imports as many types as possible in TYPE_CHECKING mode, only to limit impact of typing on runtime import time. --- pyte/__init__.py | 2 -- pyte/screens.py | 7 +++++-- pyte/streams.py | 8 ++++---- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/pyte/__init__.py b/pyte/__init__.py index e004680..d671ad5 100644 --- a/pyte/__init__.py +++ b/pyte/__init__.py @@ -26,8 +26,6 @@ "Stream", "ByteStream") import io -from typing import Union - from .screens import Screen, DiffScreen, HistoryScreen, DebugScreen from .streams import Stream, ByteStream diff --git a/pyte/screens.py b/pyte/screens.py index 7145a7b..384542f 100644 --- a/pyte/screens.py +++ b/pyte/screens.py @@ -35,8 +35,7 @@ import warnings from collections import deque, defaultdict from functools import lru_cache -from typing import Any, Dict, List, NamedTuple, Optional, Set, TextIO, TypeVar -from collections.abc import Callable, Generator, Sequence +from typing import TYPE_CHECKING, NamedTuple, TypeVar from wcwidth import wcwidth as _wcwidth # type: ignore[import-untyped] @@ -48,6 +47,10 @@ ) from .streams import Stream +if TYPE_CHECKING: + from collections.abc import Callable, Generator, Sequence + from typing import Any, NamedTuple, TextIO + wcwidth: Callable[[str], int] = lru_cache(maxsize=4096)(_wcwidth) KT = TypeVar("KT") diff --git a/pyte/streams.py b/pyte/streams.py index 401e814..f728780 100644 --- a/pyte/streams.py +++ b/pyte/streams.py @@ -25,17 +25,17 @@ import re import warnings from collections import defaultdict -from collections.abc import Mapping -from typing import Any, Dict, Optional, TYPE_CHECKING -from collections.abc import Callable, Generator +from typing import TYPE_CHECKING from . import control as ctrl, escape as esc if TYPE_CHECKING: + from collections.abc import Callable, Generator, Mapping + from typing import Any from .screens import Screen + ParserGenerator = Generator[bool | None, str, None] -ParserGenerator = Generator[Optional[bool], str, None] class Stream: """A stream is a state machine that parses a stream of bytes and From c884042c9125b1e639188e6220c7d1e714bf4afa Mon Sep 17 00:00:00 2001 From: deathaxe Date: Thu, 24 Apr 2025 20:54:38 +0200 Subject: [PATCH 2/2] On demand import io in debug mode, only --- pyte/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyte/__init__.py b/pyte/__init__.py index d671ad5..35d0a98 100644 --- a/pyte/__init__.py +++ b/pyte/__init__.py @@ -25,12 +25,13 @@ __all__ = ("Screen", "DiffScreen", "HistoryScreen", "DebugScreen", "Stream", "ByteStream") -import io from .screens import Screen, DiffScreen, HistoryScreen, DebugScreen from .streams import Stream, ByteStream if __debug__: + import io + def dis(chars: bytes | str) -> None: """A :func:`dis.dis` for terminals.