Skip to content

Move type-only imports to TYPE_CHECKING in samplers/_nsgaiii/_elite_population_selection_strategy.py - #6557

Merged
not522 merged 2 commits into
optuna:masterfrom
Aliipou:fix/type-checking-nsgaiii-elite-selection
Mar 23, 2026
Merged

not522 merged 2 commits into
optuna:masterfrom
Aliipou:fix/type-checking-nsgaiii-elite-selection

Conversation

@Aliipou

@Aliipou Aliipou commented Mar 22, 2026

Copy link
Copy Markdown
Contributor

This PR moves type-only imports in optuna/samplers/_nsgaiii/_elite_population_selection_strategy.py into a TYPE_CHECKING block, as requested in #6029.

Changes

Moved into TYPE_CHECKING:

  • collections.abc.Callable (only used in constraints_func type hint)
  • collections.abc.Sequence (only used in constraints_func return type hint)
  • optuna.samplers._lazy_random_state.LazyRandomState (only used as type annotation for rng parameter)
  • optuna.trial.FrozenTrial (only used in population list type annotations)

numpy is kept as a regular import since it is used extensively at runtime for array operations.

With from __future__ import annotations already present, all annotations are evaluated lazily and these imports are not needed at runtime.

This was identified by running:

ruff check . --select TCH --output-format=concise

Fixes part of #6029

Aliipou and others added 2 commits March 22, 2026 21:07
…i/_elite_population_selection_strategy.py

Move imports that are only used in type annotations into a `TYPE_CHECKING`
block:

- `collections.abc.Callable` (only used in `constraints_func` type hint)
- `collections.abc.Sequence` (only used in `constraints_func` return type)
- `optuna.samplers._lazy_random_state.LazyRandomState` (only used in `rng` parameter annotation)
- `optuna.trial.FrozenTrial` (only used in population list annotations)

`numpy` remains as a regular import since it is used extensively at runtime
(array operations, linear algebra, etc.).

With `from __future__ import annotations` already in place, these deferred
imports are safe and avoid unnecessary module loading.

Fixes part of #6029
@not522 not522 changed the title refactor: move type-only imports to TYPE_CHECKING in samplers/_nsgaiii/_elite_population_selection_strategy.py Move type-only imports to TYPE_CHECKING in samplers/_nsgaiii/_elite_population_selection_strategy.py Mar 23, 2026
@not522 not522 added the code-fix Change that does not change the behavior, such as code refactoring. label Mar 23, 2026
@not522 not522 added this to the v4.9.0 milestone Mar 23, 2026

@not522 not522 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@not522
not522 merged commit 5f42247 into optuna:master Mar 23, 2026
12 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.

2 participants