When a strawberry type definition is generic, it reports a deprecation warning about _type_definition
, but the alternative __strawberry_definition__
is not set.
Reproduction:
from typing import Generic, TypeVar
import strawberry
from strawberry.printer import print_schema
T = TypeVar("T")
@strawberry.type
class GenericType(Generic[T]):
@strawberry.field
def type_field(self) -> T:
...
@strawberry.type
class Query:
@strawberry.field
def int_generic(self) -> GenericType[int]:
...
print(print_schema(strawberry.Schema(query=Query)))
print(GenericType[int]._type_definition)
print(GenericType[int].__strawberry_definition__)
This prints:
__strawberry_definition__
not _type_definition
__strawberry_definition__
does not exsting.type IntGenericType {
typeField: Int!
}
type Query {
intGeneric: IntGenericType!
}
/Users/james/repos/strawberry-example/.venv/lib/python3.11/site-packages/strawberry/utils/deprecations.py:23: UserWarning: _type_definition is deprecated, use __strawberry_definition__ instead
self.warn()
StrawberryObjectDefinition(name='GenericType', is_input=False, is_interface=False, origin=<class '__main__.GenericType'>, description=None, interfaces=[], extend=False, directives=(), is_type_of=None, resolve_type=None, _fields=[Field(name='type_field',type=<strawberry.type.StrawberryTypeVar object at 0x104362d10>,default=<dataclasses._MISSING_TYPE object at 0x1030875d0>,default_factory=<dataclasses._MISSING_TYPE object at 0x1030875d0>,init=False,repr=False,hash=None,compare=False,metadata=mappingproxy({}),kw_only=True,_field_type=_FIELD)], concrete_of=None, type_var_map={})
Traceback (most recent call last):
File "/Users/james/repos/strawberry-example/models.py", line 26, in <module>
print(GenericType[int].__strawberry_definition__)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/james/.pyenv/versions/3.11.5/lib/python3.11/typing.py", line 1295, in __getattr__
raise AttributeError(attr)
AttributeError: __strawberry_definition__
I think the expected behaviour is that it should be possible to print just __strawberry_definition__
, with no deprecation warning. This works if the type is not generic.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too