Skip to content

Use TYPE_CHECKING in optuna/_gp/optim_mixed.py to avoid circular imports#6205

Merged
nabenabe0928 merged 2 commits intooptuna:masterfrom
Subodh-12:fix/type-checking-in-opim_mixed
Jul 20, 2025
Merged

Use TYPE_CHECKING in optuna/_gp/optim_mixed.py to avoid circular imports#6205
nabenabe0928 merged 2 commits intooptuna:masterfrom
Subodh-12:fix/type-checking-in-opim_mixed

Conversation

@Subodh-12
Copy link
Copy Markdown
Contributor

Motivation

Currently, a module (optuna._gp.acqf.BaseAcquisitionFunc) is imported unconditionally in optuna/_gp/opim_mixed.py, which can trigger circular‐import errors at runtime. By moving these imports into a if TYPE_CHECKING: block, we ensure they’re only used for static type hints and never executed during normal imports. This improves import safety and prevents spurious CI failures or runtime import errors when using the GP acquisition‐function code.

Description of the changes

python3 -m flake8 optuna/_gp/opim_mixed.py
emits no TC001 warnings

Fixed #6029

Comment on lines +7 to +8
if TYPE_CHECKING:
from optuna._gp.acqf import BaseAcquisitionFunc
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.

Could you merge the TYPE_CHECKING section?
It is already defined below.

@nabenabe0928 nabenabe0928 added the code-fix Change that does not change the behavior, such as code refactoring. label Jul 17, 2025
@Subodh-12
Copy link
Copy Markdown
Contributor Author

issue has been fixed

@nabenabe0928 nabenabe0928 added this to the v4.5.0 milestone Jul 20, 2025
@nabenabe0928 nabenabe0928 merged commit 8948ab0 into optuna:master Jul 20, 2025
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