diff --git a/godot_parser/files.py b/godot_parser/files.py index 6115055..0fc3282 100644 --- a/godot_parser/files.py +++ b/godot_parser/files.py @@ -7,7 +7,6 @@ Optional, Sequence, Type, - TypeVar, Union, cast, ) @@ -39,9 +38,6 @@ ] -GDFileType = TypeVar("GDFileType", bound="GDFile") - - class GodotFileException(Exception): """Thrown when there are errors in a Godot file""" @@ -304,12 +300,12 @@ def get_node(self, path: str = ".") -> Optional[GDNodeSection]: return node.section if node is not None else None @classmethod - def parse(cls: Type[GDFileType], contents: str): + def parse(cls, contents: str): """Parse the contents of a Godot file""" return cls.from_parser(scene_file.parse_string(contents, parseAll=True)) @classmethod - def load(cls: Type[GDFileType], filepath: str): + def load(cls, filepath: str): with open(filepath, "r", encoding="utf-8") as ifile: try: file = cls.parse(ifile.read()) @@ -322,7 +318,7 @@ def load(cls: Type[GDFileType], filepath: str): return file @classmethod - def from_parser(cls: Type[GDFileType], parse_result): + def from_parser(cls, parse_result): first_section = parse_result[0] if first_section.header.name == "gd_scene": scene = GDScene.__new__(GDScene) @@ -334,7 +330,6 @@ def from_parser(cls: Type[GDFileType], parse_result): return resource return cls(*parse_result) - raise GodotFileException("Unknown GDFileType %s" % first_section.header.name) def write(self, filename: str): """Writes this to a file""" @@ -386,6 +381,7 @@ def remove_at(self, index: int): section = self._sections.pop(index) if section.header.name in ["ext_resource", "sub_resource"]: self.load_steps -= 1 + return section def remove_unused_resources(self): self._remove_unused_resources(self.get_ext_resources(), ExtResource) @@ -460,3 +456,6 @@ def __init__(self, *sections: GDSection) -> None: class GDResource(GDCommonFile): def __init__(self, *sections: GDSection) -> None: super().__init__("gd_resource", *sections) + + +GDFileType = Union[GDFile, GDScene, GDResource] diff --git a/requirements_test.txt b/requirements_test.txt index c6bdd41..f595fea 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -3,3 +3,4 @@ black isort mypy pylint==2.17.7 +beartype diff --git a/tests/test_parser.py b/tests/test_parser.py index 1eeaf69..99f3120 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -1,15 +1,18 @@ import os -from typing import Optional, Union +from typing import Optional import unittest from pyparsing import ParseException +from beartype.door import is_bearable + from godot_parser import GDFile, GDObject, GDSection, GDSectionHeader, Vector2, parse -from godot_parser.files import GDResource, GDScene +from godot_parser.files import GDFileType HERE = os.path.dirname(__file__) -TEST_CASES = [ + +TEST_CASES: list[tuple[str, GDFileType]] = [ ( "[gd_scene load_steps=5 format=2]", GDFile(GDSection(GDSectionHeader("gd_scene", load_steps=5, format=2))), @@ -51,7 +54,7 @@ GDSectionHeader("sub_resource", type="RectangleShape2D", id=1), extents=Vector2(12.7855, 17.0634), other=None, - **{"with spaces": 1} + **{"with spaces": 1}, ) ), ), @@ -71,7 +74,7 @@ "update": 0, "values": [Vector2(0, 0), Vector2(1, 0)], } - } + }, ) ), ), @@ -108,7 +111,7 @@ "0:0/0": 0, "0:0/0/physics_layer_0/linear_velocity": Vector2(0, 0), "0:0/0/physics_layer_0/angular_velocity": 0.0, - } + }, ) ), ), @@ -116,11 +119,9 @@ class TestParser(unittest.TestCase): - """ """ - - def _run_test(self, string: str, expected): + def _run_test(self, string: str, expected: GDFileType): """Run a set of tests""" - parse_result: Optional[Union[GDScene, GDResource]] = None + parse_result: Optional[GDFileType] = None try: parse_result = parse(string) if expected == "error": @@ -128,8 +129,8 @@ def _run_test(self, string: str, expected): string, parse_result, ) - else: - self.assertEqual(parse_result, expected) + condition = is_bearable(parse_result, GDFileType) + assert condition, f"Expected {type(expected)}, got {type(parse_result)}" except ParseException as e: if expected != "error": print(string)