Handle list[str] as a field, etc.#105
Open
dmontagu wants to merge 3 commits intographql-python:mainfrom
Open
Conversation
17efcc7 to
99530f5
Compare
necaris
previously approved these changes
Dec 10, 2024
Collaborator
|
This will likely close #83 |
This also adds an exclusion for more recent versions of Python and older versions of Pydantic, as those aren't compatible.
99530f5 to
205d2b2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Ran into a bug trying to use
list[int]as a field type on aBaseModelin Python 3.9. (Note it is also broken with Python 3.10.). Interestingly, things are handled correctly in these versions of python when usingtyping.List[str], etc., but not when using a barelist[str](even though that is allowed in these python versions).This pull request fixes the handling of
list[str]as a field type.The issue is that, in Python 3.9 (and 3.10),
inspect.isclass(list[str])returnsTrue, but Python still raises an error when you try to callissubclass(list[str], BaseModel). If you explicitly check for being an instance oftypes.GenericAliasthough, that resolves this issue, as even in these versions of pythonisinstance(list[str], types.GenericAlias)isTrue.I'll note though that you have the comment:
right below the change I made. I suspect this is below the pydantic model check so that you handle things properly if the pydantic model has that field (maybe? maybe not..), but if it works it might alternatively make sense to just move that branch above this if statement which is passing the
type_toissubclass. But I figured there was a higher chance that that could cause other issues, so I didn't bother.