[#13954] Allows args and arg as schema properties in python client (#13955)

* Add args to reserved words

* arg and args to _arg and _args in templates

* Corrections

* Test added

* Corrections

* Use arg and args as defined properties

* Removed unnecessary assertion

* Suggested change
This commit is contained in:
Mustansir Soni
2022-11-10 04:10:29 +09:00
committed by GitHub
parent ca5d9b5e69
commit 4667b7e471
446 changed files with 1861 additions and 1574 deletions

View File

@@ -50,17 +50,17 @@ class FileIO(io.FileIO):
Note: this class is not immutable
"""
def __new__(cls, arg: typing.Union[io.FileIO, io.BufferedReader]):
if isinstance(arg, (io.FileIO, io.BufferedReader)):
if arg.closed:
def __new__(cls, _arg: typing.Union[io.FileIO, io.BufferedReader]):
if isinstance(_arg, (io.FileIO, io.BufferedReader)):
if _arg.closed:
raise ApiValueError('Invalid file state; file is closed and must be open')
arg.close()
inst = super(FileIO, cls).__new__(cls, arg.name)
super(FileIO, inst).__init__(arg.name)
_arg.close()
inst = super(FileIO, cls).__new__(cls, _arg.name)
super(FileIO, inst).__init__(_arg.name)
return inst
raise ApiValueError('FileIO must be passed arg which contains the open file')
raise ApiValueError('FileIO must be passed _arg which contains the open file')
def __init__(self, arg: typing.Union[io.FileIO, io.BufferedReader]):
def __init__(self, _arg: typing.Union[io.FileIO, io.BufferedReader]):
pass
@@ -151,11 +151,11 @@ class ValidationMetadata(frozendict.frozendict):
class Singleton:
"""
Enums and singletons are the same
The same instance is returned for a given key of (cls, arg)
The same instance is returned for a given key of (cls, _arg)
"""
_instances = {}
def __new__(cls, arg: typing.Any, **kwargs):
def __new__(cls, _arg: typing.Any, **kwargs):
"""
cls base classes: BoolClass, NoneClass, str, decimal.Decimal
The 3rd key is used in the tuple below for a corner case where an enum contains integer 1
@@ -163,15 +163,15 @@ class Singleton:
Decimal('1.0') == Decimal('1')
But if we omitted the 3rd value in the key, then Decimal('1.0') would be stored as Decimal('1')
and json serializing that instance would be '1' rather than the expected '1.0'
Adding the 3rd value, the str of arg ensures that 1.0 -> Decimal('1.0') which is serialized as 1.0
Adding the 3rd value, the str of _arg ensures that 1.0 -> Decimal('1.0') which is serialized as 1.0
"""
key = (cls, arg, str(arg))
key = (cls, _arg, str(_arg))
if key not in cls._instances:
if isinstance(arg, (none_type, bool, BoolClass, NoneClass)):
if isinstance(_arg, (none_type, bool, BoolClass, NoneClass)):
inst = super().__new__(cls)
cls._instances[key] = inst
else:
cls._instances[key] = super().__new__(cls, arg)
cls._instances[key] = super().__new__(cls, _arg)
return cls._instances[key]
def __repr__(self):
@@ -499,12 +499,12 @@ class Schema:
def __remove_unsets(kwargs):
return {key: val for key, val in kwargs.items() if val is not unset}
def __new__(cls, *args: typing.Union[dict, frozendict.frozendict, list, tuple, decimal.Decimal, float, int, str, date, datetime, bool, None, 'Schema'], _configuration: typing.Optional[Configuration] = None, **kwargs: typing.Union[dict, frozendict.frozendict, list, tuple, decimal.Decimal, float, int, str, date, datetime, bool, None, 'Schema', Unset]):
def __new__(cls, *_args: typing.Union[dict, frozendict.frozendict, list, tuple, decimal.Decimal, float, int, str, date, datetime, bool, None, 'Schema'], _configuration: typing.Optional[Configuration] = None, **kwargs: typing.Union[dict, frozendict.frozendict, list, tuple, decimal.Decimal, float, int, str, date, datetime, bool, None, 'Schema', Unset]):
"""
Schema __new__
Args:
args (int/float/decimal.Decimal/str/list/tuple/dict/frozendict.frozendict/bool/None): the value
_args (int/float/decimal.Decimal/str/list/tuple/dict/frozendict.frozendict/bool/None): the value
kwargs (str, int/float/decimal.Decimal/str/list/tuple/dict/frozendict.frozendict/bool/None): dict values
_configuration: contains the Configuration that enables json schema validation keywords
like minItems, minLength etc
@@ -513,14 +513,14 @@ class Schema:
are instance properties if they are named normally :(
"""
__kwargs = cls.__remove_unsets(kwargs)
if not args and not __kwargs:
if not _args and not __kwargs:
raise TypeError(
'No input given. args or kwargs must be given.'
)
if not __kwargs and args and not isinstance(args[0], dict):
__arg = args[0]
if not __kwargs and _args and not isinstance(_args[0], dict):
__arg = _args[0]
else:
__arg = cls.__get_input_dict(*args, **__kwargs)
__arg = cls.__get_input_dict(*_args, **__kwargs)
__from_server = False
__validated_path_to_schemas = {}
__arg = cast_to_allowed_types(
@@ -537,7 +537,7 @@ class Schema:
def __init__(
self,
*args: typing.Union[
*_args: typing.Union[
dict, frozendict.frozendict, list, tuple, decimal.Decimal, float, int, str, date, datetime, bool, None, 'Schema'],
_configuration: typing.Optional[Configuration] = None,
**kwargs: typing.Union[
@@ -2046,8 +2046,8 @@ class ListSchema(
def from_openapi_data_oapg(cls, arg: typing.List[typing.Any], _configuration: typing.Optional[Configuration] = None):
return super().from_openapi_data_oapg(arg, _configuration=_configuration)
def __new__(cls, arg: typing.Union[typing.List[typing.Any], typing.Tuple[typing.Any]], **kwargs: Configuration):
return super().__new__(cls, arg, **kwargs)
def __new__(cls, _arg: typing.Union[typing.List[typing.Any], typing.Tuple[typing.Any]], **kwargs: Configuration):
return super().__new__(cls, _arg, **kwargs)
class NoneSchema(
@@ -2060,8 +2060,8 @@ class NoneSchema(
def from_openapi_data_oapg(cls, arg: None, _configuration: typing.Optional[Configuration] = None):
return super().from_openapi_data_oapg(arg, _configuration=_configuration)
def __new__(cls, arg: None, **kwargs: Configuration):
return super().__new__(cls, arg, **kwargs)
def __new__(cls, _arg: None, **kwargs: Configuration):
return super().__new__(cls, _arg, **kwargs)
class NumberSchema(
@@ -2078,8 +2078,8 @@ class NumberSchema(
def from_openapi_data_oapg(cls, arg: typing.Union[int, float], _configuration: typing.Optional[Configuration] = None):
return super().from_openapi_data_oapg(arg, _configuration=_configuration)
def __new__(cls, arg: typing.Union[decimal.Decimal, int, float], **kwargs: Configuration):
return super().__new__(cls, arg, **kwargs)
def __new__(cls, _arg: typing.Union[decimal.Decimal, int, float], **kwargs: Configuration):
return super().__new__(cls, _arg, **kwargs)
class IntBase:
@@ -2121,8 +2121,8 @@ class IntSchema(IntBase, NumberSchema):
def from_openapi_data_oapg(cls, arg: int, _configuration: typing.Optional[Configuration] = None):
return super().from_openapi_data_oapg(arg, _configuration=_configuration)
def __new__(cls, arg: typing.Union[decimal.Decimal, int], **kwargs: Configuration):
return super().__new__(cls, arg, **kwargs)
def __new__(cls, _arg: typing.Union[decimal.Decimal, int], **kwargs: Configuration):
return super().__new__(cls, _arg, **kwargs)
class Int32Base:
@@ -2275,31 +2275,31 @@ class StrSchema(
def from_openapi_data_oapg(cls, arg: str, _configuration: typing.Optional[Configuration] = None) -> 'StrSchema':
return super().from_openapi_data_oapg(arg, _configuration=_configuration)
def __new__(cls, arg: typing.Union[str, date, datetime, uuid.UUID], **kwargs: Configuration):
return super().__new__(cls, arg, **kwargs)
def __new__(cls, _arg: typing.Union[str, date, datetime, uuid.UUID], **kwargs: Configuration):
return super().__new__(cls, _arg, **kwargs)
class UUIDSchema(UUIDBase, StrSchema):
def __new__(cls, arg: typing.Union[str, uuid.UUID], **kwargs: Configuration):
return super().__new__(cls, arg, **kwargs)
def __new__(cls, _arg: typing.Union[str, uuid.UUID], **kwargs: Configuration):
return super().__new__(cls, _arg, **kwargs)
class DateSchema(DateBase, StrSchema):
def __new__(cls, arg: typing.Union[str, date], **kwargs: Configuration):
return super().__new__(cls, arg, **kwargs)
def __new__(cls, _arg: typing.Union[str, date], **kwargs: Configuration):
return super().__new__(cls, _arg, **kwargs)
class DateTimeSchema(DateTimeBase, StrSchema):
def __new__(cls, arg: typing.Union[str, datetime], **kwargs: Configuration):
return super().__new__(cls, arg, **kwargs)
def __new__(cls, _arg: typing.Union[str, datetime], **kwargs: Configuration):
return super().__new__(cls, _arg, **kwargs)
class DecimalSchema(DecimalBase, StrSchema):
def __new__(cls, arg: str, **kwargs: Configuration):
def __new__(cls, _arg: str, **kwargs: Configuration):
"""
Note: Decimals may not be passed in because cast_to_allowed_types is only invoked once for payloads
which can be simple (str) or complex (dicts or lists with nested values)
@@ -2308,7 +2308,7 @@ class DecimalSchema(DecimalBase, StrSchema):
if one was using it for a StrSchema (where it should be cast to str) or one is using it for NumberSchema
where it should stay as Decimal.
"""
return super().__new__(cls, arg, **kwargs)
return super().__new__(cls, _arg, **kwargs)
class BytesSchema(
@@ -2318,8 +2318,8 @@ class BytesSchema(
"""
this class will subclass bytes and is immutable
"""
def __new__(cls, arg: bytes, **kwargs: Configuration):
return super(Schema, cls).__new__(cls, arg)
def __new__(cls, _arg: bytes, **kwargs: Configuration):
return super(Schema, cls).__new__(cls, _arg)
class FileSchema(
@@ -2343,8 +2343,8 @@ class FileSchema(
- to be able to preserve file name info
"""
def __new__(cls, arg: typing.Union[io.FileIO, io.BufferedReader], **kwargs: Configuration):
return super(Schema, cls).__new__(cls, arg)
def __new__(cls, _arg: typing.Union[io.FileIO, io.BufferedReader], **kwargs: Configuration):
return super(Schema, cls).__new__(cls, _arg)
class BinaryBase:
@@ -2365,8 +2365,8 @@ class BinarySchema(
FileSchema,
]
def __new__(cls, arg: typing.Union[io.FileIO, io.BufferedReader, bytes], **kwargs: Configuration):
return super().__new__(cls, arg)
def __new__(cls, _arg: typing.Union[io.FileIO, io.BufferedReader, bytes], **kwargs: Configuration):
return super().__new__(cls, _arg)
class BoolSchema(
@@ -2379,8 +2379,8 @@ class BoolSchema(
def from_openapi_data_oapg(cls, arg: bool, _configuration: typing.Optional[Configuration] = None):
return super().from_openapi_data_oapg(arg, _configuration=_configuration)
def __new__(cls, arg: bool, **kwargs: ValidationMetadata):
return super().__new__(cls, arg, **kwargs)
def __new__(cls, _arg: bool, **kwargs: ValidationMetadata):
return super().__new__(cls, _arg, **kwargs)
class AnyTypeSchema(
@@ -2416,12 +2416,12 @@ class NotAnyTypeSchema(
def __new__(
cls,
*args,
*_args,
_configuration: typing.Optional[Configuration] = None,
) -> 'NotAnyTypeSchema':
return super().__new__(
cls,
*args,
*_args,
_configuration=_configuration,
)
@@ -2435,8 +2435,8 @@ class DictSchema(
def from_openapi_data_oapg(cls, arg: typing.Dict[str, typing.Any], _configuration: typing.Optional[Configuration] = None):
return super().from_openapi_data_oapg(arg, _configuration=_configuration)
def __new__(cls, *args: typing.Union[dict, frozendict.frozendict], **kwargs: typing.Union[dict, frozendict.frozendict, list, tuple, decimal.Decimal, float, int, str, date, datetime, bool, None, bytes, Schema, Unset, ValidationMetadata]):
return super().__new__(cls, *args, **kwargs)
def __new__(cls, *_args: typing.Union[dict, frozendict.frozendict], **kwargs: typing.Union[dict, frozendict.frozendict, list, tuple, decimal.Decimal, float, int, str, date, datetime, bool, None, bytes, Schema, Unset, ValidationMetadata]):
return super().__new__(cls, *_args, **kwargs)
schema_type_classes = {NoneSchema, DictSchema, ListSchema, NumberSchema, StrSchema, BoolSchema, AnyTypeSchema}