question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

[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:closed
  • Created 4 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
Jasha10commented, Feb 10, 2022

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 the main 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:

  • no support for command line completion
  • no automatic support for saving your config file to the outputs directory
  • no --multirun support

The @hydra.main decorator is designed specifically to support the above use-cases.

0reactions
alvitawacommented, Aug 18, 2022

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:

if __name__ == "__main__":
    register_configs()

    cfg, result = main()

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found