Best practice for imports

I’m wondering if anyone could help me understand something from the ‘contributing.md’ documentation. One section reads as follows:

In cases where you have two modules that reference each other, you can fix the
import cycle by using fully qualified names in the types, and enabling
annotations. For example, instead of

from aqt.browser import Browser

def myfunc(b: Browser) -> None:
  pass

use the following instead:

from __future__ import annotations

import aqt

def myfunc(b: aqt.browser.Browser) -> None:
  pass

Is the problem that the first example would cause a circular import somehow? Is this an issue only when contributing to the anki source base or also when writing an addon?

Is the problem that the first example would cause a circular import somehow? Is this an issue only when contributing to the anki source base or also when writing an addon?

It’s a problem in Python overall, and many other programming languages, e.g. JavaScript: Circular dependency - Wikipedia.

The dilemma in this case especially is that we want the value just for typing, not even as a value.

1 Like

The section is contributing.md was targeted at users contributing to Anki’s codebase - when importing Anki’s code into your own add-on, either approach should work since it’s a one-way import.

Thanks very much for the explanations!