From 2b0239758476cd0a73054d609ce7dcae055fbf55 Mon Sep 17 00:00:00 2001 From: Kostiantyn Goloveshko Date: Sun, 8 Dec 2024 23:50:15 +0200 Subject: [PATCH] Fixup name collision --- python/src/cucumber_messages/__init__.py | 3 +++ python/src/cucumber_messages/json_converter.py | 15 +++++++-------- python/tests/test_model_load.py | 4 ++-- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/python/src/cucumber_messages/__init__.py b/python/src/cucumber_messages/__init__.py index ed77d2d0..2581aa1f 100644 --- a/python/src/cucumber_messages/__init__.py +++ b/python/src/cucumber_messages/__init__.py @@ -1,5 +1,8 @@ from . import json_converter from .messages import * +PickleStepType = Type ExpressionType = Type1 + +del Type del Type1 diff --git a/python/src/cucumber_messages/json_converter.py b/python/src/cucumber_messages/json_converter.py index 206f8235..8b31c583 100644 --- a/python/src/cucumber_messages/json_converter.py +++ b/python/src/cucumber_messages/json_converter.py @@ -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 @@ -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, @@ -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(']')] @@ -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] @@ -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) @@ -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) \ No newline at end of file diff --git a/python/tests/test_model_load.py b/python/tests/test_model_load.py index 0e33906b..fec83bd1 100644 --- a/python/tests/test_model_load.py +++ b/python/tests/test_model_load.py @@ -39,7 +39,7 @@ TestStepResult, TestStepStarted, Timestamp, - Type, + PickleStepType, ) resource_path = Path(__file__).parent.absolute() / "data" @@ -118,7 +118,7 @@ uri="samples/minimal/minimal.feature", name="cukes", language="en", - steps=[PickleStep(ast_node_ids=["1"], id="3", type=Type.context, text="I have 42 cukes in my belly")], + steps=[PickleStep(ast_node_ids=["1"], id="3", type=PickleStepType.context, text="I have 42 cukes in my belly")], tags=[], ast_node_ids=["2"], ),