0

Python's library neomodel provides the ability to define a relationship with a class that hasn't been defined yet, by using its name as string, which works fine as follows:

from neomodel import StructuredNode, RelationshipTo
# if Foo is already defined
class Bar (structuredNode):
    rel = RelationshipTo(Foo, 'REL')
# if Foo isn't defined
class Bar(StructuredNode):
    rel = RelationshipTo('Foo', 'REL')

I want to create a class in the runtime and provide it with the attribute RelationshipTo, which should make a relationship with undefined yet class, so I did:

Bar = type('Bar', (StructuredNode,), {'rel': RelationshipTo('Foo', 'REL')})

In some point in the runtime later, I define Foo:

Foo = type('Foo', (StructuredNode,), {})

Now if I want to use the class I've just made:

bar = Bar()

The next error still pops up as if I haven't defined Foo:

AttributeError: module '_pydevd_bundle.pydevd_exec2' has no attribute 'Foo'

Note: This error doesn't appear if I at least define Bar statically (not in the runtime)

Would someone explain why that happens and how to define them properly in runtime, please? I appreciate any help!

Rami Ma
  • 966
  • 1
  • 7
  • 13
  • have you tried to inspect the attributes of `Bar`? `print(Bar.rel)` gives an error? Adding the relation, always at runtime, but in another moment i.e after `Foo = ...`? – cards Mar 17 '22 at 12:48
  • Thanks for replying! `Bar.rel` doesn't give any error, but gives ``. Adding the relation afterwards does give the error. Actually, I tried even defining the Foo class before Bar, and still gives the error. It seems that it can't figure out that this Foo class is the same that has relationship with Bar. How can I call it more specifically, known that when I call `Foo` returns `` , using which as a parameter for RelationshipTo doesn't change anything – Rami Ma Mar 17 '22 at 15:10

0 Answers0