Typing problems with Pydantic/FastAPI integration Resolver #1885
Unanswered
invokermain
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi all,
I have the following set up, but the typing either doesn't play nice with type checking, or I receive type related errors when querying.
Example Application
Dependencies
Strawberry:
FastAPI:
MyPY
Code
Result
This works as intended. However running
mypy
on this file (with the extension) provides the following error, unsure if it's related:Problem
However I start running into trouble when type hinting the return of
get_document
and/or static type checking .Consider the following situations
Situation 1: Adding correct Type Hint
This will raise:
TypeError: Query fields cannot be resolved. Unexpected type '<class '__main__.DocumentModel'>'
. Even though the code is functionally identical. This is pretty strange and non-intuitive.Mypy shows the same traceback.
Situation 2: Adding incorrect Type Hint
This works but break static type checking using the IDE (PyCharm).
Mypy shows the same traceback.
Situation 3: Using
from_pydantic
, no Type HintThis works but has no typing.
Mypy shows the same traceback.
Situation 4: Using
from_pydantic
, with effectively correctDocumentType
type hintThis works but break static type checking using the IDE (PyCharm).
Mypy shows the same traceback.
Situation 5: Using
from_pydantic
, with technically correct type hintThis fails to launch with the following traceback:
Mypy shows the same traceback.
Dicussion
What is the correct Typehint for the Resolver in this situation? Feels like Situation4 is the correct one, but
from_pydantic
seems to have the wrong typing then?Is the type checking only supposed to work with mypy? i.e. are IDE type warnings expected?
What's with the
mypy
issue? Seems like a separate bug?Let me know if you want me to raise issues off the back of any of this, or anyone has any suggestions as to correct usage.
Beta Was this translation helpful? Give feedback.
All reactions