Allow forwardRefs
- Dominant language
- Python
- Stars
- 1.2k
- Forks
- 97
- PR merge metrics
- No merged PRs in 30d
Description
I have a code like this:
```python
from odmantic import Field, Model, EmbeddedModel, Reference
from typing import List
class Revision(EmbeddedModel):
modified_by: "User" = Reference()
class User(Model):
revisions: List[Revision]
```
It yields this error:
```
Traceback (most recent call last):
File "..../api/m_example.py", line 4, in
class Revision(EmbeddedModel):
File ".../lib/python3.8/site-packages/odmantic/model.py", line 443, in __new__
mcs.__validate_cls_namespace__(name, namespace)
File .../lib/python3.8/site-packages/odmantic/model.py", line 290, in __validate_cls_namespace__
parse_obj_as(field_type, value)
File "pydantic/tools.py", line 35, in pydantic.tools.parse_obj_as
File "pydantic/main.py", line 360, in pydantic.main.BaseModel.__init__
File "pydantic/main.py", line 955, in pydantic.main.validate_model
pydantic.errors.ConfigError: field "__root__" not yet prepared so type is still a ForwardRef, you might need to call ParsingModel[ForwardRef('User')].update_forward_refs().
```
Contributor guide
Research direction
Start with the failing example in the issue and inspect odmantic/model.py, especially the class-namespace validation around the traceback. Reproduce the ForwardRef error, then trace how field annotations are parsed for Revision and User. Done means the shown forward-reference declarations can be defined without ConfigError and existing model behavior remains covered by tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mongodb, python
- Domain
- databases
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100