I set max_length
to 1e3
and the build
method fails.
The bug seems to have been introduced when python>=3.12.
No response
import typing as ty
from pydantic import BaseModel, Field
from polyfactory.factories.pydantic_factory import ModelFactory
class Test(BaseModel):
notes: str = Field(
"",
max_length=1e3,
)
class TestFactory(ModelFactory[Test]):
__model__ = Test
TestFactory.build()
1. Run the above code.
2. See error.
No response
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
File ~/miniforge3/envs/polyfactory-test/lib/python3.13/site-packages/polyfactory/factories/base.py:614, in BaseFactory.get_constrained_field_value(cls, annotation, field_meta, field_build_parameters, build_context)
613 if is_safe_subclass(annotation, str) or is_safe_subclass(annotation, bytes):
--> 614 return handle_constrained_string_or_bytes(
615 random=cls.__random__,
616 t_type=str if is_safe_subclass(annotation, str) else bytes,
617 lower_case=constraints.get("lower_case") or False,
618 upper_case=constraints.get("upper_case") or False,
619 min_length=constraints.get("min_length"),
620 max_length=constraints.get("max_length"),
621 pattern=constraints.get("pattern"),
622 )
624 try:
File ~/miniforge3/envs/polyfactory-test/lib/python3.13/site-packages/polyfactory/value_generators/constrained_strings.py:120, in handle_constrained_string_or_bytes(random, t_type, lower_case, upper_case, min_length, max_length, pattern)
117 if t_type is str:
118 return cast(
119 "T",
--> 120 create_random_string(
121 min_length=min_length,
122 max_length=max_length,
123 lower_case=lower_case,
124 upper_case=upper_case,
125 random=random,
126 ),
127 )
129 return cast(
130 "T",
131 create_random_bytes(
(...)
137 ),
138 )
File ~/miniforge3/envs/polyfactory-test/lib/python3.13/site-packages/polyfactory/value_generators/primitives.py:113, in create_random_string(random, min_length, max_length, lower_case, upper_case)
103 """Generate a random string given the constraints.
104
105 :param random: An instance of random.
(...)
111 :returns: A random string.
112 """
--> 113 return create_random_bytes(
114 random=random,
115 min_length=min_length,
116 max_length=max_length,
117 lower_case=lower_case,
118 upper_case=upper_case,
119 ).decode("utf-8")
File ~/miniforge3/envs/polyfactory-test/lib/python3.13/site-packages/polyfactory/value_generators/primitives.py:81, in create_random_bytes(random, min_length, max_length, lower_case, upper_case)
79 max_length = min_length + 1 * 2
---> 81 length = random.randint(min_length, max_length)
82 result = b"" if length == 0 else hexlify(random.getrandbits(length * 8).to_bytes(length, "little"))
File ~/miniforge3/envs/polyfactory-test/lib/python3.13/random.py:340, in Random.randint(self, a, b)
337 """Return random integer in range [a, b], including both end points.
338 """
--> 340 return self.randrange(a, b+1)
File ~/miniforge3/envs/polyfactory-test/lib/python3.13/random.py:316, in Random.randrange(self, start, stop, step)
315 # Stop argument supplied.
--> 316 istop = _index(stop)
317 width = istop - istart
TypeError: 'float' object cannot be interpreted as an integer
The above exception was the direct cause of the following exception:
ParameterException Traceback (most recent call last)
Cell In[8], line 15
11 class TestFactory(ModelFactory[Test]):
12 __model__ = Test
---> 15 TestFactory.build()
File ~/miniforge3/envs/polyfactory-test/lib/python3.13/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 ~/miniforge3/envs/polyfactory-test/lib/python3.13/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 ~/miniforge3/envs/polyfactory-test/lib/python3.13/site-packages/polyfactory/factories/base.py:706, in BaseFactory.get_field_value(cls, field_meta, field_build_parameters, build_context)
703 return cls.__random__.choice(list(unwrapped_annotation))
705 if field_meta.constraints:
--> 706 return cls.get_constrained_field_value(
707 annotation=unwrapped_annotation,
708 field_meta=field_meta,
709 field_build_parameters=field_build_parameters,
710 build_context=build_context,
711 )
713 if is_union(field_meta.annotation) and field_meta.children:
714 seen_models = build_context["seen_models"]
File ~/miniforge3/envs/polyfactory-test/lib/python3.13/site-packages/polyfactory/factories/pydantic_factory.py:448, in ModelFactory.get_constrained_field_value(cls, annotation, field_meta, field_build_parameters, build_context)
443 value = cls.get_field_value(
444 field_meta, field_build_parameters=field_build_parameters, build_context=build_context
445 )
446 return to_json(value) # pyright: ignore[reportPossiblyUnboundVariable]
--> 448 return super().get_constrained_field_value(
449 annotation, field_meta, field_build_parameters=field_build_parameters, build_context=build_context
450 )
File ~/miniforge3/envs/polyfactory-test/lib/python3.13/site-packages/polyfactory/factories/base.py:669, in BaseFactory.get_constrained_field_value(cls, annotation, field_meta, field_build_parameters, build_context)
667 return handle_constrained_path(constraint=path_constraint, faker=cls.__faker__)
668 except TypeError as e:
--> 669 raise ParameterException from e
671 msg = f"received constraints for unsupported type {annotation}"
672 raise ParameterException(msg)
ParameterException:
polyfactory: 2.18.1
python: 3.13.1
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too