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

Providing Config.__getitem__() is error prone #567

Open
tartley opened this issue Sep 2, 2021 · 4 comments
Open

Providing Config.__getitem__() is error prone #567

tartley opened this issue Sep 2, 2021 · 4 comments

Comments

@tartley
Copy link

tartley commented Sep 2, 2021

In more than one of the services which use talisker, there have been multiple recurring or longstanding bugs caused by callers which should have used:

talisker.get_config()['DEVEL']
# or
talisker.get_config().devel

but instead used:

talisker.get_config()['devel']

This formulation doesn't work, it just returns None.

See definition of getitem() at

def __getitem__(self, name):

The above makes me wonder whether offering __getitem__ on the Config object itself is wise, or does it just potentially confuse callers? Perhaps it would be nice to offer it as a method on Config, like .raw(key), so it's more explicit whether callers should be passing nice property names like 'devel', or raw config keys like 'DEVEL'.

Or maybe, offering indexing right on Config() objects is fine, and the error prone part is actually that we fall back to returning None for unrecognized keys. Perhaps we should raise an index error instead?

Of course these changes would potentially be breaking, so would require care to introduce, but perhaps they would save future bugs like the ones we've already had.

@maxiberta
Copy link
Contributor

I think raising a KeyError on a missing key makes sense and is the expected behaviour from a dict-like object, while keeping full backwards compatibility.

@tartley
Copy link
Author

tartley commented Sep 3, 2021

@maxiberta Thanks. Yes, that wouldn't be an incompatible API change, but obviously it would still need deploying with care - there may be code in services which is currently passing an invalid key, getting None back and happily carrying on. After the change it will KeyError.

@maxiberta
Copy link
Contributor

Having services visibly blow up is good actually, and much better than subtly returning unexpected values.

@tartley
Copy link
Author

tartley commented Sep 3, 2021

Yes, absolutely agreed, that's why I suggest it! I'm only saying that when we deploy services using this new version of talisker, we should take care that we're not deploying something that would blow up immediately, is all (which, operationally, would be the equivalent of breaking a critical API compatibility)

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

No branches or pull requests

2 participants