Skip to content

Prevent future circular import errors #3088

@betulependule

Description

@betulependule

It happened to me a couple of times that when working on a new feature / making changes in the code, I made a new import across our sub-modules and ran into the circular import error.

Often, we use imports for the sake of type hinting, meaning some imports inside our codebase are not needed during runtime. Some circular imports could be prevented in the future by importing such modules / classes only when type checking is done. For example:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from packit_service.worker.helpers.testing_farm import (
        DownstreamTestingFarmJobHelper,
        TestingFarmJobHelper,
    )

This would require the imported classed being referred to as "DownstreamTestingFarmJobHelper" and "TestingFarmJobHelper" inside type hints (with the quotation marks). In order not to be forced to use quotation marks, it is possible to define the following at the start of the file:

from __future__ import annotations

However, doing so will resolve in ruff complaining when running pre-commit, so the check would either have to be disabled or we just have to use quotation marks.

UP045 Use `X | None` for type annotations

TODO:

  • In general, we are already doing this (only importing when TYPE_CHECKING), so check whether there are any instances where we are importing something during runtime when we don't need to, and if so, correct it.
  • Edit the styleguide for Gemini so that it suggests such changes in future PR reviews.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions