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

fix: handle unions properly #412

Closed
wants to merge 5 commits into from
Closed

fix: handle unions properly #412

wants to merge 5 commits into from

Conversation

guacs
Copy link
Member

@guacs guacs commented Oct 18, 2023

Pull Request Checklist

  • New code has 100% test coverage
  • (If applicable) The prose documentation has been updated to reflect the changes introduced by this PR
  • (If applicable) The reference documentation has been updated to reflect the changes introduced by this PR
  • Pre-Commit Checks were ran and passed
  • Tests were ran and passed

Description

This PR fixes the handling of unions. The use of unwrap_args would result in the incorrect creation of children for the field meta. It would just return a random instance of the union and it fails completely in the case of unions of collection types like list[int] | list[str] where unwrap_args might return (int,) for type_args when in fact we want (list[int], list[str]).

This also fixes the parsing of the model type for batch_factory_type i.e. types like list[SomeModel]. Before, the model was taken from the value of type_args but that gives the incorrect value in the cases of unions like list[SomeModel] | int due to the change in the behavior of type_args. Instead we should just directly parse the model type from the unwrapped_type since the is_batch_factory_type is checked on the unwrapped_annotation anyway.

Close Issue(s)

@guacs guacs requested review from a team as code owners October 18, 2023 08:07
@JacobCoffee
Copy link
Member

Is this ready now?

@guacs
Copy link
Member Author

guacs commented Nov 12, 2023

Is this ready now?

Unfortunately, no.

@guacs
Copy link
Member Author

guacs commented Nov 12, 2023

I'm a bit stuck on this due to the way optional collection types are handled in pydantic v1.

from typing import Optional

from pydantic import BaseModel


OptionalList = Optional[list[str]]
OptionalStr = Optional[str]


class Foo(BaseModel):
    optional_list: OptionalList


optional_list_model_field = Foo.__fields__["optional_list"]
sub_field = optional_list_model_field.sub_fields[0]

print(sub_field.type)  # gives 'str' but we want 'list[str]'

Due to the way the handling of the unions is being changed in this PR, we want to get list[str] instead of str so that the children of the FieldMeta of the optional_list field will have a FieldMeta with the annotation list[str]. Since we are getting back str, the child FieldMeta ends up having the annotation str and the generation of the model instance fails.

@Leobouloc
Copy link

Hi. Is this still work in progress ? Would it be possible to fix this for Pydantic v2 as an intermediate measure ?

@guacs
Copy link
Member Author

guacs commented Dec 30, 2023

Hi. Is this still work in progress ? Would it be possible to fix this for Pydantic v2 as an intermediate measure ?

Yeah, this is still in progress due to pydantic v1 tests failing. I think it might be possible to do this only for v2, though ideally, I'd like it to be done in such a way that we don't have to maintain a separate get_field_value for just v1.

@guacs
Copy link
Member Author

guacs commented Jan 19, 2024

I'm closing this for now due to #366 being fixed in #468. However, these changes should still be made but it may have to wait until we drop support for pydantic v1.

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.

Bug: Broken union type generation since "polyfactory<2.6" (pydantic_factory)
3 participants