sphinx-needs icon indicating copy to clipboard operation
sphinx-needs copied to clipboard

needs object in filter is not a dictionary as specified in docu

Open maxlutz98 opened this issue 2 years ago • 4 comments

In the documentation to filter string it is specified, that the variable is available as a dictionary. So I tried to use it like a dictionary resulting in the following error

WARNING: Filter any([True for need_id in links_satisfies_back if needs[need_id]["type"] == "req-sw" and security in needs[need_id] and needs[need_id]["security"] == "TRUE"]) != 0 and type == "req-sys" and security == "Yes" and 'example' not in tags not valid. Error: list indices must be integers or slices, not str.

It seems that needs is a list of need objects and not a dictionary. This would match with the example given for filter code in the docu. Here a dictionary is created based on the list by iterating through:

# Lets create a needs_dict to address needs by ids more easily.
needs_dict = {x['id']: x for x in needs}

Versions:

Sphinx==5.3.0
sphinx-needs==1.1.0

Will give it another try with the newest version.

Is one of the occurences in the documentation wrong? Is needs really a list?

Btw I changed the filter string according to the documentation of filter code and the warning is resolved.

any([True for need_id in links_satisfies for need in needs if need["id"] == need_id and need["type"] == "req-sys" and security in need and need["security"] == "Yes" else False]) and type == "req-sw" and security == "TRUE" and 'example' not in tags

maxlutz98 avatar Feb 02 '23 07:02 maxlutz98

You are right, that's a bug in the docs and for the filter-function ,it is really a list, not a dictionary.

danwos avatar Feb 02 '23 08:02 danwos

Wouldn't a dictionary be better at least from user perspective? Or is this from sphinx-needs perspective bad or more resource consuming?

maxlutz98 avatar Feb 02 '23 10:02 maxlutz98

No, I agree, it would be better. However this change would break a lot of existing filters, so I don't want to touch the needs variable.

But I'm open to somehow add a needs_dict.

But what are exactly the use-cases to have a need-dict available in the filter-string? I would provide such complex filters as a filter-function, which is much more efficient and often easier to maintain and to understand.

Remember, filter-strings gets executed once per need-object, which may make loops or lookups quite expensive. A filter-func gets executed only once overall, so a loop may be also executed only once.

danwos avatar Feb 02 '23 10:02 danwos

It is used in a :need_count: role to generate some metrics (amount, ratio). In this case how many of the security relevant software requirements are linked to security relevant system requirements or the other way round.

For that I need to check values of the software need and the linked system need. From the links/links_back attribute, I only get the ID of the related need but not the object itself. With a dictionary, you can get the related need object by the id as a key, with a list I need to iterate over it.

But your concerns regarding the efficiency are valid.

Based on the documentation filter-function is only applicable for needlist, needtable or needflow, so not completely fitting to show an amount and/or a ratio.

maxlutz98 avatar Feb 02 '23 12:02 maxlutz98