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: autofill model argument when calling create_factory with receiving factory __model__ #429

Merged
merged 4 commits into from
Oct 31, 2023
Merged

Conversation

g0di
Copy link
Contributor

@g0di g0di commented Oct 23, 2023

Pull Request Checklist

  • New code has 100% test coverage
  • (If applicable) The prose documentation has been updated to reflect the changes introduced by this PR
  • (If applicable) The reference documentation has been updated to reflect the changes introduced by this PR
  • Pre-Commit Checks were ran and passed
  • Tests were ran and passed

Description

This PR aims at allowing to create "sub" factories from concrete factories (i.e: a factory with a __model__ set) without having to pass the model argument to the create_factory method.

In this case, when the model argument is omitted, the __model__ of the receiving factory will be used by default, increasing code readability and making the dynamic mode working the same way as the imperative style.

The PR includes the change, the tests and the documentation update.

from dataclasses import dataclass

from polyfactory.factories import DataclassFactory

@dataclass
class Foo:
    bar: str

# Imperative mode
class FooFactory(DataclassFactory):
    __model__ = Foo

class BarFactory(FooFactory):
    bar = "bar"

# Dynamic mode (after this PR)
foo_factory = DataclassFactory.create_factory(Foo)
bar_factory = foo_factory.create_factory(bar="bar")

Close Issue(s)

Closes #357

Benoit.Godard added 2 commits October 23, 2023 16:19
Sets the model argument optional when creating a factory using
imperative style and defaults to current factory model
@g0di g0di requested review from a team as code owners October 23, 2023 14:52
@g0di g0di changed the title Autofill factory model with current factory feat: autofill model argument when calling create_factory with receiving factory __model__ Oct 23, 2023
Copy link
Member

@guacs guacs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

polyfactory/factories/base.py Outdated Show resolved Hide resolved
This is based on the library author suggestion

Co-authored-by: guacs <126393040+guacs@users.noreply.github.com>
@g0di
Copy link
Contributor Author

g0di commented Oct 25, 2023

I made the change as suggested but pyright is not happy. It was the reason why I've made it the previous way. Do you want me to simply turn off the pyright warning for that line or have any suggestion?

@guacs
Copy link
Member

guacs commented Oct 26, 2023

Aah gotcha. I think a pyright ignore is good for that line since I think it's better to have the AttributeError as the __cause__ than do workarounds for satisfying the type checkers.

@g0di
Copy link
Contributor Author

g0di commented Oct 26, 2023

Aah gotcha. I think a pyright ignore is good for that line since I think it's better to have the AttributeError as the __cause__ than do workarounds for satisfying the type checkers.

Fine, I've made the change 🤓

@github-actions
Copy link

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

Copy link
Member

@guacs guacs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@g0di sorry for the delay! Thank you very much for the contribution :)

@guacs guacs merged commit 155f4a4 into litestar-org:main Oct 31, 2023
19 checks passed
@guacs
Copy link
Member

guacs commented Oct 31, 2023

@all-contributors add @g0di for code, docs, test

@allcontributors
Copy link
Contributor

@guacs

I've put up a pull request to add @g0di! 🎉

@g0di g0di deleted the feat/optional-model branch October 31, 2023 12:21
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.

None yet

2 participants