Skip to content

I want to put different files, how to use backlink? #577

Answered by roman-right
hgalytoby asked this question in Question
Discussion options

You must be logged in to vote

Your workaround is nice.

You can do things like this also:

File a_circular_import.py:

from __future__ import annotations

from typing import Optional

from pydantic import BaseModel


class A(BaseModel):
    b: Optional[B] = None


from b_circular_import import B

A.update_forward_refs()

File b_circular_import.py:

from __future__ import annotations

from pydantic import BaseModel


class B(BaseModel):
    a: A


from a_circular_import import A

B.update_forward_refs()

b = B(a=A())

print(
    b.json(indent=2)
)

But I like your solution more

Replies: 6 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@hgalytoby
Comment options

Answer selected by hgalytoby
Comment options

You must be logged in to vote
1 reply
@hgalytoby
Comment options

Comment options

You must be logged in to vote
1 reply
@hgalytoby
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@hgalytoby
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants