Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support assert statements inside validators #653

Merged
merged 13 commits into from Aug 15, 2019

handle assertion error name, fix build

  • Loading branch information
David Montague
David Montague committed Jul 20, 2019
commit 123e09c15d798a9527733aae65dfa07f0dd7690a
@@ -109,21 +109,15 @@ def flatten_errors(

@lru_cache()
def get_exc_type(cls: Type[Exception]) -> str:
type_names = {
TypeError: 'type_error',
ValueError: 'value_error',
AssertionError: 'assertion_error'
}

try:
# use bare name
return type_names[cls]
except KeyError:
pass

# if it's not one of the errors above, we just take the lowercase of the exception name
if issubclass(cls, AssertionError):
return 'assertion_error'

base_name = 'type_error' if issubclass(cls, TypeError) else 'value_error'
if cls in (TypeError, ValueError):
# just TypeError or ValueError, no extra code
return base_name

# if it's not a TypeError or ValueError, we just take the lowercase of the exception name
# no chaining or snake case logic, use "code" for more complex error types.
for base_class, base_name in type_names.items():
if issubclass(cls, base_class):
code = getattr(cls, 'code', None) or cls.__name__.replace('Error', '').lower()
return base_name + '.' + code
code = getattr(cls, 'code', None) or cls.__name__.replace('Error', '').lower()
return base_name + '.' + code
@@ -649,7 +649,7 @@ def check_a(cls, v):

with pytest.raises(ValidationError) as exc_info:
Model(a='snap')
assert exc_info.value.errors() == [{'loc': ('a',), 'msg': f'invalid a\nassert False', 'type': 'assertion_error'}]
injected_by_pytest = '\nassert False'
assert exc_info.value.errors() == [{'loc': ('a',), 'msg': f'invalid a{injected_by_pytest}', 'type': 'assertion_error'}]

assert exc_info.value.errors() == [
{'loc': ('a',), 'msg': f'invalid a{injected_by_pytest}', 'type': 'assertion_error'}
]
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.