Skip to content

Commit

Permalink
Merge pull request #406 from Yelp/jtwang_use_nongql_jsonschema
Browse files Browse the repository at this point in the history
Change jsonschema extra to format-nongpl. Hackily add rfc3339-validator to requirements-dev to support this change. Also #ignore a bunch of lines that are failing mypy checks on master.
  • Loading branch information
jtwang authored Dec 14, 2023
2 parents f242852 + 40fbe01 commit d8db76f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
32 changes: 16 additions & 16 deletions bravado_core/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,62 +141,62 @@ def wrapper(validatable_primitive):
BASE64_BYTE_FORMAT = SwaggerFormat(
format='byte',
# Note: In Python 3, this requires a bytes-like object as input
to_wire=lambda b: six.ensure_str(base64.b64encode(b), encoding=str('ascii')),
to_python=lambda s: base64.b64decode(six.ensure_binary(s, encoding=str('ascii'))),
to_wire=lambda b: six.ensure_str(base64.b64encode(b), encoding=str('ascii')), # type: ignore
to_python=lambda s: base64.b64decode(six.ensure_binary(s, encoding=str('ascii'))), # type: ignore
validate=NO_OP, # jsonschema validates string
description='Converts [wire]string:byte <=> python bytes',
)

DEFAULT_FORMATS = {
'byte': SwaggerFormat(
format='byte',
to_wire=lambda b: b if isinstance(b, str) else str(b),
to_python=lambda s: s if isinstance(s, str) else str(s),
to_wire=lambda b: b if isinstance(b, str) else str(b), # type: ignore
to_python=lambda s: s if isinstance(s, str) else str(s), # type: ignore
validate=NO_OP, # jsonschema validates string
description='Converts [wire]string:byte <=> python byte',
),
'date': SwaggerFormat(
format='date',
to_wire=lambda d: d.isoformat(),
to_python=lambda d: dateutil.parser.parse(d).date(),
to_wire=lambda d: d.isoformat(), # type: ignore
to_python=lambda d: dateutil.parser.parse(d).date(), # type: ignore
validate=NO_OP, # jsonschema validates date
description='Converts [wire]string:date <=> python datetime.date',
),
# Python has no double. float is C's double in CPython
'double': SwaggerFormat(
format='double',
to_wire=lambda d: d if isinstance(d, float) else float(d),
to_python=lambda d: d if isinstance(d, float) else float(d),
to_wire=lambda d: d if isinstance(d, float) else float(d), # type: ignore
to_python=lambda d: d if isinstance(d, float) else float(d), # type: ignore
validate=NO_OP, # jsonschema validates number
description='Converts [wire]number:double <=> python float',
),
'date-time': SwaggerFormat(
format='date-time',
to_wire=lambda dt: (dt if dt.tzinfo else pytz.utc.localize(dt)).isoformat(),
to_python=lambda dt: dateutil.parser.parse(dt),
to_wire=lambda dt: (dt if dt.tzinfo else pytz.utc.localize(dt)).isoformat(), # type: ignore
to_python=lambda dt: dateutil.parser.parse(dt), # type: ignore
validate=NO_OP, # jsonschema validates date-time
description=(
'Converts string:date-time <=> python datetime.datetime'
),
),
'float': SwaggerFormat(
format='float',
to_wire=lambda f: f if isinstance(f, float) else float(f),
to_python=lambda f: f if isinstance(f, float) else float(f),
to_wire=lambda f: f if isinstance(f, float) else float(f), # type: ignore
to_python=lambda f: f if isinstance(f, float) else float(f), # type: ignore
validate=NO_OP, # jsonschema validates number
description='Converts [wire]number:float <=> python float',
),
'int32': SwaggerFormat(
format='int32',
to_wire=lambda i: i if isinstance(i, int) else int(i),
to_python=lambda i: i if isinstance(i, int) else int(i),
to_wire=lambda i: i if isinstance(i, int) else int(i), # type: ignore
to_python=lambda i: i if isinstance(i, int) else int(i), # type: ignore
validate=NO_OP, # jsonschema validates integer
description='Converts [wire]integer:int32 <=> python int',
),
'int64': SwaggerFormat(
format='int64',
to_wire=lambda i: i if isinstance(i, long) else long(i),
to_python=lambda i: i if isinstance(i, long) else long(i),
to_wire=lambda i: i if isinstance(i, long) else long(i), # type: ignore
to_python=lambda i: i if isinstance(i, long) else long(i), # type: ignore
validate=NO_OP, # jsonschema validates integer
description='Converts [wire]integer:int64 <=> python long',
),
Expand Down
5 changes: 5 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# TODO: avoids an issue with hanging forever in tests (CORESERV-12009).
# Inclusion of rfc3339-validator here is a workaround for an issue where
# pip is not installing this package (defined by `jsonschema[format-nongpl]`).
# This issue sounds similar to https://github.com/pypa/pip/issues/3903, but
# that was fixed in pip 20.3 and we're still seeing this issue on 22.0.2.
jsonschema<4
mock
mypy
Expand All @@ -7,6 +11,7 @@ pre-commit
pytest
pytest-benchmark[histogram]
pytest-cov
rfc3339-validator
types-mock
types-python-dateutil
types-pytz
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
],
install_requires=[
"jsonref",
"jsonschema[format]>=2.5.1",
"jsonschema[format-nongpl]>=2.5.1",
"python-dateutil",
"pyyaml",
'requests',
Expand Down
4 changes: 2 additions & 2 deletions tests/swagger20_validator/format_validator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ def validate_dummy(dummy_string):

DummyFormat = SwaggerFormat(
format="dummy",
to_wire=lambda x: x,
to_python=lambda x: x,
to_wire=lambda x: x, # type: ignore
to_python=lambda x: x, # type: ignore
validate=validate_dummy,
description="dummy format",
)
Expand Down
4 changes: 2 additions & 2 deletions tests/validate/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def validate_email_address(email_address):

email_address_format = SwaggerFormat(
format='email_address',
to_wire=lambda x: x,
to_python=lambda x: x,
to_wire=lambda x: x, # type: ignore
to_python=lambda x: x, # type: ignore
validate=validate_email_address,
description='blah',
)

0 comments on commit d8db76f

Please sign in to comment.