Browse Source

♻ Simplify code syntax in several places (#1753)

Co-authored-by: Sebastián Ramírez <[email protected]>
pull/1754/head
Yurii Karabas 5 years ago
committed by GitHub
parent
commit
55b9faeb48
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      fastapi/applications.py
  2. 2
      fastapi/dependencies/utils.py
  3. 5
      fastapi/encoders.py
  4. 4
      fastapi/openapi/constants.py
  5. 2
      fastapi/security/http.py

7
fastapi/applications.py

@ -117,11 +117,8 @@ class FastAPI(Starlette):
def setup(self) -> None:
if self.openapi_url:
server_urls = set()
for server_data in self.servers:
url = server_data.get("url")
if url:
server_urls.add(url)
urls = (server_data.get("url") for server_data in self.servers)
server_urls = {url for url in urls if url}
async def openapi(req: Request) -> JSONResponse:
root_path = req.scope.get("root_path", "").rstrip("/")

2
fastapi/dependencies/utils.py

@ -746,7 +746,7 @@ def get_body_field(*, dependant: Dependant, name: str) -> Optional[ModelField]:
first_param = flat_dependant.body_params[0]
field_info = get_field_info(first_param)
embed = getattr(field_info, "embed", None)
body_param_names_set = set([param.name for param in flat_dependant.body_params])
body_param_names_set = {param.name for param in flat_dependant.body_params}
if len(body_param_names_set) == 1 and not embed:
return get_schema_compatible_field(field=first_param)
# If one field requires to embed, all have to be embedded

5
fastapi/encoders.py

@ -1,3 +1,4 @@
from collections import defaultdict
from enum import Enum
from pathlib import PurePath
from types import GeneratorType
@ -15,9 +16,9 @@ DictIntStrAny = Dict[Union[int, str], Any]
def generate_encoders_by_class_tuples(
type_encoder_map: Dict[Any, Callable]
) -> Dict[Callable, Tuple]:
encoders_by_classes: Dict[Callable, List] = {}
encoders_by_classes: Dict[Callable, List] = defaultdict(list)
for type_, encoder in type_encoder_map.items():
encoders_by_classes.setdefault(encoder, []).append(type_)
encoders_by_classes[encoder].append(type_)
encoders_by_class_tuples: Dict[Callable, Tuple] = {}
for encoder, classes in encoders_by_classes.items():
encoders_by_class_tuples[encoder] = tuple(classes)

4
fastapi/openapi/constants.py

@ -1,3 +1,3 @@
METHODS_WITH_BODY = set(("GET", "HEAD", "POST", "PUT", "DELETE", "PATCH"))
STATUS_CODES_WITH_NO_BODY = set((100, 101, 102, 103, 204, 304))
METHODS_WITH_BODY = {"GET", "HEAD", "POST", "PUT", "DELETE", "PATCH"}
STATUS_CODES_WITH_NO_BODY = {100, 101, 102, 103, 204, 304}
REF_PREFIX = "#/components/schemas/"

2
fastapi/security/http.py

@ -86,7 +86,7 @@ class HTTPBasic(HTTPBase):
except (ValueError, UnicodeDecodeError, binascii.Error):
raise invalid_user_credentials_exc
username, separator, password = data.partition(":")
if not (separator):
if not separator:
raise invalid_user_credentials_exc
return HTTPBasicCredentials(username=username, password=password)

Loading…
Cancel
Save