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
Callable type hint does not work for callable classes #110
Comments
The support for Class from typing import Sequence
from jsonargparse import ArgumentParser, ActionConfigFile
class A:
def __call__(self):
print('A called')
class B:
def __init__(self, arr: Sequence[A]):
self.arr = arr
parser = ArgumentParser()
parser.add_class_arguments(B, 'item_b')
parser.add_argument('--config', action=ActionConfigFile)
config="""
item_b:
arr:
- class_path: __main__.A
"""
cfg = parser.parse_args([f'--config={config}'])
print(cfg.item_b) # Namespace(arr=[Namespace(class_path='__main__.A')]) I don't see in the python standard library a generic The jsonargparse implementation of |
Thank you for your detailed answer! But I think you're a bit wrong about |
I guess you saw an email notification and read my initial comment where questioned if your type hint was wrong. I changed that quickly. Sorry about that. Your type hint is correct and your use of
|
- Fixed bug in check for class_path, init_args dicts. - Fixed module mocks in cli_tests.py. - Callable no longer a simple registered type. - Import paths are now serialized as its shortest form. - Some related code cleanup and some minor unrelated fixes.
@FeryET this is now implemented in commit 9c9dcd2 and will be included in the next release due soon. The feature can be tested for your example with the following: from typing import Callable, Sequence
from jsonargparse import ArgumentParser, ActionConfigFile
class A:
def __call__(self):
print('A called')
class B:
def __init__(self, arr: Sequence[Callable]):
self.arr = arr
parser = ArgumentParser(error_handler=None)
parser.add_class_arguments(B, 'item_b')
parser.add_argument('--config', action=ActionConfigFile)
config="""
item_b:
arr:
- class_path: __main__.A
"""
cfg = parser.parse_args([f'--config={config}'])
print(cfg.item_b)
cfg_init = parser.instantiate_classes(cfg)
print(cfg_init.item_b.arr) |
Hi.
I'm trying to typehint my project accordingly, and I have something like this:
I have this config file:
But this will lead to some incomprehensible errors that will finally show that
This is actually a reproduction of a code I'm trying to implement using PytorchLightning's new CLI, but I think the issue comes from jsonargparse.
The text was updated successfully, but these errors were encountered: