sphinx-needs
sphinx-needs copied to clipboard
Internal handling of `extra_options` seems inconsistent
This issue arises from when I was looking into the code considering #1082 and #1119 (@danwos as we discussed this morning)
The use of the config variable needs_extra_options and of the global variable NEEDS_CONFIG.extra_options seem inconsistent, in that:
- Sometimes
needs_extra_optionsis used, where it seems thatNEEDS_CONFIG.extra_optionsshould probably be used instead NEEDS_CONFIG.extra_optionsis difficult to track, because at different stages it has new items added, which are obviously not taken into account in previous stages.
Below is a summary of their use in the code base:
Extra options are created by either:
- A user adds them via the
needs_extra_optionsconfiguration, as alist[str] - A user / extension uses
api.configuration.add_extra_optionto "programmatically" add anametoNEEDS_CONFIG.extra_options(raisesNeedsApiConfigWarningif already added). For examplesphinx-test-reportsuses this in aconfig-initedevent
They are used in the following places (in this order):
-
In
load_config(config-initedevent)needs_extra_optionsare added toNEEDS_CONFIG.extra_optionsobject (a warning is raised if one already exists)needs_duration_option(defaultduration) +needs_completion_option(defaultcompletion) are also added if not present (as of #1127)- each
NEEDS_CONFIG.extra_optionsis added as an option on:NeedDirectiveNeedserviceDirectiveNeedextendDirective(and also as+<name> -> unchangedand-<name> -> flag)
-
In
check_configuration(config-initedevent):needs_filter_datakeys are checked againstneeds_extra_optionsnames; matching keys cause aNeedsConfigExceptionneeds_extra_optionsnames are checked againstINTERNALSlist; matching keys cause aNeedsConfigException- link type names (and their corresponding
<name>_backkey) are checked againstneeds_extra_optionsnames; matching keys cause aNeedsConfigException - ensure all
needs_variant_optionskeys match aneeds_extra_optionsname, or a link type, or a default option; if not, aNeedsConfigExceptionis raised
-
In
prepare_env(env-before-read-docsevent):-
the
ServiceManageris initialised and theGithubServiceandOpenNeedsServiceclasses are registered viaServiceManager.register. InServiceManager.register, for alloptionson the service class, the option is added toNEEDS_CONFIG.extra_options(if not already present) -
additional "default" extra names are added to
NEEDS_CONFIG.extra_options: ~~hidden~~ (removed in #1124), ~~duration,completion~~ (removed #1127), ~~constraints~~ (removed in #1123) (if not already present)
-
-
In
load_external_needs(env-before-read-docsevent) for each loaded need, all fields inneeds_extra_optionsare always kept (this data is then parsed on toadd_external_need -> add_need) -
In
NeedDirective.run(called during read) for all names inNEEDS_CONFIG.extra_optionsgather passkwargstoadd_need, i.e.kwarks[name] = self.options.get(name, "")- It is of note that
durationandcompletionare specifically gathered, but then surely must be overridden, since these are both added as defaults above (inprepare_env)
- It is of note that
-
In
NeedimportDirective.run,NEEDS_CONFIG.extra_optionsare added to theknown_optionslist then, for each import need item, if a field is not inknown_optionsit is dropped, the rest are passed toadd_need- This is similar to the logic in
load_external_needs, BUT that usesneeds_extra_optionsinstead ofNEEDS_CONFIG.extra_options
- This is similar to the logic in
-
In
add_need(called before/during read)NEEDS_CONFIG.extra_optionsis passed to_merge_extra_options, as well as thekwargspassed toadd_need:-
All
kwargspassed toadd_needmust either be inNEEDS_CONFIG.extra_optionsor inneeds_extra_links(or aNeedsInvalidOptionis raised) -
In
_merge_extra_options, for all theNEEDS_CONFIG.extra_optionskeys:- if the key is in
kwargs, but not already set byadd_need(i.e. is not an internal key), its value is set tokwargs[key], or - if the key is not already set by
add_need(i.e. is not an internal key), it is set to a default value of""
- if the key is in
-
-
In
NeedReportDirective, if required,need_extra_optionsare listed
One thing else I note @danwos:
I see now that the duration and completion fields are added for use by the needgantt directive.
For this there are also needs_duration_option and needs_completion_option configuration options, to change the name of these fiels.
However, since duration and completion are hard-coded in the code base in a number of places,
I think if users were to use these configurations options to change the names, this would not work very well