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

feat: simplify pytest plugin #652

Merged
merged 2 commits into from
Mar 3, 2025
Merged

feat: simplify pytest plugin #652

merged 2 commits into from
Mar 3, 2025

Conversation

adhtruong
Copy link
Collaborator

Description

  • This PR simplifies pytest plugin and improves ergonomics of usage
  • Current implementation returns the pytest fixture then uses a registry to expose usage.
  • The new implementation injects the fixture into the caller scope so the returned object is the factory itself. This allow direct usage of factory and simplified usage

Closes

@adhtruong adhtruong requested a review from guacs as a code owner March 1, 2025 10:02
@provinzkraut
Copy link
Member

Looks good but this is a breaking change, right?

@adhtruong
Copy link
Collaborator Author

adhtruong commented Mar 1, 2025

I'm on the fence. This is changing the return type of register_fixture but only documented or productive use of this would be with Fixture field. I've preserved usage of Fixture field itself and deprecated that instead so existing code should work based on tests using a registered factory with Fixture field.

In terms of fixture naming and scoping with respect to pytest, this is also preserved

Edit: also worth noting the return type of Callable[[], type[T]] is largely wrong. This returns a pytest fixture so

from dataclasses import dataclass

from polyfactory.factories import DataclassFactory
from polyfactory.pytest_plugin import register_fixture


@dataclass
class MyModel:
    values: int


@register_fixture
class MyModelFactory(DataclassFactory[MyModel]): ...


print(MyModelFactory())

will raise the following error

Failed: Fixture "my_model_factory" called directly. Fixtures are not meant to be called directly,
but are created automatically when test functions request them as parameters.

@adhtruong adhtruong enabled auto-merge (squash) March 3, 2025 07:57
@adhtruong adhtruong merged commit ba7cd3f into main Mar 3, 2025
27 checks passed
@adhtruong adhtruong deleted the feat-simplify-pytest-plugin branch March 3, 2025 07:59
Copy link

github-actions bot commented Mar 3, 2025

Documentation preview will be available shortly at https://litestar-org.github.io/polyfactory-docs-preview/652

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