Skip to content

Commit 845665c

Browse files
author
Lev Rubel
authored
Update fastapi pydantic (#92)
* updated code to work with latest fastapi & pydantic (breaking changes, this version is not compatible with pydantic <= 1.0) * Bump version: 0.2.3 → 0.2.4 * temporary change of cov-fail-under
1 parent c96cb7b commit 845665c

File tree

13 files changed

+85
-66
lines changed

13 files changed

+85
-66
lines changed

.github/workflows/pythonpackage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ jobs:
3030
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
3131
- name: Test with pytest
3232
run: |
33-
py.test --cov=fastapi_contrib --cov-report=term-missing:skip-covered --cov-branch --cov-fail-under=99
33+
py.test --cov=fastapi_contrib --cov-report=term-missing:skip-covered --cov-branch --cov-fail-under=98

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ deploy:
1515
on:
1616
tags: true
1717
repo: identixone/fastapi_contrib
18-
python: 3.7
18+
python: 3.8

HISTORY.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
History
33
=======
44

5+
0.2.4
6+
--------
7+
8+
General changes:
9+
10+
* Added support for latest FastAPI & Pydantic (fastapi==0.52.0, pydantic==1.4)
11+
12+
Breaking changes:
13+
14+
* Due to the breaking changes between pydantic < 1.0 and pydantic > 1.0 this version will only work with the latter.
15+
516
0.2.0
617
--------
718

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019, DATA CORPORATION OÜ
3+
Copyright (c) 2020, DATA CORPORATION OÜ
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ lint: ## check style with flake8
5454
flake8 fastapi_contrib tests
5555

5656
test: ## run tests quickly with the default Python
57-
py.test --cov=fastapi_contrib --cov-report=term-missing:skip-covered --cov-branch --cov-fail-under=99
57+
py.test --cov=fastapi_contrib --cov-report=term-missing:skip-covered --cov-branch --cov-fail-under=98
5858

5959
test-all: ## run tests on every Python version with tox
6060
tox

fastapi_contrib/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
__author__ = """Lev Rubel"""
66
__email__ = 'l@datacorp.ee'
7-
__version__ = '0.2.3'
7+
__version__ = '0.2.4'

fastapi_contrib/exception_handlers.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from fastapi import FastAPI
44
from fastapi.exceptions import RequestValidationError
5+
from pydantic import EnumError, StrRegexError
56
from starlette.exceptions import HTTPException as StarletteHTTPException
67
from starlette.requests import Request
78

@@ -19,38 +20,38 @@ def parse_error(
1920
:param raw: Whether this is a raw error or wrapped pydantic error
2021
:return: dict with name of the field (or "__all__") and actual message
2122
"""
22-
if err.type_ == "value_error.str.regex":
23-
message = "Provided value doesn't match valid format."
24-
elif err.type_ == "type_error.enum":
23+
24+
if isinstance(err.exc, EnumError):
2525
message = "One or more values provided are not valid."
26+
elif isinstance(err.exc, StrRegexError):
27+
message = "Provided value doesn't match valid format."
2628
else:
27-
message = err.msg or ""
29+
message = str(err.exc) or ""
2830

29-
if err.type_.startswith("value_error.error_code"):
30-
error_code = int(err.type_.split(".")[-1])
31+
if hasattr(err.exc, "code") and err.exc.code.startswith("error_code"):
32+
error_code = int(err.exc.code.split(".")[-1])
3133
else:
3234
# default error code for non-custom errors is 400
3335
error_code = 400
3436

3537
if not raw:
36-
if len(err.loc) == 2:
37-
if str(err.loc[0]) in ["body", "query"]:
38-
name = err.loc[1]
38+
if len(err.loc_tuple()) == 2:
39+
if str(err.loc_tuple()[0]) in ["body", "query"]:
40+
name = err.loc_tuple()[1]
3941
else:
40-
name = err.loc[0]
41-
elif len(err.loc) == 1:
42-
if str(err.loc[0]) == "body":
42+
name = err.loc_tuple()[0]
43+
elif len(err.loc_tuple()) == 1:
44+
if str(err.loc_tuple()[0]) == "body":
4345
name = "__all__"
4446
else:
45-
name = str(err.loc[0])
47+
name = str(err.loc_tuple()[0])
4648
else:
4749
name = "__all__"
4850
else:
49-
if len(err.loc) == 2:
50-
name = str(err.loc[0])
51-
# message = f"{str(err.loc[1]).lower()}: {message}"
52-
elif len(err.loc) == 1:
53-
name = str(err.loc[0])
51+
if len(err.loc_tuple()) == 2:
52+
name = str(err.loc_tuple()[0])
53+
elif len(err.loc_tuple()) == 1:
54+
name = str(err.loc_tuple()[0])
5455
else:
5556
name = "__all__"
5657

fastapi_contrib/serializers/utils.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
from typing import Type, List, Set, Mapping, Tuple, Sequence
33

44
from pydantic import Required, create_model
5-
from pydantic.fields import Shape
5+
from pydantic.fields import (
6+
SHAPE_LIST,
7+
SHAPE_SET,
8+
SHAPE_MAPPING,
9+
SHAPE_TUPLE,
10+
SHAPE_TUPLE_ELLIPSIS,
11+
SHAPE_SEQUENCE,
12+
)
613

714
from fastapi_contrib.serializers.common import AbstractMeta, Serializer
815

@@ -11,6 +18,7 @@ class FieldGenerationMode(int, Enum):
1118
"""
1219
Defines modes in which fields of decorated serializer should be generated.
1320
"""
21+
1422
REQUEST = 1
1523
RESPONSE = 2
1624

@@ -43,17 +51,17 @@ def gen_model(cls: Type, mode: FieldGenerationMode):
4351
if t.required:
4452
f_def = Required
4553

46-
if t.shape == Shape.LIST:
54+
if t.shape == SHAPE_LIST:
4755
_type = List[t.type_]
48-
elif t.shape == Shape.SET:
56+
elif t.shape == SHAPE_SET:
4957
_type = Set[t.type_]
50-
elif t.shape == Shape.MAPPING:
58+
elif t.shape == SHAPE_MAPPING:
5159
_type = Mapping[t.key_field.type_, t.type_]
52-
elif t.shape == Shape.TUPLE:
60+
elif t.shape == SHAPE_TUPLE:
5361
_type = t.type_
54-
elif t.shape == Shape.TUPLE_ELLIPS:
62+
elif t.shape == SHAPE_TUPLE_ELLIPSIS:
5563
_type = Tuple[t.type_, ...]
56-
elif t.shape == Shape.SEQUENCE:
64+
elif t.shape == SHAPE_SEQUENCE:
5765
_type = Sequence[t.type_]
5866
else:
5967
_type = t.type_
@@ -82,4 +90,6 @@ def gen_model(cls: Type, mode: FieldGenerationMode):
8290

8391
return model
8492

85-
return create_model(f"{cls.__name__}Response", __config__=Config, **_fields)
93+
return create_model(
94+
f"{cls.__name__}Response", __config__=Config, **_fields
95+
)

requirements_dev.txt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
pip==19.3.1
1+
pip==20.0.2
22
bumpversion==0.5.3
3-
wheel==0.33.6
4-
watchdog==0.9.0
5-
Sphinx==2.3.1
3+
wheel==0.34.2
4+
watchdog==0.10.2
5+
Sphinx==2.4.3
66
twine==3.1.1
77

88
contextvars==2.4;python_version<"3.7"
9-
fastapi==0.42.0
9+
fastapi==0.52.0
10+
pydantic==1.4
1011
jaeger-client>=4.1.0
1112
opentracing>=2.2.0
1213
motor>=2.0.0
@@ -15,8 +16,8 @@ ujson>=1.35
1516

1617
coverage==5.0.3
1718
flake8==3.7.9
18-
pytest==5.3.2
19+
pytest==5.3.5
1920
pytest-asyncio==0.10.0
2021
pytest-runner==5.2
2122
pytest-cov==2.8.1
22-
tox==3.14.3
23+
tox==3.14.5

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.2.3
2+
current_version = 0.2.4
33
commit = True
44
tag = True
55

0 commit comments

Comments
 (0)