1
Fork 0
mirror of https://github.com/RGBCube/GitHubWrapper synced 2025-05-18 15:05:08 +00:00

Make parser work well for objects with unknown names

This commit is contained in:
RGBCube 2022-06-28 16:13:32 +03:00
parent 1bf7eaa932
commit ea545e27a3
3 changed files with 2496 additions and 164 deletions

View file

@ -2,7 +2,8 @@ from __future__ import annotations
__all__ = ("generate",)
from typing import Tuple # , Any
from random import randint
from typing import Tuple, Optional # , Any
types = {
"string": "str",
@ -24,7 +25,7 @@ types = {
def _generate(
obj: dict, /, *, title: str = "GeneratedObject", no_comments: bool = False
obj: dict, /, *, title: Optional[str] = None, no_comments: bool = False
) -> Tuple[str, str]: # sourcery skip: low-code-quality
"""Makes TypedDict from a JSON Schema object.
@ -36,6 +37,9 @@ def _generate(
Returns:
The generated TypedDicts, the result values name and the comments.
"""
if title is None:
title = f"GeneratedObject{randint(100, 200)}"
# The other TypedDicts
result = []
@ -209,7 +213,7 @@ if TYPE_CHECKING:
def generate(
schema: dict, /, *, object_name: str = "GeneratedObject", no_comments: bool = False
schema: dict, /, *, object_name: Optional[str] = None, no_comments: bool = False
) -> str:
generated = _generate(schema, title=object_name, no_comments=no_comments)
return text.format(generated, object_name)
return text.format(generated, object_name or "GeneratedObject")