From 9e97298f7caedcd74ca20803acfd52e70b261cf2 Mon Sep 17 00:00:00 2001 From: RGBCube <78925721+RGBCube@users.noreply.github.com> Date: Sun, 26 Jun 2022 17:21:55 +0300 Subject: [PATCH] Fix parser --- tools/a.py | 30 +++++++++++++++--------------- tools/unschema/parser.py | 8 +++++--- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/tools/a.py b/tools/a.py index 4febe90..ebe6b4a 100644 --- a/tools/a.py +++ b/tools/a.py @@ -6,17 +6,6 @@ if TYPE_CHECKING: from typing_extensions import NotRequired -class Repository(TypedDict): - # Example: 42 - id: NotRequired[int] - # Example: MDEwOlJlcG9zaXRvcnkxMjk2MjY5 - node_id: NotRequired[str] - # Example: Team Environment - name: NotRequired[str] - # Example: octocat/Hello-World - full_name: NotRequired[str] - - class LicenseSimple(TypedDict): # Example: mit key: NotRequired[str] @@ -31,7 +20,6 @@ class LicenseSimple(TypedDict): node_id: NotRequired[str] # Format: uri html_url: NotRequired[str] - license: NotRequired[Optional[LicenseSimple]] class SimpleUser(TypedDict): @@ -82,8 +70,6 @@ class SimpleUser(TypedDict): site_admin: NotRequired[bool] # Example: "2020-07-09T00:17:55Z" starred_at: NotRequired[str] - organization: NotRequired[Optional[SimpleUser]] - forks: NotRequired[int] class Permissions(TypedDict): @@ -92,7 +78,6 @@ class Permissions(TypedDict): triage: NotRequired[bool] push: NotRequired[bool] maintain: NotRequired[bool] - permissions: NotRequired[Permissions] class SimpleUser(TypedDict): @@ -143,6 +128,21 @@ class SimpleUser(TypedDict): site_admin: NotRequired[bool] # Example: "2020-07-09T00:17:55Z" starred_at: NotRequired[str] + + +class Repository(TypedDict): + # Example: 42 + id: NotRequired[int] + # Example: MDEwOlJlcG9zaXRvcnkxMjk2MjY5 + node_id: NotRequired[str] + # Example: Team Environment + name: NotRequired[str] + # Example: octocat/Hello-World + full_name: NotRequired[str] + license: NotRequired[Optional[LicenseSimple]] + organization: NotRequired[Optional[SimpleUser]] + forks: NotRequired[int] + permissions: NotRequired[Permissions] owner: NotRequired[SimpleUser] private: NotRequired[bool] # Format: uri diff --git a/tools/unschema/parser.py b/tools/unschema/parser.py index 03ae2d8..97fe3d2 100644 --- a/tools/unschema/parser.py +++ b/tools/unschema/parser.py @@ -119,7 +119,7 @@ def generate_typed_dicts_from_json_schema( total = ", total=False" if obj.get("additionalProperties") else "" annotation = obj_title = obj.get("title", title).replace(" ", "") - result.append(f"class {obj_title}(TypedDict{total}):") + typed_dict = [f"class {obj_title}(TypedDict{total}):"] for key, value in obj_properties.items(): extras, param_annotation = generate_typed_dicts_from_json_schema( @@ -130,7 +130,7 @@ def generate_typed_dicts_from_json_schema( param_annotation = f"NotRequired[{param_annotation}]" if not no_comments: - result.extend( + typed_dict.extend( [ f" # Format: {fmt}" if (fmt := value.get("format")) else "", f" # Example: {', '.join(str(ex) for ex in exs)}" @@ -139,7 +139,9 @@ def generate_typed_dicts_from_json_schema( ] ) - result.append(f" {key}: {param_annotation}") + typed_dict.append(f" {key}: {param_annotation}") + + result.extend(typed_dict) else: annotation = "dict"