Skip to content

Commit

Permalink
feat: Optional __model__ type (#452)
Browse files Browse the repository at this point in the history
Co-authored-by: Jacob Coffee <[email protected]>
Co-authored-by: guacs <[email protected]>
  • Loading branch information
3 people authored Dec 15, 2023
1 parent a371d7f commit 623d8c9
Show file tree
Hide file tree
Showing 38 changed files with 224 additions and 63 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Person:


class PersonFactory(DataclassFactory[Person]):
__model__ = Person
...


def test_is_person() -> None:
Expand Down
2 changes: 1 addition & 1 deletion docs/PYPI_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Person:


class PersonFactory(DataclassFactory[Person]):
__model__ = Person
...


def test_is_person() -> None:
Expand Down
1 change: 0 additions & 1 deletion docs/examples/configuration/test_example_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class Person:


class PersonFactory(DataclassFactory[Person]):
__model__ = Person
__random_seed__ = 1

@classmethod
Expand Down
1 change: 0 additions & 1 deletion docs/examples/configuration/test_example_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class Person:


class PersonFactory(DataclassFactory[Person]):
__model__ = Person
__random__ = Random(10)

@classmethod
Expand Down
1 change: 0 additions & 1 deletion docs/examples/configuration/test_example_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class Person:


class PersonFactory(DataclassFactory[Person]):
__model__ = Person
__faker__ = Faker(locale="es_ES")

__random_seed__ = 10
Expand Down
1 change: 0 additions & 1 deletion docs/examples/configuration/test_example_4.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ async def save_many(self, data: List[Person]) -> List[Person]:


class PersonFactory(DataclassFactory[Person]):
__model__ = Person
__sync_persistence__ = SyncPersistenceHandler
__async_persistence__ = AsyncPersistenceHandler

Expand Down
3 changes: 1 addition & 2 deletions docs/examples/configuration/test_example_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@ class Person:


class PetFactory(DataclassFactory[Pet]):
__model__ = Pet
__set_as_default_factory_for_type__ = True

name = Use(DataclassFactory.__random__.choice, ["Roxy", "Spammy", "Moshe"])


class PersonFactory(DataclassFactory[Person]):
__model__ = Person
...


def test_default_pet_factory() -> None:
Expand Down
2 changes: 0 additions & 2 deletions docs/examples/configuration/test_example_6.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ class Owner:


class OwnerFactory(DataclassFactory[Owner]):
__model__ = Owner

__randomize_collection_length__ = True
__min_collection_length__ = 2
__max_collection_length__ = 5
Expand Down
1 change: 0 additions & 1 deletion docs/examples/configuration/test_example_7.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class Person:


class PersonFactory(DataclassFactory[Person]):
__model__ = Person
__allow_none_optionals__ = False


Expand Down
1 change: 0 additions & 1 deletion docs/examples/configuration/test_example_8.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ def test_check_factory_fields() -> None:
):

class PersonFactory(DataclassFactory[Person]):
__model__ = Person
__check_model__ = True
unknown_field = PostGenerated(lambda: "foo")
2 changes: 1 addition & 1 deletion docs/examples/declaring_factories/test_example_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Person(TypedDict):


class PersonFactory(TypedDictFactory[Person]):
__model__ = Person
...


def test_is_person() -> None:
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/declaring_factories/test_example_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Person(BaseModel):


class PersonFactory(ModelFactory[Person]):
__model__ = Person
...


def test_is_person() -> None:
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/declaring_factories/test_example_4.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Person:


class PersonFactory(DataclassFactory[Person]):
__model__ = Person
...


def test_is_person() -> None:
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/declaring_factories/test_example_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Person:


class PersonFactory(DataclassFactory[Person]):
__model__ = Person
...


def test_dynamic_factory_generation() -> None:
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/declaring_factories/test_example_7.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Person:


class PersonFactory(AttrsFactory[Person]):
__model__ = Person
...


def test_person_factory() -> None:
Expand Down
2 changes: 0 additions & 2 deletions docs/examples/decorators/test_example_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ class DatetimeRange:


class DatetimeRangeFactory(DataclassFactory[DatetimeRange]):
__model__ = DatetimeRange

@post_generated
@classmethod
def to_dt(cls, from_dt: datetime) -> datetime:
Expand Down
2 changes: 0 additions & 2 deletions docs/examples/fields/test_example_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ class Person:


class PersonFactory(DataclassFactory[Person]):
__model__ = Person

pets = [pet_instance]


Expand Down
4 changes: 0 additions & 4 deletions docs/examples/fields/test_example_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,11 @@ class Person:


class PetFactory(DataclassFactory[Pet]):
__model__ = Pet

name = Use(DataclassFactory.__random__.choice, ["Ralph", "Roxy"])
species = Use(DataclassFactory.__random__.choice, list(Species))


class PersonFactory(DataclassFactory[Person]):
__model__ = Person

pets = Use(PetFactory.batch, size=2)


Expand Down
4 changes: 0 additions & 4 deletions docs/examples/fields/test_example_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,11 @@ class Person:


class PetFactory(DataclassFactory[Pet]):
__model__ = Pet

