Skip to content

Bug: empty string generated for regex requiring at least one char #695

@WilliamDEdwards

Description

@WilliamDEdwards

Description

Regex seems to be ignored inside dicts, as Polyfactory is generating `` as a value:

{'environment_variables': {'XZdrHqToIHlKKDxepfKJ': ''}}

Calling build() 10 times results in None 6 times, and `` 4 times.

--

This does NOT happen when the value is not nullable, i.e.:

class Model(BaseModel):
  environment_variables: Dict[str, constr(regex=r"^[A-Za-z_]+$")]

--

This ALSO happens when key/value are swapped:

class Model(BaseModel):
  environment_variables: Optional[Dict[constr(regex=r"^[A-Za-z_]+$"), str]]

Result:

{'environment_variables': {'': 'BildVEhMalDZKoEqvGvl'}}

--

This does NOT happen outside a dict:

class Model(BaseModel):
  environment_variables: constr(regex=r"^[A-Za-z_]+$")

Result:

{'environment_variables': 'XCjdsB'}

URL to code causing the issue

No response

MCVE

from polyfactory.factories.pydantic_factory import ModelFactory
from pydantic import BaseModel
from typing import Optional,Dict
from pydantic import constr,Field

class Model(BaseModel):
  environment_variables: Optional[Dict[str, constr(regex=r"^[A-Za-z_]+$")]]

class Factory(ModelFactory[Model]):
  ...

print(Factory.build())

Steps to reproduce

See MRE

Screenshots

No response

Logs

Release Version

2.21.0

Platform

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions