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

Error assert when use lazy_instance #515

Open
pjgao opened this issue Jun 5, 2024 · 4 comments
Open

Error assert when use lazy_instance #515

pjgao opened this issue Jun 5, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@pjgao
Copy link

pjgao commented Jun 5, 2024

🐛 Bug report

when use lazy_instance as default value, there is a assert cannot satisfy

assert name not in self.__dict__

To reproduce

  1. 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)
  1. 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
@pjgao pjgao added the bug Something isn't working label Jun 5, 2024
@mauvilsa
Copy link
Member

mauvilsa commented Jun 9, 2024

Thank you for reporting! I will look at it.

@mauvilsa
Copy link
Member

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.

@pjgao
Copy link
Author

pjgao commented Jul 31, 2024

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.

@mauvilsa
Copy link
Member

mauvilsa commented Aug 2, 2024

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.

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

No branches or pull requests

2 participants