-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support replacing params and providers #90
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -577,11 +577,114 @@ class B(Generic[T]): | |||||||||||||||||||||||||||||||
pl[B[int]] = 1.0 | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
def test_setitem_raises_if_key_exists() -> None: | ||||||||||||||||||||||||||||||||
def test_setitem_can_replace_param_with_param() -> None: | ||||||||||||||||||||||||||||||||
pl = sl.Pipeline() | ||||||||||||||||||||||||||||||||
pl[int] = 1 | ||||||||||||||||||||||||||||||||
with pytest.raises(ValueError): | ||||||||||||||||||||||||||||||||
pl[int] = 2 | ||||||||||||||||||||||||||||||||
pl[int] = 2 | ||||||||||||||||||||||||||||||||
assert pl.compute(int) == 2 | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
def test_insert_can_replace_param_with_provider() -> None: | ||||||||||||||||||||||||||||||||
def func() -> int: | ||||||||||||||||||||||||||||||||
return 2 | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
pl = sl.Pipeline() | ||||||||||||||||||||||||||||||||
pl[int] = 1 | ||||||||||||||||||||||||||||||||
pl.insert(func) | ||||||||||||||||||||||||||||||||
assert pl.compute(int) == 2 | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
def test_setitem_can_replace_provider_with_param() -> None: | ||||||||||||||||||||||||||||||||
def func() -> int: | ||||||||||||||||||||||||||||||||
return 2 | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
pl = sl.Pipeline() | ||||||||||||||||||||||||||||||||
pl.insert(func) | ||||||||||||||||||||||||||||||||
pl[int] = 1 | ||||||||||||||||||||||||||||||||
assert pl.compute(int) == 1 | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
def test_insert_can_replace_provider_with_provider() -> None: | ||||||||||||||||||||||||||||||||
def func1() -> int: | ||||||||||||||||||||||||||||||||
return 1 | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
def func2() -> int: | ||||||||||||||||||||||||||||||||
return 2 | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
pl = sl.Pipeline() | ||||||||||||||||||||||||||||||||
pl.insert(func1) | ||||||||||||||||||||||||||||||||
pl.insert(func2) | ||||||||||||||||||||||||||||||||
assert pl.compute(int) == 2 | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
def test_insert_can_replace_generic_provider_with_generic_provider() -> None: | ||||||||||||||||||||||||||||||||
T = TypeVar('T', int, float) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
@dataclass | ||||||||||||||||||||||||||||||||
class A(Generic[T]): | ||||||||||||||||||||||||||||||||
value: T | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
def func1(x: T) -> A[T]: | ||||||||||||||||||||||||||||||||
return A[T](x) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
def func2(x: T) -> A[T]: | ||||||||||||||||||||||||||||||||
return A[T](x + x) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
pl = sl.Pipeline() | ||||||||||||||||||||||||||||||||
pl[int] = 1 | ||||||||||||||||||||||||||||||||
pl.insert(func1) | ||||||||||||||||||||||||||||||||
pl.insert(func2) | ||||||||||||||||||||||||||||||||
assert pl.compute(A[int]) == A[int](2) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
def test_insert_can_replace_generic_param_with_generic_provider() -> None: | ||||||||||||||||||||||||||||||||
T = TypeVar('T', int, float) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
@dataclass | ||||||||||||||||||||||||||||||||
class A(Generic[T]): | ||||||||||||||||||||||||||||||||
value: T | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
def func(x: T) -> A[T]: | ||||||||||||||||||||||||||||||||
return A[T](x + x) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
pl = sl.Pipeline() | ||||||||||||||||||||||||||||||||
pl[int] = 1 | ||||||||||||||||||||||||||||||||
pl[A[T]] = A[T](1) # type: ignore[valid-type] | ||||||||||||||||||||||||||||||||
assert pl.compute(A[int]) == A[int](1) | ||||||||||||||||||||||||||||||||
pl.insert(func) | ||||||||||||||||||||||||||||||||
assert pl.compute(A[int]) == A[int](2) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
def test_setitem_can_replace_generic_provider_with_generic_param() -> None: | ||||||||||||||||||||||||||||||||
T = TypeVar('T', int, float) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
@dataclass | ||||||||||||||||||||||||||||||||
class A(Generic[T]): | ||||||||||||||||||||||||||||||||
value: T | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
def func(x: T) -> A[T]: | ||||||||||||||||||||||||||||||||
return A[T](x + x) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
pl = sl.Pipeline() | ||||||||||||||||||||||||||||||||
pl[int] = 1 | ||||||||||||||||||||||||||||||||
pl.insert(func) | ||||||||||||||||||||||||||||||||
assert pl.compute(A[int]) == A[int](2) | ||||||||||||||||||||||||||||||||
pl[A[T]] = A[T](1) # type: ignore[valid-type] | ||||||||||||||||||||||||||||||||
assert pl.compute(A[int]) == A[int](1) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
def test_setitem_can_replace_generic_param_with_generic_param() -> None: | ||||||||||||||||||||||||||||||||
T = TypeVar('T') | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
@dataclass | ||||||||||||||||||||||||||||||||
class A(Generic[T]): | ||||||||||||||||||||||||||||||||
value: T | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
pl = sl.Pipeline() | ||||||||||||||||||||||||||||||||
pl[A[T]] = A[T](1) # type: ignore[valid-type] | ||||||||||||||||||||||||||||||||
assert pl.compute(A[int]) == A[int](1) | ||||||||||||||||||||||||||||||||
pl[A[T]] = A[T](2) # type: ignore[valid-type] | ||||||||||||||||||||||||||||||||
assert pl.compute(A[int]) == A[int](2) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Maybe it's worth show/check that the inserted generic provider doesn't overwrite or be prioritized to the explicit one...? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unrelated to this change, I think? There are already tests for specializations. |
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
def test_init_with_params() -> None: | ||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we also have a test case that shows instance check like this...?
Just to show that it's passed by lambda function, so the instance is kept not just the value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int
is immutable, not sure I see the value of such a test?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but it shows that it doesn't copy the value and return it but keep the instance itself.
In case we want to pass around
dict
, it might be useful information I thought...?I just suggested what I wanted to try with the changes.
Feel free to drop them...!