Skip to content

Move type-only imports to TYPE_CHECKING in samplers/_tpe/sampler.py - #6545

Merged
not522 merged 1 commit into
optuna:masterfrom
yasumorishima:fix/tpe-sampler-type-checking
Mar 23, 2026
Merged

not522 merged 1 commit into
optuna:masterfrom
yasumorishima:fix/tpe-sampler-type-checking

Conversation

@yasumorishima

Copy link
Copy Markdown
Contributor

Motivation

Move type-only imports into the TYPE_CHECKING block to resolve ruff TCH001/TCH003/TCH006 violations.

Part of #6029.

Description

  • Move Callable, Sequence (TCH003) and BaseDistribution, CategoricalChoiceType (TCH001) into TYPE_CHECKING block
  • Quote type expressions in cast() calls: cast(float, ...) → cast("float", ...), cast(list, ...) → cast("list", ...) (TCH006)
  • from __future__ import annotations is already present, so all annotation-only imports are safe to defer

Testing

  • ruff check optuna/samplers/_tpe/sampler.py --select TCH → All checks passed
  • pytest tests/samplers_tests/tpe_tests/test_sampler.py → 72 passed

Move Callable, Sequence, BaseDistribution, and CategoricalChoiceType
imports into the TYPE_CHECKING block, and quote type expressions in
cast() calls. This resolves ruff TCH001/TCH003/TCH006 violations.

Part of optuna#6029.

@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 changed the title Move type-only imports to TYPE_CHECKING in samplers/_tpe/sampler.py Move type-only imports to TYPE_CHECKING in samplers/_tpe/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 merged commit 8447e9c into optuna:master Mar 23, 2026
13 checks passed
@yasumorishima
yasumorishima deleted the fix/tpe-sampler-type-checking branch November 11, 2026 02:38
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