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

Accurate error message for missing configuration #670

Merged
merged 8 commits into from
Mar 15, 2023

Conversation

emanuel-schmid
Copy link
Collaborator

@emanuel-schmid emanuel-schmid commented Mar 3, 2023

Changes proposed in this PR:

  • When ever a config value is missing in (beneath) climada.CONFIG an error message is provided that is clear about it and suggests a list of files that could be changed to solve the problem.

This PR fixes (to some extent) #665

PR Author Checklist

PR Reviewer Checklist

climada/util/config.py Outdated Show resolved Hide resolved
@chahank chahank removed the request for review from leonie-villiger March 8, 2023 08:01
Comment on lines +59 to +62
conf_files = [Path(_find_in_parents(conf_dir, CONFIG_NAME))
if _find_in_parents(conf_dir, CONFIG_NAME)
else conf_dir / CONFIG_NAME
for conf_dir in CONFIG_DIRS[::-1]]
Copy link
Member

@chahank chahank Mar 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a curiosity: why define the variablesCONFIG_NAME and CONFIG_DIRS at the very end of the file?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because CONFIG kind of has to be at the bottom of the file and both CONFIG_NAME and CONFIG_DIRS have one purpose: to define CONFIG. So I want them to be together, then I don't have to wonder or scroll.

except AttributeError:
conf_files = [Path(_find_in_parents(conf_dir, CONFIG_NAME))
if _find_in_parents(conf_dir, CONFIG_NAME)
else conf_dir / CONFIG_NAME
Copy link
Member

@chahank chahank Mar 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
else conf_dir / CONFIG_NAME
else Path(conf_dir / CONFIG_NAME)

Would this not be more consistent? It is not clear that _find_in_parents would return a string instead of a Path.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷 conf_dir is a path though and even with the proposed change it must be one.

def test_missing(self):
with self.assertRaises(AttributeError) as ve:
CONFIG.hazard.fire_fly.population.str()
self.assertIn("there is no 'fire_fly' configured for 'hazard'", str(ve.exception))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does one get for a message for CONFIG.random_module and for CONFIG.hazard.tropical_cyclone.some_file? Should this also be tested maybe?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This:

  • "there is no 'random_module' configured for 'climada.CONFIG' ..."
  • "there is no 'some_file' configured for 'tropical_cyclone' ..."

The latter should not be tested as there is nothing such a test could contribute. The former possibly to some extent as the 'climada.CONFIG' is something special. I wouldn't but if you if you think it's worth it I can add it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the given that there is nothing currently, but it would be possible (and in particular a user can add it right?) I would suggest adding it since it is very little extra work.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 sure.
Just for understanding - what do you mean by

Well, the given that there is nothing currently, but it would be possible (and in particular a user can add it right?)

?

@chahank
Copy link
Member

chahank commented Mar 12, 2023

Looks good to me, thanks for the update! A few optional minor comments.

@aleeciu
Copy link
Collaborator

aleeciu commented Mar 15, 2023

Thanks @emanuel-schmid, it looks good. Fun fact: I tried to reproduce the error on #665 from main to compare the output with the one I'd get in this branch, but I couldn't 😵 .. tests worked in main even when run from different folders ...

@emanuel-schmid
Copy link
Collaborator Author

What was the config value you were looking for? Perhaps it got established in the default config file (climada/conf/climada.conf) meanwhile?
I'll merge this nevertheless though...

@emanuel-schmid emanuel-schmid merged commit 1ebd5b8 into develop Mar 15, 2023
@emanuel-schmid emanuel-schmid deleted the feature/missing_config_hints branch March 15, 2023 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants