Skip to content

Commit

Permalink
Fixup name collision
Browse files Browse the repository at this point in the history
  • Loading branch information
elchupanebrej committed Dec 8, 2024
1 parent 98588f3 commit d610e0c
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions python/src/cucumber_messages/json_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
from dataclasses import fields, is_dataclass, Field, MISSING
from datetime import datetime, date
from enum import Enum
from typing import Any, Dict, List, Optional, Union, get_args, get_origin, TypeVar, Type, Tuple
from typing import Any, Dict, List, Optional, Union, get_args, get_origin, TypeVar, Type as TypingType, Tuple

T = TypeVar('T')


def camel_to_snake(name: str) -> str:
"""Convert string from camelCase to snake_case."""
# Validate field name - must start with letter or underscore and contain only alphanumeric and underscore
Expand Down Expand Up @@ -40,7 +39,7 @@ def __init__(self, module_scope: types.ModuleType):
self.module_scope = module_scope
self.type_cache = {}

def resolve_container_type(self, container_name: str) -> Type:
def resolve_container_type(self, container_name: str) -> TypingType:
container_types = {
'Sequence': typing.Sequence,
'List': list,
Expand All @@ -52,7 +51,7 @@ def resolve_container_type(self, container_name: str) -> Type:
raise ValueError(f"Unsupported container type: {container_name}")
return container_types[container_name]

def parse_generic_type(self, type_str: str) -> Tuple[Type, List[Type]]:
def parse_generic_type(self, type_str: str) -> Tuple[TypingType, List[TypingType]]:
container_name = type_str[:type_str.index('[')].strip()
args_str = type_str[type_str.index('[') + 1:type_str.rindex(']')]

Expand All @@ -65,7 +64,7 @@ def parse_generic_type(self, type_str: str) -> Tuple[Type, List[Type]]:

return container_type, arg_types

def resolve_type(self, type_str: str) -> Type:
def resolve_type(self, type_str: str) -> TypingType:
if type_str in self.type_cache:
return self.type_cache[type_str]

Expand All @@ -84,7 +83,7 @@ def resolve_type(self, type_str: str) -> Type:
self.type_cache[type_str] = resolved
return resolved

def _resolve_union_type(self, type_str: str) -> Type:
def _resolve_union_type(self, type_str: str) -> TypingType:
types_str = [t.strip() for t in type_str.split('|')]
resolved_types = [
self.resolve_type(t)
Expand Down Expand Up @@ -293,11 +292,11 @@ def to_dict(self) -> Dict[str, Any]:
return DataclassJSONEncoder.encode(self)

@classmethod
def from_json(cls: Type[T], json_str: str, module_scope: Optional[types.ModuleType] = None) -> T:
def from_json(cls: TypingType[T], json_str: str, module_scope: Optional[types.ModuleType] = None) -> T:
data = json.loads(json_str)
return cls.from_dict(data, module_scope)

@classmethod
def from_dict(cls: Type[T], data: Dict[str, Any], module_scope: Optional[types.ModuleType] = None) -> T:
def from_dict(cls: TypingType[T], data: Dict[str, Any], module_scope: Optional[types.ModuleType] = None) -> T:
decoder = DataclassJSONDecoder(module_scope or sys.modules[cls.__module__])
return decoder.decode(data, cls)

0 comments on commit d610e0c

Please sign in to comment.