mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Fix typing.Optional normalization.
This commit is contained in:
parent
af2573dbee
commit
43c565d39b
4 changed files with 326 additions and 318 deletions
|
|
@ -37,12 +37,10 @@ def normalize_type_annotation(item: dict):
|
|||
|
||||
|
||||
@functools.lru_cache()
|
||||
def normalize_type(string, required=True):
|
||||
def normalize_type(string):
|
||||
if not string:
|
||||
return "typing.Any"
|
||||
|
||||
union = "typing.Union" if required else "typing.Optional"
|
||||
|
||||
lower = string.lower()
|
||||
split = lower.split()
|
||||
|
||||
|
|
@ -52,7 +50,7 @@ def normalize_type(string, required=True):
|
|||
if "or" in split:
|
||||
split_types = string.split(" or ")
|
||||
norm_str = ", ".join(map(normalize_type, map(str.strip, split_types)))
|
||||
return f"{union}[{norm_str}]"
|
||||
return f"typing.Union[{norm_str}]"
|
||||
if "number" in lower:
|
||||
return normalize_type(string.replace("number", "").strip())
|
||||
if lower in ["true", "false"]:
|
||||
|
|
@ -83,3 +81,12 @@ def get_returning(description):
|
|||
break
|
||||
|
||||
return return_type, sentence + "."
|
||||
|
||||
|
||||
def normalize_optional(python_type: str, required: bool = True) -> str:
|
||||
if not required:
|
||||
if "typing.Union" in python_type:
|
||||
python_type = python_type.replace("typing.Union", "typing.Optional")
|
||||
else:
|
||||
python_type = f"typing.Optional[{python_type}]"
|
||||
return python_type
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue