Skip to content

Move type-only imports to TYPE_CHECKING in samplers/_nsgaiii/_sampler.py - #6558

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

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

Conversation

@Aliipou

@Aliipou Aliipou commented Mar 22, 2026

Copy link
Copy Markdown
Contributor

Move type-only imports in optuna/samplers/_nsgaiii/_sampler.py into the TYPE_CHECKING block, as requested in #6029.

Moved into TYPE_CHECKING: Callable, Sequence (stdlib), numpy (third-party), and BaseDistribution (application). These are only used in type annotations.

Kept as regular imports: LazyRandomState (instantiated at runtime), TrialState (enum values used at runtime), FrozenTrial (objects received at runtime).

Identified by: ruff check . --select TCH

Fixes part of #6029

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

Move imports only used in type annotations into the `TYPE_CHECKING` block:

- `collections.abc.Callable` (only in type hints)
- `collections.abc.Sequence` (only in type hints)
- `numpy` (only in `np.ndarray` annotation)
- `optuna.distributions.BaseDistribution` (only in type annotations)

`LazyRandomState`, `TrialState`, and `FrozenTrial` remain as regular
imports since `LazyRandomState` is instantiated at runtime (`LazyRandomState(seed)`)
and `TrialState` enum values are accessed at runtime.

With `from __future__ import annotations`, all annotations are evaluated
lazily so the deferred imports are safe.

Fixes part of #6029
@not522 not522 changed the title refactor: move type-only imports to TYPE_CHECKING in samplers/_nsgaiii/_sampler.py Move type-only imports to TYPE_CHECKING in samplers/_nsgaiii/_sampler.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 d5f332c 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