[Bug] Unable to return any value from main function
See original GitHub issueš Bug
Once marked with @hydra.main, the function always returns None.
To reproduce
** Minimal Code/Config snippet to reproduce **
@hydra.main(config_path='config.yaml')
def main(cfg):
return 1
if __name__ == '__main__':
always_none = main()
print(always_none) # always print None
Expected Behavior
Expects value to be returned like the usual functions do.
System information
-
Hydra 0.11.2
-
Python 3.6.6
-
Ubuntu 18.04
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (7 by maintainers)
Top Results From Across the Web
Function should return a value in function main - CodeProject
main needs to return an integer value: it should look like this: ... The zero means "no errors found" - any non-zero value...
Read more >"warning: no return statement in function returning non-void ...
Section C.1.4 says: Change: It is now invalid to return (explicitly or implicitly) from a function which is declared to return a value...
Read more >Is It Necessary to Return a Value in Main()? - Stack Overflow
The code that calls main() expects it to return int (or call exit()). You cannot change the codeĀ ...
Read more >Closed (won't fix) - Drupal
Fatal error: Can't use function return value in write context in .../sites/all/modules/menu_target/menu_target.admin.inc on line 19.
Read more >Why don't I receive a compile error when I don't return a value ...
In C++ it's not an error to fail to return a value from a value-returning function; it's just "undefined behaviour". The compiler is...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @fabrizioschiano,
The
@hydra.main
decorator is designed to wrap around your programās main entrypoint. Think of this main entrypoint as being similar to themain
function from C/C++/Java.You should checkout the compose API. This functional API will allow you to get a config from Hydra and do whatever you want with it.
The drawbacks of using the
compose
API (instead of the@hydra.main
decorator) are:outputs
directory--multirun
supportThe
@hydra.main
decorator is designed specifically to support the above use-cases.I understand that it is a best practice to do everything inside the hydra main function, but nevertheless there are situations where it is useful to be able to simply return something. For example in my app I have all the logic inside the main function, but I want the result of the main function to be available in the repl after running the script with
python -i
. This is done with:This way I can play around with the result of the script to check if it is correct or save it if it is good.
However, @hydra.main not returning a function forces me to do this through global variables which is messy. And letās not forget the time it has cost me to figure out why my main function was always returning None.
I think it is preferable if libraries donāt force the user to use them in a particular way but instead allow the user to use the library in the way that is most suitable for his/her application.
I realize Iām being a bit philosophical here but another area in which this manifests is with the custom tracebacks that are provided. It is fortunate that you provide a flag to disable them (āexport HYDRA_FULL_ERROR=1ā) but personally I wonder why a library meant for application configuration also hooks into your error tracebacks (and in the meantime prevents you from using post mortem debugging unless you disable the custom tracebacks).
Anyways, I just wanted to give my experience with hydra with regards to this.