Skip to content

Improve subclass binding when __init__ is not defined. #17

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion argbind/argbind.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,18 @@ def bind(*args, without_prefix=False, positional=False, group: Union[list, str]

def decorator(object_or_func):
func = object_or_func
prefix = func.__qualname__ # get prefix before a potential monkey patch below changes it to a superclass's prefix
is_class = inspect.isclass(func)
if is_class:
# If the class has no __init__ method, find the __init__ method from the closest superclass
# that defines one. Then monkey patch that __init__ onto the class.
if '__init__' not in func.__dict__:
for base in func.__mro__[1:]:
if '__init__' in base.__dict__:
func.__init__ = base.__init__ # monkey patch
break
func = getattr(func, "__init__")

prefix = func.__qualname__
if "__init__" in prefix:
prefix = prefix.split(".")[0]

Expand Down
4 changes: 2 additions & 2 deletions examples/bind_existing/with_argbind.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class BoundClass(MyClass):
argbind.parse_args() # add for help text, though it isn't used here.

args = {
'MyClass.x': 'from binding',
'pattern/MyClass.x': 'from binding in scoping pattern',
'BoundClass.x': 'from binding',
'pattern/BoundClass.x': 'from binding in scoping pattern',
'my_func.x': 123,
'args.debug': True # for printing arguments passed to each function
}
Expand Down
42 changes: 42 additions & 0 deletions examples/bind_existing/with_argbind_abstract_classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import abc
from typing import Any, Dict

import argbind


class Base(abc.ABC):

def __init__(self, config: Dict[str, Any] = None):
self.config = config

@abc.abstractmethod
def action(self):
"""Do something"""


@argbind.bind()
class Sub1(Base):

def action(self):
print('hello', self.config)


@argbind.bind()
class Sub2(Base):

def action(self):
print('goodbye', self.config)


if __name__ == "__main__":

argbind.parse_args() # add for help text, though it isn't used here.

args = {
'Sub1.config': {"a": "apple", "b": "banana"},
'Sub2.config': {"c": "cherry", "d": "durian"},
}

with argbind.scope(args):
Sub1().action() # prints "hello {'a': 'apple', 'b': 'banana'}"
Sub2().action() # prints "goodbye {'c': 'cherry', 'd': 'durian'}"