litestar-org / litestar-org/polyfactory

Bug: Random values from a base Enum

Open
#640 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

### Description

I don't think it's a bug, but not sure:

Consider this example:

```python
from dataclasses import dataclass
from enum import Enum

from polyfactory.factories import DataclassFactory

class BaseEnum(Enum): ...

class OtherEnum(BaseEnum): # <--- inherits from BaseEnum
VALUE_1 = 1
VALUE_2 = 2

@dataclass
class MyModel:
values: dict[BaseEnum, int] # <--- a dict with keys from the BaseEnum

class MyModelFactory(DataclassFactory[MyModel]): ...

if __name__ == "__main__":
my_model = MyModelFactory.build()
```

This will raise an IndexError: `IndexError: Cannot choose from an empty sequence`.
Which is obvious as Litestar will try to populate the keys in the `values` dict by doing this: `__random__(list(BaseEnum))`. That results in an empty list which gives the error.

But the BaseEnum (which I don't have control over) is only the base enum for the `OtherEnum` which *does* contain values.

Is there any way in my factory to tell when `BaseEnum` is encountered it should pick values from `OtherEnum` ?

### URL to code causing the issue

_No response_

### MCVE

```python

```

### Steps to reproduce

```bash
Run the above example.
```

### Screenshots

### Logs

```bash

```

### Release Version

2.19.0

### Platform

- [x] Linux
- [ ] Mac
- [ ] Windows
- [ ] Other (Please specify in the description above)

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 reproducing the example with Python 2.19.0 and trace DataclassFactory's dictionary-key generation through __random__, the entry point named in the report. Clarify whether the intended behavior is to resolve BaseEnum values from a subclass or to handle empty enums differently, then add a regression test once the expected behavior is agreed.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.