Skip to content
Snippets Groups Projects
Commit fc1b1eac authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

unit test for overloading

parent af6a979f
No related branches found
No related tags found
No related merge requests found
......@@ -3,3 +3,4 @@
* minor pylint improvements
* better coverage
* SyncableDroppable.cleanup() bugs out, wrote a quick patch for it to do nothing and filed as #61.
* unit tests for overloading
\ No newline at end of file
__version__ = '2.24.1a4'
__version__ = '2.24.1a5'
......@@ -28,17 +28,33 @@ class class_or_instancemethod(classmethod):
class TypeSignature(inspect.Signature):
"""
A type signature.
You can compare signatures:
>>> def a(a: object):
>>> pass
>>> def b(a: int):
>>> pass
>>> TypeSignature.from_fun(a) < TypeSignature(b)
"""
__slots__ = ()
def __init__(self, t_sign: inspect.Signature):
"""
:param t_sign: a inspect.Signature
"""
self._return_annotation = t_sign._return_annotation
self._parameters = t_sign._parameters
@staticmethod
def from_fun(fun):
def from_fun(fun) -> TypeSignature:
"""Return a type signature from a function"""
return TypeSignature(inspect.Signature.from_callable(fun))
def can_be_called_with_args(self, *args, **kwargs) -> bool:
"""Can this type signature be called with following arguments?"""
called = self._bind(*args, **kwargs)
# pylint: disable=protected-access
......@@ -46,6 +62,7 @@ class TypeSignature(inspect.Signature):
for arg_name, arg_value in called.items())
def is_more_generic_than(self, b: TypeSignature) -> bool:
"""Is this type signature more generic than an other?"""
if self == {}:
for key in self:
key1 = self[key]
......@@ -67,9 +84,7 @@ class TypeSignature(inspect.Signature):
bound_args = self.bind(*args, **kwargs)
bound_args.apply_defaults()
for param_name, param_value in bound_args.arguments.items():
if isinstance(param_value, self._parameters[param_name].annotation):
continue
else:
if not isinstance(param_value, self._parameters[param_name].annotation):
return False
return True
......@@ -125,13 +140,9 @@ class overload:
"""
matchings = []
for sign, fun in self.type_signatures_to_functions.items():
print('Matching %s against %s', sign, fun)
if sign.matches(*args, **kwargs):
matchings.append((sign, fun))
else:
print('Did not score a math between %s:%s and %s', args, kwargs, )
matchings.sort()
print(matchings)
if not matchings:
raise TypeError('No matching entries!')
else:
......
import unittest
from satella.coding import overload, TypeSignature
class TestOverloading(unittest.TestCase):
def test_type_signature(self):
def a(a: object):
pass
def b(a: int):
pass
self.assertLess(TypeSignature.from_fun(a), TypeSignature.from_fun(b))
def test_something(self):
@overload
def fun(i: int):
self.assertIsInstance(i, int)
@fun.overload
def fun(i: str):
self.assertIsInstance(i, str)
fun(2)
fun('test')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment