Skip to content

Use TYPE_CHECKING in optuna/_gp/acqf.py to avoid circular imports#6204

Merged
nabenabe0928 merged 1 commit intooptuna:masterfrom
CarvedCoder:fix/type-checking-for-circular-imports
Jul 17, 2025
Merged

Use TYPE_CHECKING in optuna/_gp/acqf.py to avoid circular imports#6204
nabenabe0928 merged 1 commit intooptuna:masterfrom
CarvedCoder:fix/type-checking-for-circular-imports

Conversation

@CarvedCoder
Copy link
Copy Markdown

@CarvedCoder CarvedCoder commented Jul 16, 2025

Motivation

Currently, two modules (GPRegressor and SearchSpace) are imported unconditionally in optuna/_gp/acqf.py, which can trigger circular‐import errors at runtime. By moving these imports into a if TYPE_CHECKING: block, we ensure that they’re only used for static type hints and never executed during normal imports.

Description of the changes

This fixes issue #6029 by wrapping the two imports in optuna/_gp/acqf.py with if TYPE_CHECKING: so they’re only for type hints.
Also ran python3 -m flake8 optuna/_gp/acqf.py —> no more TC001 warnings in that file.

Fixes #6029

Comment on lines +10 to +14
from optuna._gp.gp import GPRegressor
from optuna._gp.search_space import SearchSpace

if TYPE_CHECKING:
from optuna._gp.gp import GPRegressor
from optuna._gp.search_space import SearchSpace

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TYPE_CHECKING section is already prepared below, so could you please move these module imports to the section?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Thanks for pointing that out , I’ll move the imports into the existing TYPE_CHECKING section and update the PR shortly

@nabenabe0928 nabenabe0928 added the code-fix Change that does not change the behavior, such as code refactoring. label Jul 17, 2025
@CarvedCoder CarvedCoder force-pushed the fix/type-checking-for-circular-imports branch from 3a475e5 to 8e29b0e Compare July 17, 2025 07:06
Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the change, LGTM:)

@nabenabe0928 nabenabe0928 added this to the v4.5.0 milestone Jul 17, 2025
@nabenabe0928 nabenabe0928 enabled auto-merge July 17, 2025 07:09
@nabenabe0928 nabenabe0928 changed the title Use TYPE_CHECKING in optuna/_gp/acqf.py to avoid circular imports Use TYPE_CHECKING in optuna/_gp/acqf.py to avoid circular imports Jul 17, 2025
@nabenabe0928 nabenabe0928 merged commit 9544a92 into optuna:master Jul 17, 2025
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code-fix Change that does not change the behavior, such as code refactoring.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use TYPE_CHECKING if necessary

2 participants