Skip to content

Commit

Permalink
Fix typing in protobuf/extensions_element
Browse files Browse the repository at this point in the history
  • Loading branch information
tvainika committed Jun 12, 2023
1 parent 4d4ece2 commit e94882c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
25 changes: 14 additions & 11 deletions karapace/protobuf/extensions_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,36 @@
"""
# Ported from square/wire:
# wire-library/wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/internal/parser/ExtensionsElement.kt

from __future__ import annotations

from dataclasses import dataclass
from karapace.protobuf.kotlin_wrapper import KotlinRange
from karapace.protobuf.location import Location
from karapace.protobuf.utils import append_documentation, MAX_TAG_VALUE
from typing import List, Union


@dataclass
class ExtensionsElement:
location: Location
documentation: str = ""
values: List[Union[int, KotlinRange]] = None
values: list[int | KotlinRange] | None = None

def to_schema(self) -> str:
result = []
result: list[str] = []
append_documentation(result, self.documentation)
result.append("extensions ")

formatted_values = []
for value in self.values:
if isinstance(value, int):
formatted_values.append(str(value))
elif isinstance(value, KotlinRange):
max_value = str(value.maximum) if value.maximum < MAX_TAG_VALUE else "max"
formatted_values.append(f"{value.minimum} to {max_value}")
else:
raise ValueError(f"values should be a list of integers or KotlinRange, got {value!r}")
if self.values is not None:
for value in self.values:
if isinstance(value, int):
formatted_values.append(str(value))
elif isinstance(value, KotlinRange):
max_value = str(value.maximum) if value.maximum < MAX_TAG_VALUE else "max"
formatted_values.append(f"{value.minimum} to {max_value}")
else:
raise ValueError(f"values should be a list of integers or KotlinRange, got {value!r}")

result.append(", ".join(formatted_values))
result.append(";\n")
Expand Down
3 changes: 0 additions & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ ignore_errors = True
[mypy-karapace.protobuf.schema]
ignore_errors = True

[mypy-karapace.protobuf.extensions_element]
ignore_errors = True

[mypy-karapace.protobuf.protobuf_to_dict]
ignore_errors = True

Expand Down

0 comments on commit e94882c

Please sign in to comment.