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

allow to directly import constants from easybuild.framework.easyconfig.constants #4144

Merged
merged 2 commits into from
Feb 15, 2023

Conversation

Flamefire
Copy link
Contributor

This allows e.g. from easybuild.framework.easyconfig.constants import SYSTEM instead of easyconfig.constants.EASYCONFIG_CONSTANTS['SYSTEM'][0]

This allows e.g. `from easybuild.framework.easyconfig.constants import SYSTEM`
instead of `easyconfig.constants.EASYCONFIG_CONSTANTS['SYSTEM'][0]`
@Flamefire
Copy link
Contributor Author

Failures in test_det_copy_ec_specs due to rate limit. Maybe we should not run such tests when there is no token?

@boegel
Copy link
Member

boegel commented Dec 21, 2022

Failures in test_det_copy_ec_specs due to rate limit. Maybe we should not run such tests when there is no token?

Good point, done in #4149


# Add EasyConfig constants to export list
globals().update({name: value for name, (value, _) in EASYCONFIG_CONSTANTS.items()})
__all__ = ['EXTERNAL_MODULE_MARKER', 'EASYCONFIG_CONSTANTS'] + list(EASYCONFIG_CONSTANTS.keys())
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, does __all__.extend(list(EASYCONFIG_CONSTANTS.keys()) work too?

Just to make sure we don't overlooked adding stuff to all if additional (real) constants are added to constants.py

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tested that and no. __all__ has a default value if it is not set (only), so at that point it is an undefined variable.

It actually isn't really required because (almost) nobody does from easyconfig.constants import * which is the only use case of __all__, everything else works without it.

"""Test importing constants works"""
# Sanity check that importing an EC constant works as-if using EASYCONFIG_CONSTANTS
from easybuild.framework.easyconfig.constants import SYSTEM
self.assertEqual(SYSTEM, easyconfig.constants.EASYCONFIG_CONSTANTS['SYSTEM'][0])
Copy link
Member

Choose a reason for hiding this comment

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

For SYSTEM, I would make this a hard check, rather than relying on EASYCONFIG_CONSTANTS:

self.assertEqual(SYSTEM, {'name': 'system', 'version': 'system'})

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But that is not what is tested here. Only importing the constants should be tested here and that means that doing such an explicit import is the same as accessing the EASYCONFIG_CONSTANTS list, see the comment.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not saying you should not do the explicit import, you should.

I'm saying you should compare the value of SYSTEM directly to a hardcoded dict here, rather than relying on EASYCONFIG_CONSTANTS, otherwise the tests would still pass when the value for SYSTEM in EASYCONFIG_CONSTANTS in changed in the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

otherwise the tests would still pass when the value for SYSTEM in EASYCONFIG_CONSTANTS in changed in the code.

Erm, This is exactly what you want, don't you?

  • The value of SYSTEM in ECs is that of EASYCONFIG_CONSTANTS
  • Previously you would need to use easyconfig.constants.EASYCONFIG_CONSTANTS['SYSTEM'][0] in hooks etc.
  • With this PR you can instead use from easybuild.framework.easyconfig.constants import SYSTEM to the same effect
  • Hence the test checks that the imported value matches the value in EASYCONFIG_CONSTANTS to verify it is indeed the same effect and has a matching comment

So I don't understand why the test shouldn't pass when easybuild.framework.easyconfig.constants.SYSTEM == easyconfig.constants.EASYCONFIG_CONSTANTS['SYSTEM'][0]. Could you explain your thought here?

Additionally I really dislike using hardcoded values as this results in unrelated tests failing. This was e.g. the case where a spelling fix to a doc caused another test to fail: #3219 -> #4158 So if some previously run other test for any reason changed the EASYCONFIG_CONSTANTS (which shouldn't happen, but I guess is what you had in mind) this test still passing is IMO actually a good thing.

Copy link
Member

Choose a reason for hiding this comment

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

I consider a change to the value of a constant a breaking change, so tests should fail because of it.

Although no code should be relying on the value of the constant, it's not unlikely that some stuff does, and hence the value of the constant should never be changed.
Hardcoding the expected value in the tests is a way of catching that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I consider a change to the value of a constant a breaking change, so tests should fail because of it.

Ok understood. But I wouldn't do that here as that is not the purpose of the test: "importing an EC constant works as-if using EASYCONFIG_CONSTANTS".

I split the test into 2 where the 2nd ensures exactly that and the first is more like what a user would do and uses the hard-coded value. I'm still not a fan of doing that for the mentioned reason and would rather have dedicated tests to catch constant changes that shouldn't change or just say that the purpose of the constant is that the value does not matter so anything relying on it is a bug anyway.

@boegel boegel added this to the 4.x milestone Dec 21, 2022
@Flamefire
Copy link
Contributor Author

Can we get this into the next release? It only adds stuff, so nothing should be at risk breaking and it's IMO a nice addition especially for hook-authors.

@easybuilders easybuilders deleted a comment from boegelbot Jan 18, 2023
@boegel boegel modified the milestones: 4.x, next release (4.7.1?) Jan 18, 2023
@boegel
Copy link
Member

boegel commented Jan 18, 2023

Can we get this into the next release? It only adds stuff, so nothing should be at risk breaking and it's IMO a nice addition especially for hook-authors.

We missed the window for EasyBuild v4.7.0 (which got delayed significantly already, so I wasn't keen on picking up extra stuff that was not entirely ready to merge yet), but getting this in for EasyBuild v4.7.1 should definitely work...

Just take a look at the suggestion for the tests, and then it's good to go imho.

@boegel boegel changed the title Allow to directly import EasyConfig constants from the module allow to directly import constants from easybuild.framework.easyconfig.constants Feb 15, 2023
Copy link
Member

@boegel boegel left a comment

Choose a reason for hiding this comment

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

lgtm

@boegel boegel merged commit 89fc160 into easybuilders:develop Feb 15, 2023
@Flamefire Flamefire deleted the ec-constants branch February 15, 2023 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants