Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@
immutabledict
)
from .format import (
as_date_,
as_datetime_,
as_decimal_,
as_uuid_
as_date,
as_datetime,
as_decimal,
as_uuid
)

def typed_dict_to_instance(t_dict: typing_extensions._TypedDictMeta) -> typing.Mapping: # type: ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,23 @@ def parse_isodate_str(self, datestr: str) -> datetime.date:
DEFAULT_ISOPARSER = CustomIsoparser()

@functools.lru_cache()
def as_date_(arg: str) -> datetime.date:
def as_date(arg: str) -> datetime.date:
"""
type = "string"
format = "date"
"""
return DEFAULT_ISOPARSER.parse_isodate_str(arg)

@functools.lru_cache()
def as_datetime_(arg: str) -> datetime.datetime:
def as_datetime(arg: str) -> datetime.datetime:
"""
type = "string"
format = "date-time"
"""
return DEFAULT_ISOPARSER.parse_isodatetime(arg)

@functools.lru_cache()
def as_decimal_(arg: str) -> decimal.Decimal:
def as_decimal(arg: str) -> decimal.Decimal:
"""
Applicable when storing decimals that are sent over the wire as strings
type = "string"
Expand All @@ -107,7 +107,7 @@ def as_decimal_(arg: str) -> decimal.Decimal:
return decimal.Decimal(arg)

@functools.lru_cache()
def as_uuid_(arg: str) -> uuid.UUID:
def as_uuid(arg: str) -> uuid.UUID:
"""
type = "string"
format = "uuid"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@
immutabledict
)
from .format import (
as_date_,
as_datetime_,
as_decimal_,
as_uuid_
as_date,
as_datetime,
as_decimal,
as_uuid
)

def typed_dict_to_instance(t_dict: typing_extensions._TypedDictMeta) -> typing.Mapping: # type: ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,23 @@ def parse_isodate_str(self, datestr: str) -> datetime.date:
DEFAULT_ISOPARSER = CustomIsoparser()

@functools.lru_cache()
def as_date_(arg: str) -> datetime.date:
def as_date(arg: str) -> datetime.date:
"""
type = "string"
format = "date"
"""
return DEFAULT_ISOPARSER.parse_isodate_str(arg)

@functools.lru_cache()
def as_datetime_(arg: str) -> datetime.datetime:
def as_datetime(arg: str) -> datetime.datetime:
"""
type = "string"
format = "date-time"
"""
return DEFAULT_ISOPARSER.parse_isodatetime(arg)

@functools.lru_cache()
def as_decimal_(arg: str) -> decimal.Decimal:
def as_decimal(arg: str) -> decimal.Decimal:
"""
Applicable when storing decimals that are sent over the wire as strings
type = "string"
Expand All @@ -107,7 +107,7 @@ def as_decimal_(arg: str) -> decimal.Decimal:
return decimal.Decimal(arg)

@functools.lru_cache()
def as_uuid_(arg: str) -> uuid.UUID:
def as_uuid(arg: str) -> uuid.UUID:
"""
type = "string"
format = "uuid"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@
immutabledict
)
from .format import (
as_date_,
as_datetime_,
as_decimal_,
as_uuid_
as_date,
as_datetime,
as_decimal,
as_uuid
)

def typed_dict_to_instance(t_dict: typing_extensions._TypedDictMeta) -> typing.Mapping: # type: ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,23 @@ def parse_isodate_str(self, datestr: str) -> datetime.date:
DEFAULT_ISOPARSER = CustomIsoparser()

@functools.lru_cache()
def as_date_(arg: str) -> datetime.date:
def as_date(arg: str) -> datetime.date:
"""
type = "string"
format = "date"
"""
return DEFAULT_ISOPARSER.parse_isodate_str(arg)

@functools.lru_cache()
def as_datetime_(arg: str) -> datetime.datetime:
def as_datetime(arg: str) -> datetime.datetime:
"""
type = "string"
format = "date-time"
"""
return DEFAULT_ISOPARSER.parse_isodatetime(arg)

@functools.lru_cache()
def as_decimal_(arg: str) -> decimal.Decimal:
def as_decimal(arg: str) -> decimal.Decimal:
"""
Applicable when storing decimals that are sent over the wire as strings
type = "string"
Expand All @@ -107,7 +107,7 @@ def as_decimal_(arg: str) -> decimal.Decimal:
return decimal.Decimal(arg)

@functools.lru_cache()
def as_uuid_(arg: str) -> uuid.UUID:
def as_uuid(arg: str) -> uuid.UUID:
"""
type = "string"
format = "uuid"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@
immutabledict
)
from .format import (
as_date_,
as_datetime_,
as_decimal_,
as_uuid_
as_date,
as_datetime,
as_decimal,
as_uuid
)

def typed_dict_to_instance(t_dict: typing_extensions._TypedDictMeta) -> typing.Mapping: # type: ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,23 @@ def parse_isodate_str(self, datestr: str) -> datetime.date:
DEFAULT_ISOPARSER = CustomIsoparser()

@functools.lru_cache()
def as_date_(arg: str) -> datetime.date:
def as_date(arg: str) -> datetime.date:
"""
type = "string"
format = "date"
"""
return DEFAULT_ISOPARSER.parse_isodate_str(arg)

@functools.lru_cache()
def as_datetime_(arg: str) -> datetime.datetime:
def as_datetime(arg: str) -> datetime.datetime:
"""
type = "string"
format = "date-time"
"""
return DEFAULT_ISOPARSER.parse_isodatetime(arg)

@functools.lru_cache()
def as_decimal_(arg: str) -> decimal.Decimal:
def as_decimal(arg: str) -> decimal.Decimal:
"""
Applicable when storing decimals that are sent over the wire as strings
type = "string"
Expand All @@ -107,7 +107,7 @@ def as_decimal_(arg: str) -> decimal.Decimal:
return decimal.Decimal(arg)

@functools.lru_cache()
def as_uuid_(arg: str) -> uuid.UUID:
def as_uuid(arg: str) -> uuid.UUID:
"""
type = "string"
format = "uuid"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class Model(AnyTypeSchema):
m = Model.validate('12.34')
assert m == '12.34'
assert isinstance(m, str)
assert schemas.as_decimal_(m) == Decimal('12.34')
assert schemas.as_decimal(m) == Decimal('12.34')


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,39 +50,39 @@ def test_ComposedOneOfDifferentTypes(self):
# date
inst = composed_one_of_different_types.ComposedOneOfDifferentTypes.validate('2019-01-10')
assert isinstance(inst, str)
assert schemas.as_date_(inst).year == 2019
assert schemas.as_date_(inst).month == 1
assert schemas.as_date_(inst).day == 10
assert schemas.as_date(inst).year == 2019
assert schemas.as_date(inst).month == 1
assert schemas.as_date(inst).day == 10

# date
inst = composed_one_of_different_types.ComposedOneOfDifferentTypes.validate(date(2019, 1, 10))
assert isinstance(inst, str)
assert schemas.as_date_(inst).year == 2019
assert schemas.as_date_(inst).month == 1
assert schemas.as_date_(inst).day == 10
assert schemas.as_date(inst).year == 2019
assert schemas.as_date(inst).month == 1
assert schemas.as_date(inst).day == 10

# date-time
inst = composed_one_of_different_types.ComposedOneOfDifferentTypes.validate('2020-01-02T03:04:05Z')
assert isinstance(inst, str)
assert schemas.as_datetime_(inst).year == 2020
assert schemas.as_datetime_(inst).month == 1
assert schemas.as_datetime_(inst).day == 2
assert schemas.as_datetime_(inst).hour == 3
assert schemas.as_datetime_(inst).minute == 4
assert schemas.as_datetime_(inst).second == 5
assert schemas.as_datetime(inst).year == 2020
assert schemas.as_datetime(inst).month == 1
assert schemas.as_datetime(inst).day == 2
assert schemas.as_datetime(inst).hour == 3
assert schemas.as_datetime(inst).minute == 4
assert schemas.as_datetime(inst).second == 5
utc_tz = tzutc()
assert schemas.as_datetime_(inst).tzinfo == utc_tz
assert schemas.as_datetime(inst).tzinfo == utc_tz

# date-time
inst = composed_one_of_different_types.ComposedOneOfDifferentTypes.validate(datetime(2020, 1, 2, 3, 4, 5, tzinfo=timezone.utc))
assert isinstance(inst, str)
assert schemas.as_datetime_(inst).year == 2020
assert schemas.as_datetime_(inst).month == 1
assert schemas.as_datetime_(inst).day == 2
assert schemas.as_datetime_(inst).hour == 3
assert schemas.as_datetime_(inst).minute == 4
assert schemas.as_datetime_(inst).second == 5
assert schemas.as_datetime_(inst).tzinfo == utc_tz
assert schemas.as_datetime(inst).year == 2020
assert schemas.as_datetime(inst).month == 1
assert schemas.as_datetime(inst).day == 2
assert schemas.as_datetime(inst).hour == 3
assert schemas.as_datetime(inst).minute == 4
assert schemas.as_datetime(inst).second == 5
assert schemas.as_datetime(inst).tzinfo == utc_tz


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def testDateTimeWithValidations(self):
}
for input_value, expected_datetime in input_value_to_datetime.items():
inst = DateTimeWithValidations.validate(input_value)
assert schemas.as_datetime_(inst) == expected_datetime
assert schemas.as_datetime(inst) == expected_datetime

# value error is raised if an invalid string is passed in
with self.assertRaisesRegex(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def testDateWithValidations(self):
expected_date = date(2020, 1, 1)
for valid_value in valid_values:
inst = DateWithValidations.validate(valid_value)
assert schemas.as_date_(inst) == expected_date
assert schemas.as_date(inst) == expected_date

# value error is raised if an invalid string is passed in
with self.assertRaisesRegex(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ def test_DecimalPayload(self):
m = DecimalPayload.validate('12')
assert isinstance(m, str)
assert m == '12'
assert schemas.as_decimal_(m) == decimal.Decimal('12')
assert schemas.as_decimal(m) == decimal.Decimal('12')

m = DecimalPayload.validate('12.34')
assert isinstance(m, str)
assert m == '12.34'
assert schemas.as_decimal_(m) == decimal.Decimal('12.34')
assert schemas.as_decimal(m) == decimal.Decimal('12.34')

# passing in a Decimal does not work
with self.assertRaises(petstore_api.ApiTypeError):
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/python/tests_manual/test_money.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_Money(self):
'currency': 'usd',
'amount': '10.99'
})
self.assertEqual(schemas.as_decimal_(price.amount), decimal.Decimal('10.99'))
self.assertEqual(schemas.as_decimal(price.amount), decimal.Decimal('10.99'))
self.assertEqual(
price,
dict(currency='usd', amount='10.99')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ def test_UUIDString(self):
u = UUIDString.validate(uuid_value)
self.assertEqual(u, uuid_value)
self.assertTrue(isinstance(u, str))
self.assertEqual(schemas.as_uuid_(u), uuid.UUID(uuid_value))
self.assertEqual(schemas.as_uuid(u), uuid.UUID(uuid_value))

# passing in a uuid also works
u = UUIDString.validate(uuid.UUID(uuid_value))
self.assertEqual(u, uuid_value)
self.assertTrue(isinstance(u, str))
self.assertEqual(schemas.as_uuid_(u), uuid.UUID(uuid_value))
self.assertEqual(schemas.as_uuid(u), uuid.UUID(uuid_value))

# an invalid value does not work
with self.assertRaises(exceptions.ApiValueError):
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/python/schemas/__init__.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ from .validation import (
immutabledict
)
from .format import (
as_date_,
as_datetime_,
as_decimal_,
as_uuid_
as_date,
as_datetime,
as_decimal,
as_uuid
)

def typed_dict_to_instance(t_dict: typing_extensions._TypedDictMeta) -> typing.Mapping: # type: ignore
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/python/schemas/format.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,23 @@ class CustomIsoparser(parser.isoparser):
DEFAULT_ISOPARSER = CustomIsoparser()

@functools.lru_cache()
def as_date_(arg: str) -> datetime.date:
def as_date(arg: str) -> datetime.date:
"""
type = "string"
format = "date"
"""
return DEFAULT_ISOPARSER.parse_isodate_str(arg)

@functools.lru_cache()
def as_datetime_(arg: str) -> datetime.datetime:
def as_datetime(arg: str) -> datetime.datetime:
"""
type = "string"
format = "date-time"
"""
return DEFAULT_ISOPARSER.parse_isodatetime(arg)

@functools.lru_cache()
def as_decimal_(arg: str) -> decimal.Decimal:
def as_decimal(arg: str) -> decimal.Decimal:
"""
Applicable when storing decimals that are sent over the wire as strings
type = "string"
Expand All @@ -107,7 +107,7 @@ def as_decimal_(arg: str) -> decimal.Decimal:
return decimal.Decimal(arg)

@functools.lru_cache()
def as_uuid_(arg: str) -> uuid.UUID:
def as_uuid(arg: str) -> uuid.UUID:
"""
type = "string"
format = "uuid"
Expand Down