Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow forwardRefs #56

Open
SolAstri opened this issue Nov 26, 2020 · 3 comments
Open

Allow forwardRefs #56

SolAstri opened this issue Nov 26, 2020 · 3 comments
Labels
enhancement New feature or request reference-rework

Comments

@SolAstri
Copy link

I have a code like this:

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 <module>
    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().
@art049 art049 added the bug Something isn't working label Nov 30, 2020
@art049
Copy link
Owner

art049 commented Nov 30, 2020

Yes actually calling update_forward_refs() would be needed while initializing the model.

However, it would allow to create cyclic references. This is currently not handled as the references are eager for now

@SolAstri
Copy link
Author

SolAstri commented Jun 4, 2021

Is it possible to make cyclic references now?

@gagandeep
Copy link

gagandeep commented Sep 5, 2021

In my self-referential model case even update_forward_refs not working. It's look something like following

from __future__ import annotations

class Task(Model):
    source_task: Task = Reference()

Task.update_forward_refs()

If I am removing = Reference() then its working fine.

@art049 art049 added enhancement New feature or request and removed bug Something isn't working labels Sep 25, 2022
@art049 art049 changed the title How do I create references to not-yet-defined Classes? Allow forwardRefs Sep 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request reference-rework
Projects
None yet
Development

No branches or pull requests

3 participants