From 715801a43c76dc3ba85b02fc974bd642218ea442 Mon Sep 17 00:00:00 2001 From: FriedrichFroebel Date: Sat, 28 Oct 2023 12:38:46 +0200 Subject: [PATCH] attempt to fix types --- brother_ql_web/utils.py | 11 +++++++++-- brother_ql_web/web.py | 6 +++--- tests/test_cli.py | 2 ++ tests/test_configuration.py | 2 ++ tests/test_font_helpers.py | 2 ++ 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/brother_ql_web/utils.py b/brother_ql_web/utils.py index 7cb301c..9347c42 100644 --- a/brother_ql_web/utils.py +++ b/brother_ql_web/utils.py @@ -1,5 +1,6 @@ from __future__ import annotations +import sys from typing import cast from brother_ql.backends import backend_factory, BrotherQLBackendGeneric, guess_backend @@ -7,6 +8,12 @@ from brother_ql_web.configuration import Configuration from brother_ql_web.font_helpers import get_fonts +if sys.version_info < (3, 9): + from typing import Type + BACKEND_TYPE = Type[BrotherQLBackendGeneric] +else: + BACKEND_TYPE = type[BrotherQLBackendGeneric] + def collect_fonts(configuration: Configuration) -> dict[str, dict[str, str]]: fonts = get_fonts() @@ -23,7 +30,7 @@ class BackendGuessingError(ValueError): pass -def get_backend_class(configuration: Configuration) -> type[BrotherQLBackendGeneric]: +def get_backend_class(configuration: Configuration) -> BACKEND_TYPE: try: selected_backend = guess_backend(configuration.printer.printer) except ValueError: @@ -31,6 +38,6 @@ def get_backend_class(configuration: Configuration) -> type[BrotherQLBackendGene "Couln't guess the backend to use from the printer string descriptor" ) return cast( - type[BrotherQLBackendGeneric], + BACKEND_TYPE, backend_factory(selected_backend)["backend_class"], ) diff --git a/brother_ql_web/web.py b/brother_ql_web/web.py index d622f8a..541a9b6 100644 --- a/brother_ql_web/web.py +++ b/brother_ql_web/web.py @@ -5,7 +5,6 @@ from typing import Any, cast import bottle -from brother_ql.backends import BrotherQLBackendGeneric from brother_ql_web.configuration import Configuration from brother_ql_web.labels import ( LabelParameters, @@ -14,6 +13,7 @@ generate_label, print_label, ) +from brother_ql_web.utils import BACKEND_TYPE logger = logging.getLogger(__name__) @@ -133,7 +133,7 @@ def print_text() -> dict[str, bool | str]: Configuration, get_config("brother_ql_web.configuration") ), backend_class=cast( - type[BrotherQLBackendGeneric], + BACKEND_TYPE, get_config("brother_ql_web.backend_class"), ), ) @@ -152,7 +152,7 @@ def main( configuration: Configuration, fonts: dict[str, dict[str, str]], label_sizes: list[tuple[str, str]], - backend_class: type[BrotherQLBackendGeneric], + backend_class: BACKEND_TYPE, ) -> None: app = bottle.default_app() app.config["brother_ql_web.configuration"] = configuration diff --git a/tests/test_cli.py b/tests/test_cli.py index d35c0c0..21d2f5b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from argparse import Namespace from io import StringIO from contextlib import redirect_stderr diff --git a/tests/test_configuration.py b/tests/test_configuration.py index 97ce383..ff3eb98 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import json from tempfile import NamedTemporaryFile from typing import cast diff --git a/tests/test_font_helpers.py b/tests/test_font_helpers.py index 39060a9..79fd4c6 100644 --- a/tests/test_font_helpers.py +++ b/tests/test_font_helpers.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from unittest import TestCase from brother_ql_web.font_helpers import get_fonts