diff --git a/RELEASE.md b/RELEASE.md new file mode 100644 index 0000000000..d3879a408b --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,3 @@ +Release type: patch + +This release fixes an issubclass test failing for `Literal`s in the experimental `pydantic` integration. diff --git a/strawberry/experimental/pydantic/object_type.py b/strawberry/experimental/pydantic/object_type.py index 806e967f4b..1e171263c6 100644 --- a/strawberry/experimental/pydantic/object_type.py +++ b/strawberry/experimental/pydantic/object_type.py @@ -6,6 +6,7 @@ from pydantic import BaseModel from pydantic.fields import ModelField +from typing_extensions import Literal import strawberry from strawberry.arguments import UNSET @@ -24,6 +25,10 @@ def replace_pydantic_types(type_: Any): + origin = getattr(type_, "__origin__", None) + if origin is Literal: + # Literal does not have types in its __args__ so we return early + return type_ if hasattr(type_, "__args__"): new_type = type_.copy_with( tuple(replace_pydantic_types(t) for t in type_.__args__) diff --git a/tests/experimental/pydantic/test_fields.py b/tests/experimental/pydantic/test_fields.py index 2da0ae7fcf..dd83d24847 100644 --- a/tests/experimental/pydantic/test_fields.py +++ b/tests/experimental/pydantic/test_fields.py @@ -1,6 +1,7 @@ import pytest import pydantic +from typing_extensions import Literal import strawberry from strawberry.type import StrawberryOptional @@ -128,3 +129,20 @@ class Model(pydantic.BaseModel): @strawberry.experimental.pydantic.type(Model) class Type: field: strawberry.auto + + +def test_literal_types(): + class Model(pydantic.BaseModel): + field: Literal["field"] + + @strawberry.experimental.pydantic.type(Model) + class Type: + field: strawberry.auto + + definition: TypeDefinition = Type._type_definition + assert definition.name == "Type" + + [field] = definition.fields + + assert field.python_name == "field" + assert field.type == Literal["field"]