From 3745b6bea5827017f1d616b1856a232b3fc25d01 Mon Sep 17 00:00:00 2001 From: Darren Eberly Date: Mon, 1 Jun 2020 21:14:11 -0400 Subject: [PATCH] refactor: changed RawProperties in properties.py to be a TypedDict --- pytiled_parser/properties.py | 25 ++++++++++++++++++------- tests/test_tiled_object.py | 2 +- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/pytiled_parser/properties.py b/pytiled_parser/properties.py index 9e198af..bc20c30 100644 --- a/pytiled_parser/properties.py +++ b/pytiled_parser/properties.py @@ -1,25 +1,36 @@ from pathlib import Path from typing import Dict, List, NamedTuple, Optional, Union +from typing_extensions import TypedDict + from .common_types import Color -Property = Union[int, float, Path, str, bool, Color] -class +Property = Union[float, Path, str, bool, Color] +RawProperty = Union[float, str, bool] -RawProperties = List[Dict[str, Property]] + +class RawProperties(TypedDict): + """A dictionary of raw properties.""" + + name: str + type: str + value: RawProperty Properties = Dict[str, Property] -def cast(raw: RawProperties) -> Dict[str, Property]: +def cast(raw: List[RawProperties]) -> Dict[str, Property]: final: Properties = {} + value: Property + for prop in raw: - value = prop["value"] if prop["type"] == "file": - value = Path(str(value)) + value = Path(str(prop["value"])) elif prop["type"] == "color": - value = Color(str(value)) + value = Color(str(prop["value"])) + else: + value = prop["value"] final[str(prop["name"])] = value return final diff --git a/tests/test_tiled_object.py b/tests/test_tiled_object.py index 3fe1b0e..02d99b1 100644 --- a/tests/test_tiled_object.py +++ b/tests/test_tiled_object.py @@ -1100,6 +1100,6 @@ OBJECTS = ELLIPSES + RECTANGLES + POINTS + TILES + POLYGONS + POLYLINES + TEXTS @pytest.mark.parametrize("raw_object_json,expected", OBJECTS) def test_parse_layer(raw_object_json, expected): raw_object = json.loads(raw_object_json) - result = tiled_object._cast_tiled_object(raw_object) + result = tiled_object.cast(raw_object) assert result == expected