Skip to content

Commit

Permalink
Fix check of Literal in replace_pydantic_types (#1445)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Patrick Arminio <[email protected]>
  • Loading branch information
3 people authored Nov 25, 2021
1 parent 4ffe209 commit 7e47b1b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Release type: patch

This release fixes an issubclass test failing for `Literal`s in the experimental `pydantic` integration.
5 changes: 5 additions & 0 deletions strawberry/experimental/pydantic/object_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__)
Expand Down
18 changes: 18 additions & 0 deletions tests/experimental/pydantic/test_fields.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

import pydantic
from typing_extensions import Literal

import strawberry
from strawberry.type import StrawberryOptional
Expand Down Expand Up @@ -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"]

0 comments on commit 7e47b1b

Please sign in to comment.