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(factories): added an ability to use the Coroutine as a factory field #641

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

nisemenov
Copy link
Contributor

The main thing is the ability to use a coroutine type field for more complex logic (for example, using existing data from the DB).

Description

  • added some async methods (for both the main purpose and semantic)
  • split some common logic into two separate methods _get_initial_variables and _check_special_field
  • created models.py and conftest.py in SQLAlchemy test level
  • replaced create_tables function with pytest fixture fx_drop_create_meta
  • "pymongo<4.9" has been added in pyproject.toml to prevent
    PytestDeprecationWarning: Module 'beanie' was found, but when imported by pytest it raised: ImportError("cannot 
    import name '_QUERY_OPTIONS' from 'pymongo.cursor' (.venv/lib/python3.13/site-packages/pymongo/cursor.py)")
    
  • appropriate tests and docs have been added

@nisemenov nisemenov requested a review from guacs as a code owner February 7, 2025 13:41
@adhtruong
Copy link
Collaborator

Docs and updated examples look good

I think this is a good feature to have but would have concerns about

  1. expanded interface and keeping these parts in sync for maintainability
  2. potential edge case for this being a breaking change e.g. if Coroutine is already used it will now be resolved. This is probably a minor case

For 1, would it be possible to resolve coroutine from sync code itself by running in a thread or suggesting this in docs? This avoids pain of maintaining both sync and async versions at the expense of correctness and general proper async behaviour.

Something like

import asyncio
import concurrent.futures

def run_coroutine_sync(coro):
    loop = asyncio.new_event_loop()
    with concurrent.futures.ThreadPoolExecutor() as executor:
        future = executor.submit(loop.run_until_complete, coro)
        return future.result()

with above being used when a coroutine is found as a field.

@nisemenov
Copy link
Contributor Author

Summary

  • updated docs with describing an approach of using data async pre-fetched in factory fields;
  • split some common logic into two separate methods _get_initial_variables and _check_special_field;
  • created models.py and conftest.py in SQLAlchemy test level;
  • replaced create_tables function with pytest fixture fx_drop_create_meta;
  • "pymongo<4.9" has been added in pyproject.toml to prevent
    PytestDeprecationWarning: Module 'beanie' was found, but when imported by pytest it raised: ImportError("cannot 
    import name '_QUERY_OPTIONS' from 'pymongo.cursor' (.venv/lib/python3.13/site-packages/pymongo/cursor.py)")
    

Copy link

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

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.

3 participants