Skip to content

Commit

Permalink
Merge pull request #72 from altescy/modify-lazy-constructor-prop
Browse files Browse the repository at this point in the history
Return lazy constructor by tracing registrable
  • Loading branch information
altescy authored Sep 19, 2024
2 parents 76e0576 + b61a3c9 commit b06384b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
20 changes: 20 additions & 0 deletions colt/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,26 @@ def _catname(parent: str, *keys: Union[int, str]) -> str:
key = ".".join(str(x) for x in keys)
return f"{parent}.{key}" if parent else key

def _get_constructor(
self,
config: Any,
param_name: str,
annotation: Optional[Union[Type[T], Callable[..., T]]] = None,
) -> Optional[Union[Type[T], Callable[..., T]]]:
if not isinstance(config, Mapping):
return None
if self._typekey not in config:
return None
name = config[self._typekey]
if not isinstance(name, str):
return None
return self._get_constructor_by_name(
name,
param_name,
annotation,
allow_to_import=not self._strict,
)

def _construct_args(
self,
constructor: Callable[..., T],
Expand Down
19 changes: 16 additions & 3 deletions colt/lazy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import typing
from copy import deepcopy
from typing import Any, Generic, Mapping, Optional, Sequence, Type, TypeVar, Union
from typing import (
Any,
Callable,
Generic,
Mapping,
Optional,
Sequence,
Type,
TypeVar,
Union,
)

from colt.utils import update_field

Expand Down Expand Up @@ -32,8 +42,11 @@ def config(self) -> Any:
return self._config

@property
def constructor(self) -> Optional[Type[T]]:
return self._cls
def constructor(self) -> Optional[Union[Type[T], Callable[..., T]]]:
return (
self._builder._get_constructor(self._config, self._param_name, self._cls)
or self._cls
)

def update(
self,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "colt"
version = "0.13.0"
version = "0.13.1"
description = "A configuration utility for Python object."
authors = ["altescy <altescy@fastmail.com>"]
license = "MIT License"
Expand Down
31 changes: 30 additions & 1 deletion tests/test_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

import colt
from colt import ConfigurationError, Lazy
from colt import ConfigurationError, Lazy, Registrable


def test_lazy() -> None:
Expand Down Expand Up @@ -49,3 +49,32 @@ class Bar:

with pytest.raises(ConfigurationError):
bar.foo.update(name=123)


def test_lazy_constructor() -> None:
@dataclasses.dataclass
class Foo:
name: str

@dataclasses.dataclass
class Bar:
foo: Lazy[Foo]

bar = colt.build({"foo": {"name": "foo"}}, Bar)

assert bar.foo.constructor == Foo


def test_lazy_constructor_with_registrable() -> None:
class Foo(Registrable): ...

@Foo.register("bar")
class Bar(Foo): ...

@dataclasses.dataclass
class Baz:
foo: Lazy[Foo]

baz = colt.build({"foo": {"@type": "bar"}}, Baz)

assert baz.foo.constructor == Bar
2 changes: 1 addition & 1 deletion tests/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


def test_version() -> None:
assert colt.__version__ == "0.13.0"
assert colt.__version__ == "0.13.1"

0 comments on commit b06384b

Please # to comment.