litestar-org / litestar-org/polyfactory

Enhancement: Sequence field

Open
#593 3 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
1.5k
Forks
120
PR merge metrics
No merged PRs in 30d

Description

### Summary

Hello, I want to take my comment from [older closed issue](https://github.com/litestar-org/polyfactory/issues/134#issuecomment-2425905672).
I miss the ability to generate guaranteed unique values. I used to use the FactoryBoy library for factories and there is a `Sequence` field for this purpose, which allows you to specify a function that will receive the next integer value at each call.
I would like to have a similar field in this library and would like to discuss a possible implementations.

I would like to hear feedback on the proposed solutions and can take on the implementation.

### Basic Example

What are the possibilities I see:

```python
@dataclass
class Person:
email: str
```

1. Similar to `__random__`:
```python
class PersonFactory(DataclassFactory[Person]):
@classmethod
def email(cls) -> str:
return cls.__sequence__(lambda n: f'user{n}@domain.tld')
```

2. Similar to `__random__` and `Use`:
```python
class PersonFactory(DataclassFactory[Person]):
email = Use(DataclassFactory.__sequence__, lambda n: f'user{n}@domain.tld')
```

3. Similar to FactoryBoy
```python
class PersonFactory(DataclassFactory[Person]):
email = Sequence(lambda n: f'user{n}@domain.tld')
```

### Drawbacks and Impact

It will be possible to generate guaranteed unique or sequential values.

### Unresolved questions

I would like to hear feedback on the proposed solutions and can take on the implementation.
Perhaps the maintainers can suggest why this might be a bad idea or suggest the best way this can be built into the current codebase.
It might be worth discussing how the sequence should be reset.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the existing __random__ and Use APIs in the factory implementation, since the issue proposes extending those patterns. Compare the three proposed Sequence designs, resolve how sequence state should reset, and define completion around an agreed API that produces sequential values; the issue names no files or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.