-
-
Notifications
You must be signed in to change notification settings - Fork 99
feat(fields): implement Param and CallableParam for handling unmapped parameters that can be referenced during build #650
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
leverage-analytics
wants to merge
15
commits into
litestar-org:main
Choose a base branch
from
leverage-analytics:feature-604
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
15 commits
Select commit
Hold shift + click to select a range
94dc3e1
Merge branch 'main' into feature-604
leverage-analytics 4557260
feat(fields): implement Param and CallableParam for handling unmapped…
leverage-analytics 0921a25
test(test_factory_fields): implement tests for two new unmapped param…
leverage-analytics cbbcfc3
docs(fields): updated docs to include definitions and examples with n…
leverage-analytics 1af8f75
feat(Param): consolidate Param API into a single class for simplified…
leverage-analytics 612201d
test(test_factory_fields.py) update tests for new Param type
leverage-analytics 2d6de83
docs(fields.rst) update documentation for new Param type
leverage-analytics d37dce1
chore: resolve merge conflicts between master and new
leverage-analytics e1f094e
fix(get_factory_params): fix issue caused by compatibility of cache w…
leverage-analytics 9035739
fix(test_factory_fields) update with typings accepted by previous ver…
leverage-analytics 8e0bc8f
fix(test_factory_fields) update typing for compatibility with previou…
leverage-analytics dadeec6
Merge branch 'main' into feature-604
leverage-analytics 21c56a2
Merge branch 'main' into feature-604
leverage-analytics 5b31795
Merge branch 'main' into feature-604
leverage-analytics 19fe618
Merge branch 'main' into feature-604
leverage-analytics 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from dataclasses import dataclass | ||
|
||
from polyfactory.decorators import post_generated | ||
from polyfactory.factories import DataclassFactory | ||
from polyfactory.fields import Param | ||
|
||
|
||
@dataclass | ||
class Person: | ||
name: str | ||
age_next_year: int | ||
|
||
|
||
class PersonFactoryWithParamValueSpecifiedInFactory(DataclassFactory[Person]): | ||
"""In this factory, the next_years_age_from_calculator must be passed at build time.""" | ||
|
||
next_years_age_from_calculator = Param[int](lambda age: age + 1, is_callable=True, age=20) | ||
|
||
@post_generated | ||
@classmethod | ||
def age_next_year(cls, next_years_age_from_calculator: int) -> int: | ||
return next_years_age_from_calculator | ||
|
||
|
||
def test_factory__in_factory() -> None: | ||
person = PersonFactoryWithParamValueSpecifiedInFactory.build() | ||
|
||
assert isinstance(person, Person) | ||
assert not hasattr(person, "next_years_age_from_calculator") | ||
assert person.age_next_year == 21 | ||
|
||
|
||
class PersonFactoryWithParamValueSetAtBuild(DataclassFactory[Person]): | ||
"""In this factory, the next_years_age_from_calculator must be passed at build time.""" | ||
|
||
next_years_age_from_calculator = Param[int](is_callable=True, age=20) | ||
|
||
@post_generated | ||
@classmethod | ||
def age_next_year(cls, next_years_age_from_calculator: int) -> int: | ||
return next_years_age_from_calculator | ||
|
||
|
||
def test_factory__build_time() -> None: | ||
person = PersonFactoryWithParamValueSpecifiedInFactory.build(next_years_age_from_calculator=lambda age: age + 1) | ||
|
||
assert isinstance(person, Person) | ||
assert not hasattr(person, "next_years_age_from_calculator") | ||
assert person.age_next_year == 21 |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from dataclasses import dataclass | ||
from typing import List | ||
|
||
from polyfactory.decorators import post_generated | ||
from polyfactory.factories import DataclassFactory | ||
from polyfactory.fields import Param | ||
|
||
|
||
@dataclass | ||
class Pet: | ||
name: str | ||
sound: str | ||
|
||
|
||
class PetFactoryWithParamValueSetAtBuild(DataclassFactory[Pet]): | ||
"""In this factory, the name_choices must be passed at build time.""" | ||
|
||
name_choices = Param[List[str]]() | ||
|
||
@post_generated | ||
@classmethod | ||
def name(cls, name_choices: List[str]) -> str: | ||
return cls.__random__.choice(name_choices) | ||
|
||
|
||
def test_factory__build_time() -> None: | ||
names = ["Ralph", "Roxy"] | ||
pet = PetFactoryWithParamValueSetAtBuild.build(name_choices=names) | ||
|
||
assert isinstance(pet, Pet) | ||
assert not hasattr(pet, "name_choices") | ||
assert pet.name in names | ||
|
||
|
||
class PetFactoryWithParamSpecififiedInFactory(DataclassFactory[Pet]): | ||
"""In this factory, the name_choices are specified in the | ||
factory and do not need to be passed at build time.""" | ||
|
||
name_choices = Param[List[str]](["Ralph", "Roxy"]) | ||
|
||
@post_generated | ||
@classmethod | ||
def name(cls, name_choices: List[str]) -> str: | ||
return cls.__random__.choice(name_choices) | ||
|
||
|
||
def test_factory__in_factory() -> None: | ||
pet = PetFactoryWithParamSpecififiedInFactory.build() | ||
|
||
assert isinstance(pet, Pet) | ||
assert not hasattr(pet, "name_choices") | ||
assert pet.name in ["Ralph", "Roxy"] |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.