Skip to content

refactor(config): migrate Config/QlibConfig to Pydantic BaseModel#2106

Open
Ayush10 wants to merge 1 commit intomicrosoft:mainfrom
Ayush10:fix/issue-1309-pydantic-config
Open

refactor(config): migrate Config/QlibConfig to Pydantic BaseModel#2106
Ayush10 wants to merge 1 commit intomicrosoft:mainfrom
Ayush10:fix/issue-1309-pydantic-config

Conversation

@Ayush10
Copy link

@Ayush10 Ayush10 commented Jan 30, 2026

Summary

  • Replace the raw dict wrapper in Config/QlibConfig with a typed Pydantic BaseModel (QlibConfigModel)
  • All 15+ consumer files continue to work without any changes
  • Add 39 tests covering all backward-compatible access patterns

Motivation

Issue #1309 requests refactoring the dict-based config management to use Pydantic for type safety, validation, and documentation. This PR is the first step: it introduces the Pydantic model internally while maintaining 100% backward compatibility.

What Changed

  • qlib/config.py: Added QlibConfigModel(BaseModel) with typed fields for all config keys. Config class now wraps a QlibConfigModel instance instead of a raw dict. Uses extra="allow" so dynamic keys still work.
  • tests/test_config_pydantic.py (new): 39 tests covering all backward-compatible access patterns

What Did NOT Change

  • No consumer files changed
  • Both C["key"] and C.key access patterns work identically
  • No new dependencies (pydantic already required via pydantic-settings)

Test plan

  • All 39 new tests pass
  • Manual smoke test: all access patterns work
  • Pickle round-trip verified
  • Nested dict mutation verified

Closes #1309

Replace the raw dict wrapper in Config/QlibConfig with a typed Pydantic
BaseModel (`QlibConfigModel`). All existing access patterns are preserved:
- `C["key"]` dict-style access
- `C.key` attribute access
- `C.get(key, default)` safe access
- `"key" in C` containment check
- `C.update()`, `C.reset()` lifecycle methods
- Pickle serialization
- Nested dict mutation (logging_config, exp_manager, etc.)
- Dynamic keys via `extra="allow"` (flask_server, region, etc.)

The QlibConfigModel provides typed fields with defaults for all config
keys, enabling IDE autocompletion, type checking, and future validation.
No consumer files are changed in this PR.

Add 39 tests covering all backward-compatible access patterns.

Closes microsoft#1309
@Ayush10 Ayush10 force-pushed the fix/issue-1309-pydantic-config branch from 57c8473 to 3c7e319 Compare January 30, 2026 13:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor the setting/config management with pedantic

1 participant