Skip to content

Commit

Permalink
Fix unicode attribute error if typing installed on py2.7 (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
elliott-omosheye authored and axnsan12 committed Jun 12, 2019
1 parent 91ef83e commit b5aba72
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/drf_yasg/inspectors/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import inspect
import logging
import operator
import sys
import uuid
from collections import OrderedDict
from decimal import Decimal
Expand Down Expand Up @@ -489,6 +490,9 @@ def hint_class_issubclass(hint_class, check_class):
(datetime.date, (openapi.TYPE_STRING, openapi.FORMAT_DATE)),
]

if sys.version_info < (3, 0):
hinting_type_info.append((unicode, (openapi.TYPE_STRING, None)))

if typing:
def inspect_collection_hint_class(hint_class):
args = hint_class.__args__
Expand Down
7 changes: 6 additions & 1 deletion testproj/users/serializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys

from django.contrib.auth.models import User
from rest_framework import serializers

Expand All @@ -6,7 +8,10 @@

try:
import typing # noqa: F401
from .method_serializers_with_typing import MethodFieldExampleSerializer
if sys.version_info >= (3, 4):
from .method_serializers_with_typing import MethodFieldExampleSerializer
else:
from .method_serializers_without_typing import MethodFieldExampleSerializer
except ImportError:
from .method_serializers_without_typing import MethodFieldExampleSerializer

Expand Down
1 change: 1 addition & 0 deletions tests/test_schema_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ def action_post(self, request):

@pytest.mark.parametrize('choices, expected_type', [
(['A', 'B'], openapi.TYPE_STRING),
([u'A', u'B'], openapi.TYPE_STRING),
([123, 456], openapi.TYPE_INTEGER),
([1.2, 3.4], openapi.TYPE_NUMBER),
(['A', 456], openapi.TYPE_STRING)
Expand Down
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ isolated_build_env = .package

# https://docs.djangoproject.com/en/dev/faq/install/#what-python-version-can-i-use-with-django
envlist =
py27-django111-drf39-typing,
py{27,34,35,36}-django111-drf{37,38,39},
py{34,35,36,37}-django20-drf{37,38,39},
py{35,36,37}-django21-drf{37,38,39},
Expand All @@ -26,6 +27,8 @@ deps =
drf38: djangorestframework>=3.8.0,<3.9
drf39: djangorestframework>=3.9,<3.10

typing: typing>=3.6.6

# test with the latest build of django-rest-framework to get early warning of compatibility issues
djmaster: https://github.com/encode/django-rest-framework/archive/master.tar.gz
djmaster: https://github.com/django/django/archive/master.tar.gz
Expand Down

0 comments on commit b5aba72

Please sign in to comment.