748: handle obj.get(k, default) call pattern for backward compatibility #755
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #748
Why is this the best possible solution? Were any other approaches considered?
obj.get(key, default)
with SurveyElement if neededAlternatives
The original code in #748 meant that
elem.get("foo")
would return None. This seemed seemed like it would be confusing for pyxform maintenance since a) there is still someelem.get("foo")
code, and b) its easier to only be concerned about AttributeErrors (as would be expected for a non-dict class) rather than a mix of None, KeyError, AttributeError. If calling code likeelem.get("foo")
expects None then it can be adapted to use an explicitNone
default e.g.elem.get("foo", None)
- otherwise it will raise an AttributeError as before. TheSurveyElement
also differs fromdict
inelem["foo"]
(AttributeError not KeyError, respectively) so it didn't seem appropriate to fully replicatedict
behaviour in this custom Mapping class.Other alternatives mentioned in #748 and the warning message, for external libs to implement:
What are the regression risks?
There are no internal calls like
obj.get(key, default)
. External code should work as before, except for the DeprecationWarning which can be ignored as shown in the tests, i.e. add:Does this change require updates to documentation? If so, please file an issue here and include the link below.
No.
Before submitting this PR, please make sure you have:
tests
python -m unittest
and verified all tests passruff format pyxform tests
andruff check pyxform tests
to lint code