confingy.tracking#
tracking
#
Lazy
#
Bases: Generic[T]
A proxy that delays instantiation until the object is actually used.
This class wraps a configuration for an object and only creates the
actual instance when the instantiate() method is called.
This class is returned by the lazy function or when using the
Lazy classmethod on a @track-decorated class.
It can also be used as a type hint:
def process(model: Lazy[Model]):
# model must be a Lazy[Model]
actual_model = model.instantiate()
All internal attributes of the Lazy object are prepended with '_confingy_' to avoid
name collisions with the wrapped class's constructor arguments (including underscore-prefixed ones).
Source code in src/confingy/tracking.py
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 | |
__getattr__
#
__getattr__(name: str) -> Any
Access configuration parameters as attributes.
This allows direct access to the constructor arguments stored in the Lazy config, enabling easy inspection and chained access for nested Lazy objects.
Examples:
lazy = MyDataset.lazy(data=[1,2,3], processor=Pipeline.lazy(scalers=[...]))
lazy.data # Returns [1,2,3]
lazy.processor # Returns the Pipeline Lazy
lazy.processor.scalers # Chained access works!
Source code in src/confingy/tracking.py
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | |
__setattr__
#
__setattr__(name: str, value: Any) -> None
Set configuration parameters as attributes with validation.
Internal attributes (starting with 'confingy') are set normally. Other attributes update the configuration and trigger validation.
Examples:
lazy = MyDataset.lazy(data=[1,2,3], processor=p)
lazy.data = [4,5,6] # Updates config, validates
lazy.processor = new_p # Updates config, validates
Source code in src/confingy/tracking.py
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | |
get_config
#
get_config() -> dict[str, Any]
Get a copy of the configuration used to create this lazy instance. The returned dictionary contains the constructor arguments for the lazy instance.
Source code in src/confingy/tracking.py
421 422 423 424 425 426 | |
copy
#
copy(**updates: Any) -> Lazy[T]
Create a new Lazy instance with updated configuration.
This provides an immutable update pattern - the original Lazy is unchanged, and a new Lazy is returned with the specified updates applied.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**updates
|
Any
|
Keyword arguments to update in the new Lazy's config. These override the original config values. |
{}
|
Returns:
| Type | Description |
|---|---|
Lazy[T]
|
A new Lazy instance with the updated configuration. |
Examples:
lazy = MyDataset.lazy(data=[1,2,3], processor=p)
# Create a new Lazy with updated data
new_lazy = lazy.copy(data=[4,5,6])
# Original is unchanged
assert lazy.data == [1,2,3]
assert new_lazy.data == [4,5,6]
# Chain copies for multiple updates
another = lazy.copy(data=[7,8,9]).copy(processor=new_p)
Source code in src/confingy/tracking.py
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 | |
instantiate
#
instantiate() -> T
Create and return an instance of the wrapped class.
Each call creates a new instance - this is a factory method.
Returns:
| Type | Description |
|---|---|
T
|
A new instance of the wrapped class, constructed with the stored config. |
Source code in src/confingy/tracking.py
485 486 487 488 489 490 491 492 493 494 | |
unlens
#
unlens() -> Any
Reconstruct the object, preserving the original laziness structure.
When a Lazy is created via lens() from a tracked instance, calling
unlens() will instantiate it. When created from an existing Lazy,
it remains a Lazy.
This enables a round-trip: lens(obj) -> modify -> unlens() preserves
whether each node was originally Lazy or instantiated.
Returns:
| Type | Description |
|---|---|
Any
|
Either an instantiated object or a new Lazy, depending on how |
Any
|
this Lazy was created. |
Examples:
# From tracked instance - unlens() instantiates
obj = Outer(middle=Middle(inner=Inner(value=42)))
l = lens(obj)
l.middle.inner.value = 100
new_obj = l.unlens() # Returns Outer instance
# From Lazy - unlens() returns Lazy
lazy = Outer.lazy(middle=Middle.lazy(inner=Inner.lazy(value=42)))
l = lens(lazy)
l.middle.inner.value = 100
new_lazy = l.unlens() # Returns Lazy[Outer]
Source code in src/confingy/tracking.py
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 | |
__call__
#
__call__(*args: Any, **kwargs: Any) -> Lazy[T]
Make Lazy callable to support the lazy(Class)(...) pattern.
When called with no arguments, returns self. When called with arguments, merges them into the config and returns a new Lazy.
Source code in src/confingy/tracking.py
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 | |
__getstate__
#
__getstate__() -> dict
Prepare state for pickling, excluding unpicklable validation model.
Source code in src/confingy/tracking.py
591 592 593 594 595 596 597 598 599 600 | |
__setstate__
#
__setstate__(state: dict) -> None
Restore state after unpickling.
Source code in src/confingy/tracking.py
602 603 604 605 606 607 608 609 610 611 612 613 | |
disable_validation
#
disable_validation()
Context manager to disable validation for tracked and lazy objects.
Examples:
class NonTrackedObject:
def __init__(self, value):
self.value = value
class TrackedObject:
def __init__(self, obj: NonTrackedObject):
self.obj = obj
# Raises validation error
track(TrackedObject)(obj=NonTrackedObject(value=10))
with disable_validation():
# No validation error
track(TrackedObject)(obj=NonTrackedObject(value=10))
Source code in src/confingy/tracking.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
is_class
#
is_class(obj: Any) -> TypeGuard[type[Any]]
Check if an object is a class.
Source code in src/confingy/tracking.py
74 75 76 | |
lazy
#
lazy(cls: Callable[P, T]) -> Callable[P, Lazy[T]]
lazy(
cls: Callable[P, T], *args: args, **kwargs: kwargs
) -> Lazy[T]
lazy(cls: Any, *args: Any, **kwargs: Any) -> Any
Create a lazy instance of a @track-decorated class.
This function provides explicit lazy instantiation. Classes should be decorated with @track, and then lazy is used when you want deferred instantiation.
Examples:
@track
class ExpensiveModel:
def __init__(self, size: int):
self.weights = np.random.randn(size, size)
# Normal instantiation (immediate)
model = ExpensiveModel(size=1000)
# Lazy instantiation (deferred) - two ways:
lazy_model = lazy(ExpensiveModel)(size=1000) # Returns Lazy[ExpensiveModel]
# Or more directly:
lazy_model = lazy(ExpensiveModel, size=1000) # Returns Lazy[ExpensiveModel]
# Access when needed
result = lazy_model.instantiate().forward(data)
Source code in src/confingy/tracking.py
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 | |
lens
#
lens(obj: Any) -> Lazy[Any]
Convert a tracked or Lazy instance to a Lazy for nested parameter updates.
This function provides a unified interface for modifying nested configurations.
After making changes, call unlens() to reconstruct the object with the
original laziness structure preserved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Either a tracked instance (has |
required |
Returns:
| Type | Description |
|---|---|
Lazy[Any]
|
A Lazy instance that can be modified via attribute access. |
Examples:
@track
class Outer:
def __init__(self, middle: Middle):
self.middle = middle
@track
class Middle:
def __init__(self, inner: Inner):
self.inner = inner
@track
class Inner:
def __init__(self, value: int):
self.value = value
# Create a tracked instance
obj = Outer(middle=Middle(inner=Inner(value=42)))
# Use lens to modify nested values
l = lens(obj)
l.middle.inner.value = 100
# Reconstruct with original structure (all instantiated)
new_obj = l.unlens()
assert new_obj.middle.inner.value == 100
# Works with Lazy instances too
lazy_obj = Outer.lazy(middle=Middle.lazy(inner=Inner.lazy(value=42)))
l = lens(lazy_obj)
l.middle.inner.value = 100
new_lazy = l.unlens() # Returns Lazy since original was Lazy
Source code in src/confingy/tracking.py
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 | |
track
#
track(
cls_or_instance: None = None, *, _validate: bool = True
) -> Callable[[C], C]
track(cls_or_instance: C, *, _validate: bool = True) -> C
track(
cls_or_instance: Optional[Any] = None,
*args: Any,
_validate: bool = True,
**kwargs: Any,
) -> Any
Track constructor arguments for serialization.
Can be used as a decorator or function to enable argument tracking for later serialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
_validate
|
bool
|
Whether to validate constructor arguments with pydantic (default: True) can be overridden by the context manager disable_validation |
True
|
Example
from confingy import track, save_config
@track
class Dataset:
def __init__(self, path: str, size: int):
self.path = path
self.size = size
# Arguments are tracked and can be serialized
dataset = Dataset(path="/data", size=1000)
save_config(dataset, "config.json")
# Skip validation
@track(_validate=False)
class FastDataset:
def __init__(self, path: str):
self.path = path
You can also turn a tracked class into a lazy one:
lazy_dataset = Dataset.lazy(path="/data", size=1000)
Source code in src/confingy/tracking.py
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 | |
update
#
update(parent_obj: Any) -> Callable[..., Any]
Create an updated version of a tracked or lazy object with new constructor arguments.
This function supports "inheritance" for confingy objects by allowing you to create a new instance with updated parameters while preserving the original object's type and validation behavior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent_obj
|
Any
|
Either a tracked instance (has |
required |
Returns:
| Type | Description |
|---|---|
Callable[..., Any]
|
A function that accepts new constructor arguments and returns: |
Callable[..., Any]
|
|
Callable[..., Any]
|
|
Examples:
# With tracked instances
@track
class Foo:
def __init__(self, bar: str, baz: int = 10):
self.bar = bar
self.baz = baz
parent_foo = track(Foo)(bar="hello")
child_foo = update(parent_foo)(bar="world") # bar="world", baz=10
# With lazy instances
parent_lazy = lazy(Foo)(bar="hello")
child_lazy = update(parent_lazy)(bar="world") # Returns Lazy[Foo]
Source code in src/confingy/tracking.py
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 | |