litestar-org / litestar-org/polyfactory

Bug: SQLAlchemy Mapped field type not used correctly

Open
#347 10 comments 0 reactions 1 assignee Claimed by @anthonyjgraff View on GitHub
bug
Dominant language
Python
Stars
1.5k
Forks
120
PR merge metrics
No merged PRs in 30d

Description

### Description

In the below example, you would expect `foo.id` to be an `int`

When `get_field_value` is called, `field_meta.type_args` has correctly been identified as an `int` - however, we end up calling the unwrapped annotation directly

This behaves as expected in `polyfactory==2.2`, provided `from __future__ import annotations` is not imported - and seems to have broken somewhere between there and `polyfactory==2.3`

### URL to code causing the issue

https://github.com/litestar-org/polyfactory/blob/main/polyfactory/factories/base.py#L673

### MCVE

```python
from polyfactory.factories import DataclassFactory
from sqlalchemy.orm import DeclarativeBase, Mapped, MappedAsDataclass, mapped_column

class Base(MappedAsDataclass, DeclarativeBase):
pass

class Foo(Base):
__tablename__ = "foo"

id: Mapped[int] = mapped_column(primary_key=True)

class FooFactory(DataclassFactory[Foo]):
__model__ = Foo

def test_factory():
foo = FooFactory.build()
print(type(foo))
assert isinstance(foo.id, int)

if __name__ == "__main__":
test_factory()
```

### Steps to reproduce

_No response_

### Screenshots

_No response_

### Logs

```bash

Traceback (most recent call last):
File "/app/tests/test_example.py", line 26, in
test_factory()
File "/app/tests/test_example.py", line 22, in test_factory
assert isinstance(foo.id, int)
AssertionError
```

### Release Version

Python 3.11.4

polyfactory==2.8.0
SQLAlchemy==2.0.16

### Platform

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.