Apart from "don't do that, you are painting yourself into a corner", you could also postpone the import of B until you need it. File a.py
:
class A:
def __init__(self, ref):
from b import B
assert isinstance(ref, B)
self.ref = ref
Class B
won't be imported until you instantiate class A
, by which time the module already has been imported fully by module b
.
You can also use a common base class and test for that.