-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support PEP484 function argument annotation (#65)
* support PEP484 function argument annotation cc: Asana: by: 顏孜羲 <joseph.yen@gmail.com> * fix python2 compatibility * make Dispatcher.register() supports function annotation * add python 3 only test * Add tests for methods and overlapping signatures * fix compatibility of function annotation to methods cc: Asana: by: 顏孜羲 <joseph.yen@gmail.com> * add test_overlaps_conflict_annotation
- Loading branch information
Showing
5 changed files
with
164 additions
and
26 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
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# import sys | ||
|
||
# from nose import SkipTest | ||
|
||
from multipledispatch import dispatch | ||
from multipledispatch.dispatcher import Dispatcher | ||
|
||
|
||
def test_function_annotation_register(): | ||
f = Dispatcher('f') | ||
|
||
@f.register() | ||
def inc(x: int): | ||
return x + 1 | ||
|
||
@f.register() | ||
def inc(x: float): | ||
return x - 1 | ||
|
||
assert f(1) == 2 | ||
assert f(1.0) == 0.0 | ||
|
||
|
||
def test_function_annotation_dispatch(): | ||
@dispatch() | ||
def inc(x: int): | ||
return x + 1 | ||
|
||
@dispatch() | ||
def inc(x: float): | ||
return x - 1 | ||
|
||
assert inc(1) == 2 | ||
assert inc(1.0) == 0.0 | ||
|
||
|
||
def test_function_annotation_dispatch_custom_namespace(): | ||
namespace = {} | ||
|
||
@dispatch(namespace=namespace) | ||
def inc(x: int): | ||
return x + 2 | ||
|
||
@dispatch(namespace=namespace) | ||
def inc(x: float): | ||
return x - 2 | ||
|
||
assert inc(1) == 3 | ||
assert inc(1.0) == -1.0 | ||
|
||
assert namespace['inc'] == inc | ||
assert set(inc.funcs.keys()) == set([(int,), (float,)]) | ||
|
||
|
||
def test_method_annotations(): | ||
class Foo(): | ||
@dispatch() | ||
def f(self, x: int): | ||
return x + 1 | ||
|
||
@dispatch() | ||
def f(self, x: float): | ||
return x - 1 | ||
|
||
foo = Foo() | ||
|
||
assert foo.f(1) == 2 | ||
assert foo.f(1.0) == 0.0 | ||
|
||
|
||
def test_overlaps(): | ||
@dispatch(int) | ||
def inc(x: int): | ||
return x + 1 | ||
|
||
@dispatch(float) | ||
def inc(x: float): | ||
return x - 1 | ||
|
||
assert inc(1) == 2 | ||
assert inc(1.0) == 0.0 | ||
|
||
|
||
def test_overlaps_conflict_annotation(): | ||
@dispatch(int) | ||
def inc(x: str): | ||
return x + 1 | ||
|
||
@dispatch(float) | ||
def inc(x: int): | ||
return x - 1 | ||
|
||
assert inc(1) == 2 | ||
assert inc(1.0) == 0.0 |