-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Match functions by parameter names and types
- Loading branch information
1 parent
1ae36a6
commit f1f2226
Showing
3 changed files
with
230 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,115 @@ | ||
from funktools import template | ||
|
||
|
||
class Foo: pass | ||
class Bar: pass | ||
class Baz: pass | ||
class Qux: pass | ||
class Fee: pass | ||
class Foo: | ||
pass | ||
|
||
|
||
class Bar: | ||
pass | ||
|
||
|
||
class Baz: | ||
pass | ||
|
||
|
||
class Qux: | ||
pass | ||
|
||
|
||
class Fee: | ||
pass | ||
|
||
|
||
@template | ||
def funk(): | ||
return "empty" | ||
|
||
|
||
@template[Foo] | ||
def funk(): | ||
return "Foo" | ||
|
||
|
||
@template[Bar] | ||
def funk(): | ||
return "Bar" | ||
|
||
|
||
@template[Baz] | ||
def funk(baz: Baz): | ||
return "Baz" | ||
|
||
|
||
@template[Foo, Bar, Baz] | ||
def funk(baz: Baz): | ||
return "Foo, Bar, Baz" | ||
|
||
|
||
@template | ||
def funk(val: int): | ||
return "int" | ||
|
||
|
||
def foo_qux(): | ||
return "Qux" | ||
|
||
|
||
funk[Qux] = foo_qux | ||
|
||
|
||
def foo_fee(fee: Fee): | ||
return "Fee" | ||
|
||
|
||
funk.add(foo_fee) | ||
|
||
def test_calls() -> None: | ||
|
||
def test_typed_calls() -> None: | ||
assert funk() == "empty" | ||
assert funk[()]() == "empty" | ||
assert funk[Foo]() == "Foo" | ||
assert funk[Bar]() == "Bar" | ||
assert funk[Baz](Baz()) == "Baz" | ||
assert funk(Baz()) == "Baz" | ||
# TODO(lpe): Should this one eventually allow | ||
# funk[Foo, Bar](Baz()) as an equivalent call? | ||
assert funk[Foo, Bar, Baz](Baz()) == "Foo, Bar, Baz" | ||
assert funk[int](31) == "int" | ||
assert funk(42) == "int" | ||
assert funk[Qux]() == "Qux" | ||
assert funk[Fee](Fee()) == "Fee" | ||
assert funk(Fee()) == "Fee" | ||
|
||
|
||
@template | ||
def funky(a: int, b) -> str: | ||
return f"funky({a}: int, {b})" | ||
|
||
|
||
@template | ||
def funky(a: int, c: float) -> str: | ||
return f"funky({a}: int, {c}: float)" | ||
|
||
|
||
@template | ||
def funky(a: int, b: float, *, c: str, d: tuple = (1,)) -> str: | ||
return f"funky({a}: int, {b}: float, *, {c}: str, {d}: tuple)" | ||
|
||
|
||
def test_arg_calls() -> None: | ||
assert funky(1, 2.3) == "funky(1: int, 2.3)" | ||
assert funky(1, b=2.3) == "funky(1: int, 2.3)" | ||
assert funky(a=1, b=2.3) == "funky(1: int, 2.3)" | ||
assert funky(b=2.3, a=1) == "funky(1: int, 2.3)" | ||
|
||
assert funky(1, c=3.4) == "funky(1: int, 3.4: float)" | ||
assert funky(a=1, c=3.4) == "funky(1: int, 3.4: float)" | ||
assert funky(c=3.4, a=1) == "funky(1: int, 3.4: float)" | ||
|
||
assert ( | ||
funky(1, 2.3, d=(1, 2), c="abc") | ||
== "funky(1: int, 2.3: float, *, abc: str, (1, 2): tuple)" | ||
) | ||
assert ( | ||
funky(a=1, b=2.3, c="abc") | ||
== "funky(1: int, 2.3: float, *, abc: str, (1,): tuple)" | ||
) |