litestar-org / litestar-org/polyfactory

Bug: forward referenced Circular import Dataclass don't work with `DataclassFactory`

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

Description

### Description

The MCVE results in the following error:
`NameError: name 'AAA' is not defined`

Ignore() is not working.

### URL to code causing the issue

_No response_

### MCVE

```python
# aaa.py
from dataclasses import dataclass
from bbb import BBB

@dataclass
class AAA:
a: int
b: str
c: float
other: BBB

# bbb.py

from __future__ import annotations
from dataclasses import dataclass
from typing import TYPE_CHECKING
from polyfactory.factories import DataclassFactory

if TYPE_CHECKING:
from aaa import AAA

@dataclass
class BBB:
a: int
b: str
c: float
other: "AAA" | None

class BBBFactory(DataclassFactory):
__model__ = BBB

bbb = BBBFactory.build() # <= raise NameError from typing.py
# bbb = BBB(a=1, b="test", c=3.14, other=None) # <= works well
```

### Steps to reproduce

```bash

```

### Screenshots

"In the format of: `![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)`"

### Logs

```bash

```

### Release Version

polyfactory==2.22.2

### Platform

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

Contributor guide

Open the contributing guide

Research direction

Start by running the MCVE in aaa.py and bbb.py, focusing on BBBFactory.build() and the NameError reported from typing.py. Read the DataclassFactory behavior around forward references and Ignore(); done means the circular-import example builds without the NameError while preserving the explicitly ignored field behavior.

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
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.