Skip to content

Docs: fixtures resulting from register_fixture is of type type[Factory] instead of Factory #751

@Glinte

Description

@Glinte

Summary

The usages in https://polyfactory.litestar.dev/latest/usage/fixtures.html suggests the fixture returns an instance of the factory class, but instead it returns the class itself. This can be shown using the example in the guide and printing the types

from dataclasses import dataclass
from datetime import date, datetime
from typing import List, Optional, Union
from uuid import UUID

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


@dataclass
class Person:
    id: UUID
    name: str
    hobbies: Optional[List[str]]
    nicks: List[str]
    age: Union[float, int]
    birthday: Union[datetime, date]


@register_fixture(name="aliased_person_factory")
class PersonFactory(DataclassFactory[Person]): ...


def test_person_factory(person_factory: PersonFactory) -> None:
    print(type(person_factory))  # <class 'abc.ABCMeta'>
    print(type(person_factory()))  # <class 'poly.PersonFactory'>
    person_instance = person_factory.build()
    assert isinstance(person_instance, Person)

It can also be shown in FactoryFixture.__call__:

def _factory_fixture() -> type[T]:
"""The wrapped factory"""
return cast("Type[T]", factory)
caller_globals = inspect.stack()[depth][0].f_globals
caller_globals[fixture_name] = fixture_register(_factory_fixture)
return factory # type: ignore[return-value]

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationgood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions