litestar-org / litestar-org/polyfactory

Enhancement: allow customizing TypedDict keys that are invalid identifiers

Open
#752 0 comments 2 reactions 0 assignees View on GitHub
enhancement
Dominant language
Python
Stars
1.5k
Forks
120
PR merge metrics
No merged PRs in 30d

Description

### Summary

With the [alternative syntax](https://typing.python.org/en/latest/spec/typeddict.html#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.

```python
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_

Contributor guide

Open the contributing guide

Research direction

Start in polyfactory/factories/typed_dict_factory.py at TypedDictFactory and trace how fields from the alternative TypedDict syntax are discovered and customized. Check the existing factory customization mechanisms, then verify that a custom value for "#not-valid-identifier" is used when building the TypedDict.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.