Error when building from staticmethod
gabmis opened this issue ยท 8 comments
Hi @rsokl, thanks a lot for this great package.
I'm on hydra-zen 0.10.1 in a fresh python 3.11 virtualenv.
I'm getting an error when trying to instantiate from a staticmethod. Is that the expected behavior?
The following should reproduce the error.
from hydra_zen import builds, instantiate
class A:
@staticmethod
def method():
pass
if __name__ == "__main__":
conf = builds(A.method)
instantiate(conf)
A possible workaround is to add a reference to the staticmethod with the same name at the top level of the module containing the underlying class but it's not great. Plus, if the class comes from a third party then it's out of the question. Finally, you could wrap the staticmethod in your own code to be able to instantiate from it but that's not a great solution either.
from hydra_zen import builds, instantiate
class A:
@staticmethod
def method():
print(12)
if __name__ == "__main__":
method = A.method
conf = builds(method)
instantiate(conf)
Hi @gabmis thank you for the nice writeup and apologies for the delay.
I recall trying to get builds
to work on static methods about a year ago and found at the time that there is not enough information attached to the static method in order for builds
to set the _target_
attribute on the config. That being said, I think it is worth taking another pass at this.
Hi @gabmis, can you try upgrading to the latest release, I am able to run the above code with both staticmethod
and classmethod
. Here are the versions I'm using:
- hydra-zen, 0.11.0rc11
- python, 3.10.9
@jgbos I am not able to run the above code; I am on:
- Python 3.11.4
- hydra-zen 0.11.0rc11.post1.dev14+g3e087e3
Running:
from hydra_zen import builds, instantiate
class A:
@staticmethod
def method():
pass
conf = builds(A.method)
instantiate(conf)
produces:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
File [c:\Users\Ryan](file:///C:/Users/Ryan)\.conda\envs\py311\Lib\site-packages\hydra\_internal\utils.py:650, in _locate(path)
649 try:
--> 650 obj = import_module(mod)
651 continue
File [c:\Users\Ryan](file:///C:/Users/Ryan)\.conda\envs\py311\Lib\importlib\__init__.py:126, in import_module(name, package)
125 level += 1
--> 126 return _bootstrap._gcd_import(name[level:], package, level)
File :1204, in _gcd_import(name, package, level)
File :1176, in _find_and_load(name, import_)
File :1135, in _find_and_load_unlocked(name, import_)
ModuleNotFoundError: No module named '__main__.method'; '__main__' is not a package
Ok, I must have made a mistake running things last time. I get the same error, only works with classmethod
, sorry.