Visitar URL original
Add config validator that maps null -> default by deruyter92 · Pull Request #3475 · DeepLabCut/DeepLabCut · GitHub
Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add tests for DefaultIfNone validator
  • Loading branch information
deruyter92 committed Sep 1, 2026
commit aa4bedca631a471431944e63eddd17a9358e6b76
58 changes: 58 additions & 0 deletions tests/core/config/test_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#
# DeepLabCut Toolbox (deeplabcut.org)
# © A. & M.W. Mathis Labs
# https://github.com/DeepLabCut/DeepLabCut
#
# Licensed under GNU Lesser General Public License v3.0
#
"""Tests for the annotated types in deeplabcut.core.config.validation."""

import pytest
from pydantic import ValidationError

from deeplabcut.core.config import DLCBaseConfig
from deeplabcut.core.config.validation import DefaultIfNone, Fraction


class ThresholdConfig(DLCBaseConfig):
"""Config exercising DefaultIfNone over a constrained type."""

threshold: DefaultIfNone[Fraction] = 0.01
name: DefaultIfNone[str] = "detector"


# ------------------------------------------------------------------
# DefaultIfNone
# ------------------------------------------------------------------


class TestDefaultIfNone:
def test_none_falls_back_to_default(self):
assert ThresholdConfig(threshold=None).threshold == 0.01

def test_explicit_value_is_kept(self):
assert ThresholdConfig(threshold=0.6).threshold == 0.6

def test_falsy_value_is_kept(self):
# A plain ``value or default`` would silently turn 0.0 into the default.
assert ThresholdConfig(threshold=0.0).threshold == 0.0

def test_applies_to_any_wrapped_type(self):
assert ThresholdConfig(name=None).name == "detector"
assert ThresholdConfig(name="ssdlite").name == "ssdlite"

def test_wrapped_annotation_is_not_swallowed(self):
# The generic alias must compose with Fraction, not replace it.
with pytest.raises(ValidationError):
ThresholdConfig(threshold=1.5)

def test_none_is_normalized_on_assignment(self):
# DLCBaseConfig sets validate_assignment=True, so the validator runs here too.
cfg = ThresholdConfig(threshold=0.6)
cfg.threshold = None
assert cfg.threshold == 0.01

def test_default_is_serialized_instead_of_none(self):
# The null must not survive a load/save round trip.
cfg = ThresholdConfig.from_dict({"threshold": None})
assert cfg.to_dict()["threshold"] == 0.01