litestar-org / polyfactory

Simple and powerful factories for mock data generation
https://polyfactory.litestar.dev/
MIT License
1.06k stars 83 forks source link

Bug: Exception for missing dependecy `beanie` when importing a factory #168

Closed RafaelWO closed 1 year ago

RafaelWO commented 1 year ago

Description

I installed polyfactory and pydantic to use the pydantic ModelFactory in my code. However, I a MissingDependencyException with regards to beanie but I should not need it. Or should I?

polyfactory.exceptions.MissingDependencyException: beanie is not installed

This also happens when I import a factory that does not need any dependencies, e.g. DataclassFactory.

URL to code causing the issue

No response

MCVE

from polyfactory.factories.pydantic_factory import ModelFactory

Steps to reproduce

1. Install `pydantic`
2. Run the MCVE
3. See the error

Screenshots

No response

Logs

Traceback (most recent call last):
  File "/home/my/venv/lib/python3.8/site-packages/polyfactory/factories/beanie_odm_factory.py", line 17, in <module>
    from beanie import Document
ModuleNotFoundError: No module named 'beanie'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/my/python_script.py", line 6, in <module>
    from polyfactory.factories.pydantic_factory import ModelFactory
  File "/home/my/venv/lib/python3.8/site-packages/polyfactory/__init__.py", line 2, in <module>
    from .factories import BaseFactory
  File "/home/my/venv/lib/python3.8/site-packages/polyfactory/factories/__init__.py", line 1, in <module>
    from polyfactory.factories.base import BaseFactory
  File "/home/my/venv/lib/python3.8/site-packages/polyfactory/factories/base.py", line 695, in <module>
    _register_builtin_factories()
  File "/home/my/venv/lib/python3.8/site-packages/polyfactory/factories/base.py", line 690, in _register_builtin_factories
    import_module(module)
  File "/home/my/miniconda3/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/my/venv/lib/python3.8/site-packages/polyfactory/factories/beanie_odm_factory.py", line 19, in <module>
    raise MissingDependencyException("beanie is not installed") from e
polyfactory.exceptions.MissingDependencyException: beanie is not installed

Starlite Version

I'm not using "Starlite" but polyfactory :wink:

❯ pip list | egrep "pydantic|polyfactory"
polyfactory          1.0.0a1
pydantic             1.10.7

Platform

provinzkraut commented 1 year ago

@RafaelWO Thanks for reporting this! It has already been fixed in #167 and I am currently preparing the next alpha release, which will contain the fix.

Please report back should this issue persist after upgrading to the next version!