-
-
Notifications
You must be signed in to change notification settings - Fork 95
fix: no blank children names #666
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
Open
iloveitaly
wants to merge
4
commits into
litestar-org:main
Choose a base branch
from
iloveitaly:children-names
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1064b3f
test: tests for field name in optional custom types
iloveitaly 94cb7aa
docs: inline documentation for less obvious code paths
iloveitaly 059923f
fix: add field name to union type field value generation
iloveitaly 3f202c2
tests: clean up test structure
iloveitaly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
from decimal import Decimal | ||
from ipaddress import IPv4Address, IPv4Interface, IPv4Network, IPv6Address, IPv6Interface, IPv6Network | ||
from pathlib import Path | ||
from typing import Callable, Dict, FrozenSet, List, Literal, Optional, Sequence, Set, Tuple, Type, Union | ||
from typing import Any, Callable, Dict, FrozenSet, List, Literal, Optional, Sequence, Set, Tuple, Type, Union | ||
from uuid import UUID | ||
|
||
import pytest | ||
|
@@ -64,8 +64,10 @@ | |
validator, | ||
) | ||
|
||
from polyfactory.exceptions import ParameterException | ||
from polyfactory.factories import DataclassFactory | ||
from polyfactory.factories.pydantic_factory import _IS_PYDANTIC_V1, ModelFactory | ||
from polyfactory.field_meta import FieldMeta | ||
from tests.models import Person, PetFactory | ||
|
||
IS_PYDANTIC_V1 = _IS_PYDANTIC_V1 | ||
|
@@ -634,6 +636,49 @@ class A(BaseModel): | |
assert AFactory.build() | ||
|
||
|
||
@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires modern union types") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can these be split out so this is tested on all python versions? |
||
@pytest.mark.skipif(IS_PYDANTIC_V1, reason="pydantic 2 only test") | ||
def test_optional_custom_type() -> None: | ||
from pydantic_core import core_schema | ||
|
||
class CustomType: | ||
def __init__(self, _: Any) -> None: | ||
pass | ||
|
||
def __get_pydantic_core_schema__(self, _: Any) -> core_schema.StringSchema: | ||
# for pydantic to stop complaining | ||
return core_schema.str_schema() | ||
|
||
class OptionalFormOne(BaseModel): | ||
optional_custom_type: Optional[CustomType] | ||
|
||
@classmethod | ||
def should_set_none_value(cls, field_meta: FieldMeta) -> bool: | ||
return False | ||
|
||
class OptionalFormOneFactory(ModelFactory[OptionalFormOne]): | ||
@classmethod | ||
def should_set_none_value(cls, field_meta: FieldMeta) -> bool: | ||
return False | ||
|
||
class OptionalFormTwo(BaseModel): | ||
# this is represented differently than `Optional[None]` internally | ||
optional_custom_type_second_form: CustomType | None | ||
|
||
class OptionalFormTwoFactory(ModelFactory[OptionalFormTwo]): | ||
@classmethod | ||
iloveitaly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def should_set_none_value(cls, field_meta: FieldMeta) -> bool: | ||
return False | ||
|
||
# ensure the custom type field name and variant is in the error message | ||
|
||
with pytest.raises(ParameterException, match=r"optional_custom_type"): | ||
OptionalFormOneFactory.build() | ||
|
||
with pytest.raises(ParameterException, match=r"optional_custom_type_second_form"): | ||
OptionalFormTwoFactory.build() | ||
|
||
|
||
def test_collection_unions_with_models() -> None: | ||
class A(BaseModel): | ||
a: int | ||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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...There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I don't think the functionality is broken here, it was just challenging to understand what was going on so I added the comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this checks optional type based on https://github.com/litestar-org/polyfactory/pull/666/files#r2014961152. Can this be reverted?