Skip to content

Bug: date value generator incompatible with masgspec datetime 'tz' constraint #768

@Spasley

Description

@Spasley

Description

parameter tz of msgspec.Meta is bool for checking if given value contains tzinfo but generator expects None or tzinfo object to use it as actual tzinfo data

URL to code causing the issue

No response

MCVE

from datetime import datetime
from typing import Annotated

from msgspec import Struct, Meta
from polyfactory.factories.msgspec_factory import MsgspecFactory


class SomeStruct(Struct):
    some_datetime: Annotated[datetime, Meta(tz=True)]


class SomeStructFactory(MsgspecFactory[SomeStruct]):...


SomeStructFactory.build()

Steps to reproduce

1. Run suggested snippet

Screenshots

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

Logs

Traceback (most recent call last):
  File "C:\repos\daisy\.venv\Lib\site-packages\polyfactory\factories\base.py", line 727, in get_constrained_field_value
    return handle_constrained_date(
        faker=cls.__faker__,
    ...<4 lines>...
        tz=cast("Any", constraints.get("tz")),
    )
  File "C:\repos\daisy\.venv\Lib\site-packages\polyfactory\value_generators\constrained_dates.py", line 29, in handle_constrained_date
    start_date = datetime.now(tz=tz).date() - timedelta(days=100)
                 ~~~~~~~~~~~~^^^^^^^
TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'bool'

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

Traceback (most recent call last):
  File "C:\repos\daisy\msgspec_tz.py", line 15, in <module>
    SomeStructFactory.build()
    ~~~~~~~~~~~~~~~~~~~~~~~^^
  File "C:\repos\daisy\.venv\Lib\site-packages\polyfactory\factories\base.py", line 1174, in build
    return cast("T", cls.__model__(**cls.process_kwargs(**kwargs)))
                                     ~~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "C:\repos\daisy\.venv\Lib\site-packages\polyfactory\factories\base.py", line 1101, in process_kwargs
    field_result = cls.get_field_value(
        field_meta,
        field_build_parameters=field_build_parameters,
        build_context=_build_context,
    )
  File "C:\repos\daisy\.venv\Lib\site-packages\polyfactory\factories\base.py", line 783, in get_field_value
    return cls.get_constrained_field_value(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        annotation=unwrapped_annotation,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<2 lines>...
        build_context=build_context,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\repos\daisy\.venv\Lib\site-packages\polyfactory\factories\base.py", line 745, in get_constrained_field_value
    raise ParameterException from e
polyfactory.exceptions.ParameterException

Release Version

msgspec==0.19.0
polyfactory==2.22.3
python==3.13.4

Platform

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions