0

If I want to use annotations in both classes in the different modules is cross?

from BModule import B

class A:
  def method(self, b: B):
     pass

~

from AModule import A

class B:
  def method(self, a: A):
     pass

I got a ImportError: cannot import name 'B'? But how if I need annotate this, what to do?

Also if I just import AModule\BModule and use class as attribute of module AModule.A I got AttributeError: module 'BModule' has no attribute 'B'

Artem Selivanov
  • 1,867
  • 1
  • 27
  • 45

1 Answers1

0

What is forcing the dependency? It seems to me that in this case, any method of A that takes a B could be implemented as a method on B that takes an A, so make one of the classes the "main" class and use that class to operate on objects of the other class, if that makes sense?

kfb
  • 6,252
  • 6
  • 40
  • 51
  • It's uncomfortably in my case. This modules placed in different contexts (services). And takes in args `mailbox` of objects (not directly objects, but contains API of these objects). So methods will sends some info to A from B or to B from A (depends on the situation). – Artem Selivanov Sep 16 '16 at 08:32
  • I'm not sure I completely understand what you mean. Could you provide an example with a little more detail in a gist or somewhere (there's no need to pollute the original question)? It sounds like the architecture of the whole system needs looking at in order to remove the dependency. Unless of course, you'd be happy to drop the requirement to annotate the type in this particular instance. – kfb Sep 16 '16 at 08:50
  • Annotations used for generating C++ code (services not only written on Python) and serialization. In C++ I can use `forward declaration` to avoid circular dependency. But also if args annotated I can boldly invoke the specific methods without a doubt. I think I can use string annotations, but it's not pretty. – Artem Selivanov Sep 16 '16 at 08:56