Skip to content
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

Handle list[str] as a field, etc. #105

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dmontagu
Copy link

@dmontagu dmontagu commented Jun 11, 2024

Ran into a bug trying to use list[int] as a field type on a BaseModel in Python 3.9. (Note it is also broken with Python 3.10.). Interestingly, things are handled correctly in these versions of python when using typing.List[str], etc., but not when using a bare list[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]) returns True, but Python still raises an error when you try to call issubclass(list[str], BaseModel). If you explicitly check for being an instance of types.GenericAlias though, that resolves this issue, as even in these versions of python isinstance(list[str], types.GenericAlias) is True.


I'll note though that you have the comment:

    # NOTE: this has to come before any `issubclass()` checks, because annotated
    # generic types aren't valid arguments to `issubclass`

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_ to issubclass. But I figured there was a higher chance that that could cause other issues, so I didn't bother.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant