Skip to content
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

🔧 Add better typing for global_options config variable #1120

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions sphinx_needs/api/need.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
NeedsTagNotAllowed,
NeedsTemplateException,
)
from sphinx_needs.config import NeedsSphinxConfig
from sphinx_needs.config import GlobalOptionsType, NeedsSphinxConfig
from sphinx_needs.data import NeedsInfoType, SphinxNeedsData
from sphinx_needs.directives.needuml import Needuml, NeedumlException
from sphinx_needs.filter_common import filter_single_need
Expand Down Expand Up @@ -795,7 +795,7 @@ def _merge_extra_options(


def _merge_global_options(
app: Sphinx, needs_info: NeedsInfoType, global_options: dict[str, Any]
app: Sphinx, needs_info: NeedsInfoType, global_options: GlobalOptionsType
) -> None:
"""Add all global defined options to needs_info"""
if global_options is None:
Expand All @@ -815,7 +815,9 @@ def _merge_global_options(
continue

for single_value in values:
# TODO should first match break loop?
if len(single_value) < 2 or len(single_value) > 3:
# TODO this should be validated earlier at the "config" level
raise NeedsInvalidException(
f"global option tuple has wrong amount of parameters: {key}"
)
Expand Down
19 changes: 17 additions & 2 deletions sphinx_needs/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from dataclasses import MISSING, dataclass, field, fields
from typing import TYPE_CHECKING, Any, Callable, Literal, TypedDict
from typing import TYPE_CHECKING, Any, Callable, Dict, Literal, TypedDict

from sphinx.application import Sphinx
from sphinx.config import Config as _SphinxConfig
Expand Down Expand Up @@ -101,6 +101,20 @@ class ExternalSource(TypedDict, total=False):
"""Added as the `external_css` field for each need item (optional)"""


GlobalOptionsType = Dict[str, Any]
"""Default values given to specified fields of needs

Values can be:

- a tuple: ``(value, filter_string)``, where the default is only applied if the filter_string is fulfilled
- a tuple: ``(value, filter_string, alternative default)``,
where the default is applied if the filter_string is fulfilled,
otherwise the alternative default is used
- a list of the tuples above
- otherwise, always set as the given value
"""


@dataclass
class NeedsSphinxConfig:
"""A wrapper around the Sphinx configuration,
Expand Down Expand Up @@ -236,9 +250,10 @@ def __setattr__(self, name: str, value: Any) -> None:
functions: list[Callable[..., Any]] = field(
default_factory=list, metadata={"rebuild": "html", "types": (list,)}
)
global_options: dict[str, Any] = field(
global_options: GlobalOptionsType = field(
default_factory=dict, metadata={"rebuild": "html", "types": (dict,)}
)
"""Default values given to specified fields of needs"""
duration_option: str = field(
default="duration", metadata={"rebuild": "html", "types": (str,)}
)
Expand Down
Loading