Skip to content

Enhancement: allow customizing TypedDict keys that are invalid identifiers #752

@Glinte

Description

@Glinte

Summary

With the alternative syntax for TypedDict it is possible to construct one with keys that cannot be represented using the class syntax due to variable naming issues. There does not seem to be any way in polyfactory to customize these fields.

from typing import TypedDict, NotRequired
from polyfactory.factories.typed_dict_factory import TypedDictFactory

A = TypedDict("A", {"x": int, "y": NotRequired[str], "#not-valid-identifier": float})

class AFactory(TypedDictFactory[A]):
    # Nothing can be done here to change the behavior for generating "#not-valid-identifier"
    pass

print(AFactory.build())  # {'x': 544, 'y': 'egGRVOHCaCMUAwOchRWT', '#not-valid-identifier': 427363237424.216}

Basic Example

No response

Drawbacks and Impact

No response

Unresolved questions

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions