Dicts in pyfake
Simple usage
pyfake generates values for mapping fields declared with typing.Dict[...] or the builtin dict[...] generic. Keys and values are generated independently from their annotated types.
from typing import Dict
from pyfake import fake
from pydantic import BaseModel
class Maps(BaseModel):
by_name: Dict[str, int]
by_id: dict[int, float]
print(fake(Maps))
No setup required — pyfake inspects the schema and generates keys and values automatically.
Typing forms
Both typing.Dict[K, V] and dict[K, V] are resolved. Prefer explicit key/value types for predictable results.
Receiving Model Instances
By default pyfake returns dictionaries (serialized). To obtain Pydantic model instances (preserving types where Pydantic coerce applies), set as_dict=False.
from pyfake import fake
from pydantic import BaseModel
class Item(BaseModel):
label: str
class Container(BaseModel):
mapping: dict[int, Item]
print(fake(Container, as_dict=False))
Returning Multiple Values
Use num to generate multiple examples as with other types.
print(fake(Maps, num=3))
{'by_name': {'gllDxwbMVV': 27, 'grIUPcdNDM': 99}, 'by_id': {41: 38.31790652661318, 67: 16.56897318208862, 8: 79.2069059099716}},
{'by_name': {'CZDQHnlJrt': 86, 'mSoZkjgxWj': 53, 'IUIJAQwzXx': 52, 'sNfLpYthTK': 26}, 'by_id': {23: 74.71911608258735}},
{'by_name': {'AVelENxysQ': 22, 'dOeRvKfxTs': 31}, 'by_id': {47: 28.34939934408407, 43: 4.9681132448364025}}
]
:lucide-key:
Keys, Values, and Constraints
pyfake resolves and generates keys and values independently. Container size uses min_length/max_length on the mapping itself (controls how many key/value pairs are generated). Key and value types may themselves be annotated with Field/Annotated metadata which the resolver propagates into the inner GeneratorArgs.
How mapping size is chosen
The number of key/value pairs follows the same rules as list lengths:
- Default: when no
min_length/max_lengthare provided the registry picks a length between1and5(inclusive). - With
min_length/max_length: a random length is chosen between those bounds (inclusive).max_length=0gives an empty dict.
Keys are generated until there are enough distinct ones, so a dict always reaches its min_length. If the key type has too few possible values (e.g. Dict[bool, int] with min_length=3), pyfake raises InvalidConstraints.
Key/value constraints (Annotated / Field)
You can apply Annotated[...] / Field(...) constraints to either the key type or the value type. Those constraints are used when generating each key or value.
from typing import Annotated, Dict
from pydantic import BaseModel, Field
from pyfake import fake
class Constrained(BaseModel):
# keys between 1 and 10, values are short strings
mapping: Dict[
Annotated[int, Field(ge=1, le=10)],
Annotated[str, Field(min_length=3, max_length=6)],
]
print(fake(Constrained, seed=42))
Examples: different key/value shapes
Dict[int, str]
class M1(BaseModel):
m: Dict[int, str]
print(fake(M1))
Dict[int, list[int]]— values are collections generated according to list rules (inner min/max lengths apply):
from typing import List
class M2(BaseModel):
m: Dict[int, List[int]]
print(fake(M2, seed=1))
Dict[int, Dict[str, str]]— nested mappings; inner keys/values are generated using their schemas. You can annotate inner key/value types as well:
class M3(BaseModel):
nested: Dict[int, Dict[str, str]]
print(fake(M3, seed=2))
Annotated keys & values with Field
You can annotate the key or the inner value with Field to influence generation. The resolver merges these constraints into the inner GeneratorArgs (see tests/core/test_pyfake_complex.py for examples of inherited/propagated constraints).
from typing import Annotated
class AnnotatedKV(BaseModel):
data: Dict[
Annotated[int, Field(ge=10, le=20)],
Annotated[List[int], Field(min_length=2, max_length=3)],
]
print(fake(AnnotatedKV, seed=3))
Nullable mappings
If a mapping is declared Optional[Dict[K, V]] or Dict[K, V] | None, the resolver marks the field as a nullable union. The registry will return None when the nullable branch triggers (currently about 20% probability); otherwise it generates a mapping normally.
from typing import Optional
class MaybeMap(BaseModel):
maybe: Optional[Dict[int, str]]
print(fake(MaybeMap, num=6, seed=1))
{'maybe': {1: 'Abc'}},
{'maybe': None},
{'maybe': {3: 'Xy'}},
{'maybe': {7: 'Z'}},
{'maybe': None},
{'maybe': {2: 'Qw'}}
]
Dict[Any, Any] and dynamic types
Dict[Any, Any], a bare dict, and Any keys or values are supported: each Any gets a random JSON-safe scalar (an int, float, str or bool). For predictable results prefer explicit key and value types.
Mapping[K, V], MutableMapping[K, V], OrderedDict[K, V], DefaultDict[K, V] and Counter[K] are generated as dicts too. See Other types.
Keys must be hashable
Because Python dict keys must be hashable, ensure your key type produces hashable values (e.g. int, str, tuple). If a generator produces an unhashable key (e.g. list) a TypeError will be raised when the mapping is constructed.
Registry direct generation
You can call the GeneratorRegistry directly with an explicit schema (tests exercise this path):
from pyfake.core.context import Context
from pyfake.core.registry import GeneratorRegistry
from pyfake.schemas import GeneratorArgs
context = Context(seed=0)
registry = GeneratorRegistry(context=context)
schema = {
"type": dict,
"keys": {"type": str, "generator_args": GeneratorArgs()},
"values": {"type": int, "generator_args": GeneratorArgs()},
"generator_args": GeneratorArgs(),
}
result = registry._generate(schema)
assert isinstance(result, dict)
This mirrors the behaviour tested in tests/core/test_pyfake_complex.py.
Implementation notes
- Container size selection follows the list length rules — default range
1..5. - Keys and values are generated by recursively resolving their schemas; any
Annotated/Fieldmetadata on key/value types is propagated and honoured where the underlying generator supports those constraints (e.g.ge/lefor integers,min_length/max_lengthfor strings/lists). - Duplicate keys: keys are generated until there are enough distinct ones, then a value is generated for each key.
- Unsupported inner types: if the registry does not have a generator for a key or value type (e.g.
pathlib.Path), it raisesGeneratorNotFoundnaming the field (seeTestRegistryFallbackin tests).
Unsupported / Partial support
Dict[Any, Any]and other highly dynamic annotations produce valid but not very meaningful values — use concrete types.