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: no blank children names #666

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

iloveitaly
Copy link
Contributor

Description

ensure no blank field names on children variants

@iloveitaly iloveitaly requested a review from guacs as a code owner March 19, 2025 20:00
field_name="",
# this is a fake field name, but it makes it possible to debug which type variant
# is the source of an exception downstream
field_name=f"{field_name}__{arg.__name__}",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason we couldn't

  1. just use field_name
  2. specify arg.__name__ as a separate field on field_meta

current approach here feels bad

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, I believe the main use is mapping this back to the model itself. For subfields, guessing currently unused so field_name could work and feels preferable to above.

What is the issue with having no field name? Is this to improve exception message based on test? If that is the case then maybe could update on that side instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the issue with having no field name?

I ran into this when attempting to run a FactoryModel.build() command. The ParameterException said that a factory for a type was missing, but it the name was blank, so I couldn't determine which field was causing the error.

For subfields, guessing currently unused so field_name could work and feels preferable to above

Great, I'll change the PR to just use field_name instead.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran into this when attempting to run a FactoryModel.build() command. The ParameterException said that a factory for a type was missing, but it the name was blank, so I couldn't determine which field was causing the error.

Where is that error coming from? Does the type matter there?

Probably clearer to have field_name regardless

@@ -106,7 +106,7 @@ def is_new_type(annotation: Any) -> "TypeGuard[type[NewType]]":


def is_annotated(annotation: Any) -> bool:
"""Determine whether a given annotation is 'typing.Annotated'.
"""Determine whether a given annotation is 'typing.Annotated', or a similar typing annotation such as Optional.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct? The function looks for annotated origin or similar

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is why i left this comment, it's pretty tricky

In [12]: a
Out[12]: typing.Optional[int]

In [13]: a.__args__
Out[13]: (int, NoneType)

In [14]: b = int | None
Out[14]: int | None

In [15]: b.__args__
Out[15]: (int, NoneType)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this match _AnnotatedAlias or other conditions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with _AnnotatedAlias, not sure...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on testing in terminal think this handles this correctly so fine as is?

>>> from polyfactory.utils.predicates import is_annotated
>>> from typing import Optional
>>> is_annotated(Optional[int])
False
>>> is_annotated(int | None)
False

Copy link
Collaborator

@adhtruong adhtruong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good. Can you review failing tests?

class OptionalFormTwo(BaseModel):
optional_custom_type_second_form: CustomType | None

@classmethod
Copy link
Collaborator

@adhtruong adhtruong Mar 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed? This is not used by the factory

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.

2 participants