Skip to content

Commit

Permalink
introduce direction literal / import consolidation #582
Browse files Browse the repository at this point in the history
  • Loading branch information
tfranzel committed Oct 31, 2021
1 parent 56d720d commit 7fd64d3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 17 deletions.
7 changes: 7 additions & 0 deletions drf_spectacular/drainage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
from collections import defaultdict
from typing import DefaultDict

if sys.version_info >= (3, 8):
from typing import Final, Literal, _TypedDictMeta # type: ignore[attr-defined] # noqa: F401
else:
from typing_extensions import ( # type: ignore[attr-defined] # noqa: F401
Final, Literal, _TypedDictMeta,
)


class GeneratorStats:
_warn_cache: DefaultDict[str, int] = defaultdict(int)
Expand Down
5 changes: 3 additions & 2 deletions drf_spectacular/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Type, Union

from drf_spectacular.plumbing import OpenApiGeneratorExtension
from drf_spectacular.utils import Direction

if TYPE_CHECKING:
from rest_framework.views import APIView
Expand Down Expand Up @@ -58,7 +59,7 @@ def get_name(self) -> Optional[str]:
""" return str for overriding default name extraction """
return None

def map_serializer(self, auto_schema: 'AutoSchema', direction):
def map_serializer(self, auto_schema: 'AutoSchema', direction: Direction):
""" override for customized serializer mapping """
return auto_schema._map_serializer(self.target_class, direction, bypass_extensions=True)

Expand All @@ -82,7 +83,7 @@ def get_name(self) -> Optional[str]:
return None

@abstractmethod
def map_serializer_field(self, auto_schema: 'AutoSchema', direction):
def map_serializer_field(self, auto_schema: 'AutoSchema', direction: Direction):
""" override for customized serializer field mapping """
pass # pragma: no cover

Expand Down
7 changes: 1 addition & 6 deletions drf_spectacular/plumbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
from enum import Enum
from typing import DefaultDict, Generic, List, Optional, Type, TypeVar, Union

if sys.version_info >= (3, 8):
from typing import Literal, _TypedDictMeta # type: ignore[attr-defined]
else:
from typing_extensions import Literal, _TypedDictMeta # type: ignore[attr-defined]

import inflection
import uritemplate
from django.apps import apps
Expand All @@ -42,7 +37,7 @@
from rest_framework.utils.mediatypes import _MediaType
from uritemplate import URITemplate

from drf_spectacular.drainage import cache, error, warn
from drf_spectacular.drainage import Literal, _TypedDictMeta, cache, error, warn
from drf_spectacular.settings import spectacular_settings
from drf_spectacular.types import (
DJANGO_PATH_CONVERTER_MAPPING, OPENAPI_TYPE_MAPPING, PYTHON_TYPE_MAPPING, OpenApiTypes,
Expand Down
11 changes: 2 additions & 9 deletions drf_spectacular/utils.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
import inspect
import sys
from typing import Any, Callable, Dict, List, Optional, Tuple, Type, TypeVar, Union

from rest_framework.fields import Field, empty
from rest_framework.serializers import Serializer
from rest_framework.settings import api_settings

from drf_spectacular.drainage import (
error, get_view_method_names, isolate_view_method, set_override, warn,
Final, Literal, error, get_view_method_names, isolate_view_method, set_override, warn,
)
from drf_spectacular.types import OpenApiTypes, _KnownPythonTypes

if sys.version_info >= (3, 8):
from typing import Final, Literal
else:
from typing_extensions import Final, Literal


_SerializerType = Union[Serializer, Type[Serializer]]
_FieldType = Union[Field, Type[Field]]

_ParameterLocationType = Literal['query', 'path', 'header', 'cookie']
Direction = Literal['request', 'response']


class PolymorphicProxySerializer(Serializer):
Expand Down

0 comments on commit 7fd64d3

Please sign in to comment.