From c510de13d7d1d075d8914098c5d0a83f502d4715 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristi=20V=C3=AEjdea?= Date: Wed, 10 Oct 2018 00:35:43 +0300 Subject: [PATCH] Do not try to use inspect.signature on Python 2.7 Fixes #222. --- src/drf_yasg/inspectors/field.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/drf_yasg/inspectors/field.py b/src/drf_yasg/inspectors/field.py index dbf4400..63d5a6a 100644 --- a/src/drf_yasg/inspectors/field.py +++ b/src/drf_yasg/inspectors/field.py @@ -5,7 +5,6 @@ import operator import uuid from collections import OrderedDict from decimal import Decimal -from inspect import isclass from django.core import validators from django.db import models @@ -18,11 +17,15 @@ from ..utils import decimal_as_float, filter_none, get_serializer_class, get_ser from .base import FieldInspector, NotHandled, SerializerInspector try: - # Python>=3.5 import typing except ImportError: typing = None +try: + from inspect import signature as inspect_signature +except ImportError: + inspect_signature = None + logger = logging.getLogger(__name__) @@ -83,10 +86,10 @@ class InlineSerializerInspector(SerializerInspector): ref_name = self.get_serializer_ref_name(field) - def make_schema_definition(): + def make_schema_definition(serializer=field): properties = OrderedDict() required = [] - for property_name, child in field.fields.items(): + for property_name, child in serializer.fields.items(): property_name = self.get_property_name(property_name) prop_kwargs = { 'read_only': bool(child.read_only) or None @@ -544,13 +547,13 @@ class SerializerMethodFieldInspector(FieldInspector): serializer.read_only = True return self.probe_field_inspectors(serializer, swagger_object_type, use_references, read_only=True) - elif typing: + elif typing and inspect_signature: # look for Python 3.5+ style type hinting of the return value - hint_class = inspect.signature(method).return_annotation + hint_class = inspect_signature(method).return_annotation - if not isclass(hint_class) and hasattr(hint_class, '__args__'): + if not inspect.isclass(hint_class) and hasattr(hint_class, '__args__'): hint_class = hint_class.__args__[0] - if isclass(hint_class) and not issubclass(hint_class, inspect._empty): + if inspect.isclass(hint_class) and not issubclass(hint_class, inspect._empty): type_info = get_basic_type_info_from_hint(hint_class) if type_info is not None: