diff --git a/connectome/__version__.py b/connectome/__version__.py index a71c5c7..f0788a8 100644 --- a/connectome/__version__.py +++ b/connectome/__version__.py @@ -1 +1 @@ -__version__ = '0.7.0' +__version__ = '0.7.1' diff --git a/connectome/interface/factory.py b/connectome/interface/factory.py index 56d7d7d..6f2bfab 100644 --- a/connectome/interface/factory.py +++ b/connectome/interface/factory.py @@ -208,9 +208,17 @@ def _collect_nodes(self): is_argument = False if callable(value): # 1. a callable argument without annotation: x = some_func - inside_body = getattr(value, '__qualname__', '').startswith(self.name) + qualname = getattr(value, '__qualname__', '') + # if we can't detect this better not to annoy the user + inside_body = None + if qualname.count('.') >= 1: + scope, func_name = qualname.rsplit('.', 1) + # lambdas are a special case. don't know what to do with them + if func_name != '': + inside_body = scope.endswith(self.name) + if name not in annotations: - if not inside_body: + if inside_body is not None and not inside_body: warnings.warn( f'The parameter {name} is defined outside of the class body. Are you trying to pass ' f'a default value for an argument? If so, add a type annotation: "{name}: Callable = ..."', @@ -219,7 +227,7 @@ def _collect_nodes(self): # a function defined inside the body, which also has a type annotation else: is_argument = True - if not inside_body: + if inside_body is not None and inside_body: warnings.warn( f'The default value for the argument {name} is a function, defined inside of the ' f'class body. Did you forget to remove the type annotation?', diff --git a/tests/test_interface/test_interface.py b/tests/test_interface/test_interface.py index 5080897..2d70a25 100644 --- a/tests/test_interface/test_interface.py +++ b/tests/test_interface/test_interface.py @@ -249,8 +249,11 @@ def x(_a): with warnings.catch_warnings(): warnings.filterwarnings('error') with pytest.raises(UserWarning, match='Are you trying to pass a default value for an argument?'): + def _a(): + pass + class B(Transform): - _a = lambda: [] + _b = _a with pytest.raises(UserWarning, match='Did you forget to remove the type annotation?'): class C(Transform):