-
Notifications
You must be signed in to change notification settings - Fork 270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@extend_schema_field
type hint does not accept simple python types
#1212
Comments
|
(I've updated the original description to add the link to our repo where this is used) |
Scratch what I said, it was wrong. The feature I was referring to is basically the same thing, but applies somewhere else (not Okay so the type hint does not cover that, true, but it was not necessary because you are supposed write it normally for regular types. def get_sponsors(self, obj) -> list[str]:
return self.context.get("sponsors") should do just fine. The decorator is just for cases where an actual type hint (->) would be technically wrong and linters would complain (like with |
Added a fix to fully support regular as well as higher order types (union, dict, list). The hint now also includes basic types (str, int, ...). Unfortunately, it is tricky to create a type hint for higher order types that works with all supported py version (3.7-3.12) and at the same time satisfy mypy. If someone has a good idea, feel free to submit a PR. So I would recommend to add a |
I see I forgot to mention why we even added Similarly with If you're telling me it should've worked as-is w/o the decorator, I can open a different issue for that. This was done in the context of a weekend hackathon where a handful of us had never even touched django before. So whatever worked quickly. |
Hello again! So yeah, you're right all I had to do was to use explicit return types to avoid having to use Python: def get_sponsors(self, obj) -> list[str]:
return self.context.get("sponsors", []) OpenAPI spec output: "sponsors": {
"type": "array",
"readOnly": true,
"items": {
"type": "string"
}
}, Typescript output: readonly sponsors!: (string | undefined)[]; Python: # https://github.com/tfranzel/drf-spectacular/issues/1212
@extend_schema_field(list[str]) # type: ignore[arg-type] # pyright: ignore[reportArgumentType]
def get_sponsors(self, obj) -> list[str]:
return self.context.get("sponsors", []) edit: or this actually works in more recent python versions def get_sponsors(self, obj) -> list[str]:
return self.context.get("sponsors", list[str]()) OpenAPI spec output: "sponsors": {
"type": "array",
"readOnly": true,
"items": {
"type": "string"
}
}, Typescript output: readonly sponsors!: string[]; |
Describe the bug
https://drf-spectacular.readthedocs.io/en/latest/customization.html#step-3-extend-schema-field-and-type-hints states that
Which seems correct. However, the type hint does no represent that (see screenshot)
To Reproduce
extend_schema_field(list[str])
Example from source code: https://github.com/BesLogic/releaf-canopeum/blob/0f327ab8769fefc188be26f25ccd5eeeb331cfda/canopeum_backend/canopeum_backend/serializers.py#L142
Expected behavior
Supported simple python types to be allowed (
type[int]
,type[str]
,type[list]
, etc.)Even better if the list type is recursive (something like
_ManyFieldTypes = type[list[_SingleFieldTypes] | _ManyFieldTypes]
, haven't tested, just writing off the top of my head), butlist[Any]
is sufficient for our needsCC @theo-auffeuvre & @NicolasDontigny for visibility on this
The text was updated successfully, but these errors were encountered: