Drf-yasg: Optional type hint schema generation support broken

Created on 29 Jul 2019  路  2Comments  路  Source: axnsan12/drf-yasg

1.15.1 broke support for Optional type hinting schema generation.

  • Support for Optional was added in #272
  • I believe that support was broken in #363 or 69b628a7afdbc9de5f8872a34669a4b1a827155b (part of 1.15.1).

Python versions on which the problem as been reproduced:

  • CPython 3.7.3
  • CPython 3.6.8
  • PyPy 7.1.1 (3.6 compatible)

To reproduce, create a Serializer like:

from typing import Optional

class MySerializer(Serializer):
    x = SerializerMethodField()

    def get_x(self, instance) -> Optional[int]:
        pass

In 1.15.0, this is properly detected in the generated schema as an integer.
In 1.16.0 (I couldn't get 1.15.1 to run properly), it is incorrectly detected as a string.

Most helpful comment

I think you're right, it looks like it was broken in 69b628a by removing the if not inspect.isclass(hint_class) and hasattr(hint_class, '__args__'): check in SerializerMethodFieldInspector. I have a PR coming up.

All 2 comments

I think you're right, it looks like it was broken in 69b628a by removing the if not inspect.isclass(hint_class) and hasattr(hint_class, '__args__'): check in SerializerMethodFieldInspector. I have a PR coming up.

428 has been merged. Closing.

Was this page helpful?
0 / 5 - 0 ratings