♻️ Store unset extra options as Undefined
and remove from needs.json
#1125
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.
As discussed in more detail at #1082 (comment),
at present every stored need (dict) item in a project is required to have every key present.
In particular this is required so that filter strings (which are evaluated as Python code) can be evaluated for all needs; for example
"xxx == 1 "
would raise aNameError
ifxxx
was not a key of the need.As discussed also in #1122 (comment),
this has meant that currently all fields added to
NEEDS_CONFIG.extra_options
, are added to every need item, with a default of""
.Storing as a
""
makes it difficult to tell if this was specifically set by the user or not, and consequently whether it can be removed from theneeds.json
By contrast, jinja2 has the concept of https://jinja.palletsprojects.com/en/3.0.x/api/#undefined-types, by default assigned to any variable not provided as part of the context.
This PR changes the default to an
Undefined
instance and, when writing to aneeds.json
, removes and keys with this value.As can be seen from the tests, for a standard need item, this removes 18 fields.
This solution should be "minmally breaking", in that mainly
Undefined
acts the same as""
.Although there may some rare exceptions, for example, the
calc_sum
filter function expectedfloat(value)
to raise aValueError
, but it now raises anUndefinedError
.