name = lambda: DataclassFactory.__random__.choice(["Ralph", "Roxy"])
species = lambda: DataclassFactory.__random__.choice(list(Species))


class PersonFactory(DataclassFactory[Person]):
__model__ = Person

pets = Use(PetFactory.batch, size=2)


Expand Down
4 changes: 0 additions & 4 deletions docs/examples/fields/test_example_4.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ class Person:


class PetFactory(DataclassFactory[Pet]):
__model__ = Pet

@classmethod
def name(cls) -> str:
return cls.__random__.choice(["Ralph", "Roxy"])
Expand All @@ -44,8 +42,6 @@ def species(cls) -> str:


class PersonFactory(DataclassFactory[Person]):
__model__ = Person

pets = Use(PetFactory.batch, size=2)


Expand Down
2 changes: 0 additions & 2 deletions docs/examples/fields/test_example_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ class Person(TypedDict):


class PersonFactory(TypedDictFactory[Person]):
__model__ = Person

id = Ignore()


Expand Down
2 changes: 0 additions & 2 deletions docs/examples/fields/test_example_6.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ class Person(TypedDict):


class PersonFactory(TypedDictFactory[Person]):
__model__ = Person

id = Require()


Expand Down
2 changes: 0 additions & 2 deletions docs/examples/fields/test_example_7.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class DatetimeRange:


class DatetimeRangeFactory(DataclassFactory[DatetimeRange]):
__model__ = DatetimeRange

to_dt = PostGenerated(add_timedelta)


Expand Down
4 changes: 0 additions & 4 deletions docs/examples/fields/test_example_8.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,10 @@ class Person:


class PetFactory(DataclassFactory[Pet]):
__model__ = Pet

name = lambda: DataclassFactory.__random__.choice(["Ralph", "Roxy"])


class PersonFactory(DataclassFactory[Person]):
__model__ = Person

pet = PetFactory


Expand Down
2 changes: 1 addition & 1 deletion docs/examples/fixtures/test_example_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Person:

@register_fixture
class PersonFactory(DataclassFactory[Person]):
__model__ = Person
...


def test_person_factory(person_factory: PersonFactory) -> None:
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/fixtures/test_example_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Person:


class PersonFactory(DataclassFactory[Person]):
__model__ = Person
...


person_factory_fixture = register_fixture(PersonFactory)
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/fixtures/test_example_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Person:


class PersonFactory(DataclassFactory[Person]):
__model__ = Person
...


person_factory_fixture = register_fixture(PersonFactory, name="aliased_person_factory")
Expand Down
4 changes: 1 addition & 3 deletions docs/examples/fixtures/test_example_4.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ class ClassRoom:

@register_fixture
class PersonFactory(DataclassFactory[Person]):
__model__ = Person
...


class ClassRoomFactory(DataclassFactory[ClassRoom]):
__model__ = ClassRoom

teacher = Fixture(PersonFactory, name="Ludmilla Newman")
pupils = Fixture(PersonFactory, size=20)

Expand Down
2 changes: 0 additions & 2 deletions docs/examples/handling_custom_types/test_example_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class Person:
# by default the factory class cannot handle unknown types,
# so we need to override the provider map to add it:
class PersonFactory(DataclassFactory[Person]):
__model__ = Person

@classmethod
def get_provider_map(cls) -> Dict[Type, Any]:
providers_map = super().get_provider_map()
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/handling_custom_types/test_example_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Person:

# we use our CustomDataclassFactory as a base for the PersonFactory
class PersonFactory(CustomDataclassFactory[Person]):
__model__ = Person
...


def test_custom_dataclass_base_factory() -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Author(Base):


class AuthorFactory(SQLAlchemyFactory[Author]):
__model__ = Author
...


def test_sqla_factory() -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ class Book(Base):


class AuthorFactory(SQLAlchemyFactory[Author]):
__model__ = Author
...


class AuthorFactoryWithRelationship(SQLAlchemyFactory[Author]):
__model__ = Author
__set_relationships__ = True


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class Book(Base):


class AuthorFactory(SQLAlchemyFactory[Author]):
__model__ = Author
__set_relationships__ = True


Expand Down
2 changes: 1 addition & 1 deletion docs/examples/model_coverage/test_example_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Profile:


class ProfileFactory(DataclassFactory[Profile]):
__model__ = Profile
...


def test_profile_coverage() -> None:
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/model_coverage/test_example_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SocialGroup:


class SocialGroupFactory(DataclassFactory[SocialGroup]):
__model__ = SocialGroup
...


def test_social_group_coverage() -> None:
Expand Down
18 changes: 18 additions & 0 deletions docs/usage/declaring_factories.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ base factories.
:caption: Declaring a factory for a dataclass
:language: python

You can also specify the model type by only specifying the factory generic type parameter.

.. code-block:: python
@dataclass
class Person:
name: str
age: float
height: float
weight: float
class PersonFactory(DataclassFactory[Person]):
...
.. note::
The syntax with the ``__model__`` class attribute omitting
is only available since version 2.13.0.

The same applies to the other factories exported by this library, for example:

.. literalinclude:: /examples/declaring_factories/test_example_2.py
Expand Down
Loading

0 comments on commit 623d8c9

Please sign in to comment.