Error assert when use lazy_instance
pjgao opened this issue Β· comments
π Bug report
when use lazy_instance as default value, there is a assert cannot satisfy
assert name not in self.__dict__
To reproduce
- Using the CLI function
# a.py
import tensorflow as tf
from jsonargparse import lazy_instance, CLI
def func(metric: tf.keras.metrics.Metric = lazy_instance(tf.keras.metrics.AUC)):
print(metric.metrics.name)
CLI(func)
- print config from cli
python a.py --print_config
trace:
Traceback (most recent call last):
File "D:\a.py", line 5, in <module>
def func(metric: tf.keras.metrics.Metric = lazy_instance(tf.keras.metrics.AUC)):
File "C:\Users\xx\AppData\Local\pypoetry\Cache\virtualenvs\deepmodel-jiO4NXK3-py3.10\lib\site-packages\jsonargparse\_typehints.py", line 1420, in lazy_instance
return lazy_init_class(class_type, kwargs)
File "C:\Users\xx\AppData\Local\pypoetry\Cache\virtualenvs\deepmodel-jiO4NXK3-py3.10\lib\site-packages\jsonargparse\_typehints.py", line 1360, in __init__
assert name not in self.__dict__
AssertionError
when comment # assert name not in self.__dict__
it works success
metric:
class_path: keras.metrics.AUC
init_args:
num_thresholds: 200
curve: ROC
summation_method: interpolation
multi_label: false
from_logits: false
-->
Expected behavior
run normaly
Environment
- jsonargparse version (e.g., 4.29.0):
- Python version (e.g., 3.10.1):
- How jsonargparse was installed (e.g.
pip install jsonargparse[all]
): - OS (e.g., Linux): wsl ubuntu 20.24
- tensorflow 2.10
Thank you for reporting! I will look at it.
For now some information. That assert is there because I didn't know if it could happen, because it it did, the implementation does not currently consider it. Removing the assert wouldn't impact the CLI logic, but the lazy instance could misbehave if used. Also, the problem happens with tensorflow 2.10 but not with the latest version. Would be good to extend the implementation to consider this case. But I would do so only if I am able to write a unit test for it, and I haven't been able for the moment.
For now some information. That assert is there because I didn't know if it could happen, because it it did, the implementation does not currently consider it. Removing the assert wouldn't impact the CLI logic, but the lazy instance could misbehave if used. Also, the problem happens with tensorflow 2.10 but not with the latest version. Would be good to extend the implementation to consider this case. But I would do so only if I am able to write a unit test for it, and I haven't been able for the moment.
@mauvilsa Hi mauvilsa! Where can I find lazy_instance
useage example for CLI
, I can hardly find any infomation from documentation.
Well, there isn't much to illustrate about lazy_instance
. It just creates a class instance delaying the __init__
call until first use. The example here at the top is just as good as the single one in the docs. The only other thing to say is that implementing a wrapper that delays __init__
for any possible class is not trivial, so there might be cases in which it doesn't work.