Skip to content

♻️ Store needs as NeedItem / NeedPartItem, rather than standard dict #1485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 7, 2025

Conversation

chrisjsewell
Copy link
Member

@chrisjsewell chrisjsewell commented Aug 6, 2025

Currently, need data items are stored as standard Python dicts, which is also exposed to the user in various APIs and as the needs.json.
This is problematic as it does not allow for

  1. any internal checks to ensure data is stored/updated consistently (e.g. fields like id should never be changed),
  2. any data to be "hid" from the user, or presented in a different way to its internal structure.
    • because the dict is a flat key/val store, it does not allow for distinction between internal / user set extra/link fields
    • we would like to improve how values that are dynamic/variant functions are stored (prior to evaluation)
    • we would also like to store source mapping data from distinct sources (directives, external, imports) in a more coherent way.
  3. deprecations of any fields in a non-breaking manner

In this PR, we instead add a specific NeedItem class, which closely mimics adict, but also allows for additional constraints and functionality.

We also introduce a NeedPartItem which is distinctly different from a NeedItem, e.g. it should not be mutated.

To ensure that no existing user code is accidentally breaking this, e.g. by directly add dictionaries to the list of needs,
we perform a check, at the end of the post-processing stage, to make sure all need items are instances of NeedItem
(and point them towards the "correct" add_need API)


‼️ Breaking

This would be breaking for any users doing "non-API" modifications / additions to the needs data, i.e. directly adding dict items.
It should not change interactions with standard APIs like add_need or filter strings, etc

@chrisjsewell chrisjsewell marked this pull request as draft August 6, 2025 08:55
@chrisjsewell chrisjsewell changed the title ♻️ Store needs as NeedItem, rather than standard dict ♻️ Store needs as NeedItem / NeedPartItem, rather than standard dict Aug 6, 2025
Copy link

codecov bot commented Aug 6, 2025

Codecov Report

❌ Patch coverage is 89.86175% with 22 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.74%. Comparing base (4e10030) to head (d30b46f).
⚠️ Report is 145 commits behind head on master.

Files with missing lines Patch % Lines
sphinx_needs/need_item.py 80.21% 18 Missing ⚠️
sphinx_needs/directives/need.py 87.50% 1 Missing ⚠️
sphinx_needs/functions/common.py 83.33% 1 Missing ⚠️
sphinx_needs/layout.py 87.50% 1 Missing ⚠️
sphinx_needs/views.py 92.30% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1485      +/-   ##
==========================================
+ Coverage   86.87%   88.74%   +1.86%     
==========================================
  Files          56       68      +12     
  Lines        6532     8473    +1941     
==========================================
+ Hits         5675     7519    +1844     
- Misses        857      954      +97     
Flag Coverage Δ
pytests 88.74% <89.86%> (+1.86%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@chrisjsewell chrisjsewell requested a review from ubmarco August 6, 2025 13:46
@chrisjsewell chrisjsewell marked this pull request as ready for review August 6, 2025 15:09
@chrisjsewell chrisjsewell changed the title ♻️ Store needs as NeedItem / NeedPartItem, rather than standard dict ♻️ Store needs as NeedItem / NeedPartItem, rather than standard dict Aug 6, 2025
Currently, need data items are stored as standard Python dicts,
which is also exposed to the user in various APIs
This is problematic as it does not allow for (a) any internal checks to ensure data is updated consistently (e.g. fields like `id` should never be changed), (b) it does not allow any data to be "hid" from the user, or presented in a different way to its internal structure.

In this PR, we instead add a specific `NeedItem` class, which closely mimics a dictionary, but also allows for additional functionality.

We also introduce a `NeedPartItem` which is distinctly different from a `NeedItem`, e.g. it should not be mutated.
@chrisjsewell chrisjsewell force-pushed the need-item-custom-dict2 branch from f73cfec to f3a92e8 Compare August 7, 2025 05:45
Copy link
Member

@ubmarco ubmarco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me and is a required step to get people to use the API or complain if something is missing. We should inform core users about this change.
I just have 2 comments, please decide whether those are issues right now or not.

self,
key: Literal[
"id",
"id_parent",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id_parent should be optional, it's about need part parent - would it break the data model to change it to str | None ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This particular PR is not intended to change any schema specifics; e.g. it shows that this change does not change any output needs.json
So it may well be a legitimate change, but should not be made here

"external_css",
"constraints_error",
"section_name",
"parent_need",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, parent_need is optional, it's about need nesting. Wouldn't str | None be better?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

@chrisjsewell chrisjsewell merged commit d2bd68f into master Aug 7, 2025
20 checks passed
@chrisjsewell chrisjsewell deleted the need-item-custom-dict2 branch August 7, 2025 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants