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 aBaseModel
in 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.GenericAlias
though, 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.