From 8e9b478eb4b4dd8f2cf91c2120d5ae222a702980 Mon Sep 17 00:00:00 2001 From: Jarkko Jaakola Date: Tue, 8 Oct 2024 13:19:45 +0300 Subject: [PATCH] chore: remove Python 3.8 related code --- src/karapace/avro_dataclasses/introspect.py | 6 +---- src/karapace/protobuf/proto_normalizations.py | 3 +-- src/karapace/protobuf/type_tree.py | 3 +-- src/karapace/utils.py | 13 --------- tests/unit/test_utils.py | 27 +------------------ 5 files changed, 4 insertions(+), 48 deletions(-) diff --git a/src/karapace/avro_dataclasses/introspect.py b/src/karapace/avro_dataclasses/introspect.py index 64b2e5856..d9efdc2d8 100644 --- a/src/karapace/avro_dataclasses/introspect.py +++ b/src/karapace/avro_dataclasses/introspect.py @@ -10,11 +10,7 @@ from dataclasses import Field, fields, is_dataclass, MISSING from enum import Enum from functools import lru_cache -from typing import Final, Sequence, TYPE_CHECKING, TypeVar, Union - -# Note: It's important get_args and get_origin are imported from typing_extensions -# until support for Python 3.8 is dropped. -from typing_extensions import get_args, get_origin +from typing import Final, Sequence, TYPE_CHECKING, TypeVar, Union, get_args, get_origin import datetime import uuid diff --git a/src/karapace/protobuf/proto_normalizations.py b/src/karapace/protobuf/proto_normalizations.py index 6a5356103..e3539aa46 100644 --- a/src/karapace/protobuf/proto_normalizations.py +++ b/src/karapace/protobuf/proto_normalizations.py @@ -20,7 +20,6 @@ from karapace.protobuf.service_element import ServiceElement from karapace.protobuf.type_element import TypeElement from karapace.protobuf.type_tree import TypeTree -from karapace.utils import remove_prefix from typing import Sequence import abc @@ -90,7 +89,7 @@ class NormalizedOneOfElement(OneOfElement): def normalize_type_field_element(type_field: FieldElement, package: str, type_tree: TypeTree) -> NormalizedFieldElement: sorted_options = None if type_field.options is None else list(sorted(type_field.options, key=sort_by_name)) - field_type_normalized = remove_prefix(remove_prefix(type_field.element_type, "."), f"{package}.") + field_type_normalized = type_field.element_type.removeprefix(".").removeprefix(f"{package}.") reference_in_type_tree = type_tree.type_in_tree(field_type_normalized) google_included_type = ( field_type_normalized in KnownDependency.index_simple or field_type_normalized in KnownDependency.index diff --git a/src/karapace/protobuf/type_tree.py b/src/karapace/protobuf/type_tree.py index f9279e864..71fe83b3d 100644 --- a/src/karapace/protobuf/type_tree.py +++ b/src/karapace/protobuf/type_tree.py @@ -6,7 +6,6 @@ from collections.abc import Iterable, Sequence from karapace.dataclasses import default_dataclass -from karapace.utils import remove_prefix import itertools @@ -84,7 +83,7 @@ def _type_in_tree(tree: TypeTree, remaining_tokens: list[str]) -> TypeTree | Non return tree def type_in_tree(self, queried_type: str) -> TypeTree | None: - return TypeTree._type_in_tree(self, remove_prefix(queried_type, ".").split(".")) + return TypeTree._type_in_tree(self, queried_type.removeprefix(".").split(".")) def expand_missing_absolute_path(self) -> Sequence[str]: oldest_import = self.oldest_matching_import() diff --git a/src/karapace/utils.py b/src/karapace/utils.py index 071b3e9d3..10db7bfdb 100644 --- a/src/karapace/utils.py +++ b/src/karapace/utils.py @@ -246,19 +246,6 @@ def log( self.logger.exception("Error in logging") -def remove_prefix(string: str, prefix: str) -> str: - """ - Not available in python 3.8. - """ - i = 0 - while i < len(string) and i < len(prefix): - if string[i] != prefix[i]: - return string - i += 1 - - return string[i:] - - def shutdown(): """ Send a SIGTERM into the current running application process, which should initiate shutdown logic. diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 51633376c..9a3a33d73 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -3,37 +3,12 @@ See LICENSE for details """ from _pytest.logging import LogCaptureFixture -from karapace.utils import remove_prefix, shutdown +from karapace.utils import shutdown from unittest.mock import patch import logging -def test_remove_prefix_basic() -> None: - result = remove_prefix("hello world", "hello ") - assert result == "world" - - -def test_remove_prefix_empty_prefix() -> None: - result = remove_prefix("hello world", "") - assert result == "hello world" - - -def test_remove_prefix_prefix_not_in_string() -> None: - result = remove_prefix("hello world", "hey ") - assert result == "hello world" - - -def test_remove_prefix_multiple_occurrences_of_prefix() -> None: - result = remove_prefix("hello hello world", "hello ") - assert result == "hello world" - - -def test_remove_prefix_empty_string() -> None: - result = remove_prefix("", "hello ") - assert result == "" - - def test_shutdown(caplog: LogCaptureFixture) -> None: with caplog.at_level(logging.WARNING, logger="karapace.utils"): with patch("karapace.utils.signal") as mock_signal: