[ENH] respect explicit default in get_tag to match getattr semantics
Reference Issues/PRs
Fixes #291
What does this implement/fix? Explain your changes.
This PR modifies get_tag (and the internal _get_flag) to respect an explicitly provided tag_value_default. Currently, get_tag raises a ValueError if a tag is missing even if a default value is supplied, unless raise_error=False is also explicitly passed. This behavior is unintuitive, and inconsistent with get_class_tag and standard patterns like getattr() or dict.get()
Changes:
- Introduced a
_DEFAULT_SENTINELin_tagmanager.pyto reliably distinguish between an omitted default and an explicit None default. - Updated
_get_flagto bypass theraise_errorcheck if a default was provided. - Updated public method signatures and docstrings in
_base.pyand_tagmanager.pyto clarify the new behavior. - Added unit tests in
test_base.pyto verify that explicit defaults (including None) correctly suppress the ValueError without needing raise_error=False.
Backward Compatibility:
The change is fully backward compatible. Calling get_tag("missing") without a second argument will still raise a ValueError by default, preserving current error-checking behavior for typos.
Does your contribution introduce a new dependency? If yes, which one?
No
What should a reviewer concentrate their feedback on?
- The use of the
_DEFAULT_SENTINELobject to ensureget_tag("name", None)correctly returns None instead of raising an error. - The alignment of the
get_taglogic with the existing behavior ofget_class_tag.
Any other comments?
As discussed in the issue thread, a follow-up PR could be opened later to start a FutureWarning deprecation cycle for changing the global default of raise_error from True to False, but this PR focuses on respecting the user-supplied default first.
PR checklist
For all contributions
- [x] I've reviewed the project documentation on contributing
- [x] I've added myself to the list of contributors.
- [x] The PR title starts with either [ENH], [CI/CD], [MNT], [DOC], or [BUG] indicating whether the PR topic is related to enhancement, CI/CD, maintenance, documentation, or a bug.
For code contributions
- [x] Unit tests have been added covering code functionality
- [x] Appropriate docstrings have been added (see documentation standards)
- [ ] New public functionality has been added to the API Reference
Hi @fkiraly
The motivation was from the thread in Issue #291.
get_tag("name","fallback") raises a ValueError even though the caller explicitly provided a fallback.
This defeats the purpose of tag_value_default unless the user also knows to pass raise_error=False. This deviates from standard Python patterns (getattr, dict.get) and is
inconsistent with get_class_tag, which already returns the supplied default unconditionally.
The PR does change behavior for the case where a caller passes tag_value_default but leaves raise_error as its default True.
While this call pattern is contradictory (saying "use this fallback if missing" and "raise an error if missing" simultaneously), but it shouldn't silently change.
As you suggested in the original issue thread, a deprecation cycle would be a place to start.
Would you like me to update this PR so that passing a default while raise_error=True emits a FutureWarning (warning of the future getattr-like behavior) but still raises the ValueError for now?
Hm, having thought about this: I think we should not change the default behaviour of get_tag.
The reason for this is that:
- per design expectation, default tag values should be set in a common super class, and not in the
get_tagmethod. - more generally, default values for tag should be managed globally (inside a package) and not locally (at call)
- in most downstream uses which do follow this design, it feels preferable to have a failure if the tag is not actually present, but requested
I will add these thoughts to the original issue.