コード例 #1
0
def test_type_hint_extraction(no_warnings, type_hint, ref_schema):
    if sys.version_info >= (3, 10):
        # This partially defeats the purpose of the test, as this is not what
        # happens in reality for 3.10. However, the other tests cover annotation
        # usage sufficiently. i'm unaware on how to do this programmatically
        # in the context of PEP 563. suggestions welcome!
        def func():
            pass  # pragma: no cover

        func.__annotations__['return'] = type_hint
    else:
        # this is perfectly fine up to 3.9
        def func() -> type_hint:
            pass  # pragma: no cover

    # check expected resolution
    schema = resolve_type_hint(typing.get_type_hints(func).get('return'))
    assert json.dumps(schema) == json.dumps(ref_schema)

    # check schema validity
    class XSerializer(serializers.Serializer):
        x = serializers.SerializerMethodField()

    XSerializer.get_x = func

    class XView(generics.RetrieveAPIView):
        serializer_class = XSerializer

    validate_schema(generate_schema('/x', view=XView))
コード例 #2
0
ファイル: openapi.py プロジェクト: jorrete/drf-spectacular
    def _map_response_type_hint(self, method):
        hint = get_override(method, 'field') or typing.get_type_hints(method).get('return')

        if is_serializer(hint) or is_field(hint):
            return self._map_serializer_field(force_instance(hint), 'response')

        try:
            return resolve_type_hint(hint)
        except UnableToProceedError:
            warn(
                f'unable to resolve type hint for function "{method.__name__}". consider '
                f'using a type hint or @extend_schema_field. defaulting to string.'
            )
            return build_basic_type(OpenApiTypes.STR)
コード例 #3
0
def test_type_hint_extraction(no_warnings, type_hint, ref_schema):
    def func() -> type_hint:
        pass  # pragma: no cover

    # check expected resolution
    schema = resolve_type_hint(typing.get_type_hints(func).get('return'))
    assert json.dumps(schema) == json.dumps(ref_schema)

    # check schema validity
    class XSerializer(serializers.Serializer):
        x = serializers.SerializerMethodField()
    XSerializer.get_x = func

    class XView(generics.RetrieveAPIView):
        serializer_class = XSerializer

    validate_schema(generate_schema('/x', view=XView))