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

Bug: Error with OdmanticModelFactory: AttributeError on _datetime.validate for Models with datetime Attributes #618

Open
1 of 4 tasks
FlorianEisenbarth opened this issue Dec 6, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@FlorianEisenbarth
Copy link

FlorianEisenbarth commented Dec 6, 2024

Description

Hello,

I'm encountering an issue when trying to create a Factory using OdmanticModelFactory. If the Odmantic model contains datetime attributes, the following error is raised:

AttributeError: type object '_datetime' has no attribute 'validate'

The error originates from line 48 in the file odmantic_odm_factory.py.

Could you please provide guidance on resolving this issue? Thank you!

URL to code causing the issue

No response

MCVE

class Test(Model):
    date: datetime = datetime.now()
    name: str = "Jean"

class TestFactory( OdmanticModelFactory[Test]): ...

TestFactory.build()

Steps to reproduce

1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

Screenshots

"In the format of: ![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)"

Logs

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[438], line 7
      3     name: str = "Jean"
      5 class TestFactory( OdmanticModelFactory[Test]): ...
----> 7 TestFactory.build()

File ~/opt/miniconda3/envs/AdaptPA/lib/python3.10/site-packages/polyfactory/factories/pydantic_factory.py:473, in ModelFactory.build(cls, factory_use_construct, **kwargs)
    468 if "_build_context" not in kwargs:
    469     kwargs["_build_context"] = PydanticBuildContext(
    470         seen_models=set(), factory_use_construct=factory_use_construct
    471     )
--> 473 processed_kwargs = cls.process_kwargs(**kwargs)
    475 return cls._create_model(kwargs["_build_context"], **processed_kwargs)

File ~/opt/miniconda3/envs/AdaptPA/lib/python3.10/site-packages/polyfactory/factories/base.py:1003, in BaseFactory.process_kwargs(cls, **kwargs)
    996     result[field_meta.name] = cls._handle_factory_field(
    997         field_value=field_value,
    998         field_build_parameters=field_build_parameters,
    999         build_context=_build_context,
   1000     )
   1001     continue
-> 1003 field_result = cls.get_field_value(
   1004     field_meta,
   1005     field_build_parameters=field_build_parameters,
   1006     build_context=_build_context,
   1007 )
   1008 if field_result is Null:
   1009     continue

File ~/opt/miniconda3/envs/AdaptPA/lib/python3.10/site-packages/polyfactory/factories/base.py:785, in BaseFactory.get_field_value(cls, field_meta, field_build_parameters, build_context)
    776     return handle_collection_type(
    777         field_meta,
    778         origin,
   (...)
    781         build_context=build_context,
    782     )
    784 if provider := cls.get_provider_map().get(unwrapped_annotation):
--> 785     return provider()
    787 if isinstance(unwrapped_annotation, TypeVar):
    788     return create_random_string(cls.__random__, min_length=1, max_length=10)

File ~/opt/miniconda3/envs/AdaptPA/lib/python3.10/site-packages/polyfactory/factories/odmantic_odm_factory.py:48, in OdmanticModelFactory.get_provider_map.<locals>.<lambda>()
     40 @classmethod
     41 def get_provider_map(cls) -> dict[Any, Callable[[], Any]]:
     42     provider_map = super().get_provider_map()
     43     provider_map.update(
     44         {
     45             odbson.Int64: lambda: odbson.Int64.validate(cls.__faker__.pyint()),
     46             odbson.Decimal128: lambda: _to_decimal128(cls.__faker__.pydecimal()),
     47             odbson.Binary: lambda: odbson.Binary.validate(create_random_bytes(cls.__random__)),
---> 48             odbson._datetime: lambda: odbson._datetime.validate(cls.__faker__.date_time_between()),
     49             # bson.Regex and bson._Pattern not supported as there is no way to generate
     50             # a random regular expression with Faker
     51             # bson.Regex:
     52             # bson._Pattern:
     53         },
     54     )
     55     return provider_map

AttributeError: type object '_datetime' has no attribute 'validate'

Release Version

2.18.1

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Note

While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.

Check out all issues funded or available for funding on our Polar.sh dashboard

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.
Fund with Polar
@FlorianEisenbarth FlorianEisenbarth added the bug Something isn't working label Dec 6, 2024
@FlorianEisenbarth FlorianEisenbarth changed the title Bug: <title> Bug: Error with OdmanticModelFactory: AttributeError on _datetime.validate for Models with datetime Attributes Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant