Skip to content
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

class instantiation with multiple parameters #372

Closed
hadipash opened this issue Sep 7, 2023 · 4 comments · Fixed by #383
Closed

class instantiation with multiple parameters #372

hadipash opened this issue Sep 7, 2023 · 4 comments · Fixed by #383
Labels
bug Something isn't working

Comments

@hadipash
Copy link
Contributor

hadipash commented Sep 7, 2023

Hello, I have a question on how I can instantiate a class that requires multiple parameters that determined during run time only?
I followed the example on Optimizer but can't figure out how I can expand it to multiple parameters:

# Example from the website
class Optimizer:
    def __init__(self, params: Iterable):
        self.params = params


class SGD(Optimizer):
    def __init__(self, params: Iterable, lr: float):
        super().__init__(params)
        self.lr = lr


if __name__ == '__main__':
    value = {
        "class_path": "SGD",
        "init_args": {
            "lr": 0.01,
        },
    }

    parser = ArgumentParser()
    parser.add_argument("--optimizer", type=Callable[[Iterable], Optimizer])
    cfg = parser.parse_args(["--optimizer", str(value)])
    init = parser.instantiate_classes(cfg)
    optimizer = init.optimizer([1, 2, 3])

Let's say I want to add one more parameter to SGD(params, weight, lr). How will the flow change?

class SGD(Optimizer):
    def __init__(self, params: Iterable, weight: float, lr: float):
...
parser.add_argument("--optimizer", type=Callable[[Iterable, float], Optimizer])

gives the following error:

usage: example.py [-h] [--optimizer OPTIMIZER]
example.py: error: Parser key "optimizer":
  Expected a dot import path string: SGD

Setting class_path to __main__.SGD produces another error:

usage: example.py [-h] [--optimizer OPTIMIZER]
example.py: error: Parser key "optimizer":
  Type typing.Callable[[typing.Iterable, float], __main__.Optimizer] expects a function or a callable class: Expected '__main__.SGD' to be a class that instantiates into callable or a subclass of False.. Got value: Namespace(class_path='__main__.SGD', init_args=Namespace(lr=0.01))

@mauvilsa
Copy link
Member

mauvilsa commented Sep 7, 2023

Thank you for reporting!

This seems to be a bug in line:

return_type = typehint.__args__[1]

Should be instead return_type = typehint.__args__[-1].

@mauvilsa mauvilsa added the bug Something isn't working label Sep 7, 2023
@mauvilsa
Copy link
Member

mauvilsa commented Sep 8, 2023

@hadipash would you be interested in contributing the fix?

@hadipash
Copy link
Contributor Author

Interested, but can only start working on this issue in the middle of next week at the earliest, if it's ok.

@mauvilsa
Copy link
Member

can only start working on this issue in the middle of next week at the earliest, if it's ok.

That is okay. The task should be simple. Possibly just that line that I referenced above and add a unit test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants