From 664f8bc5dc760aed26194e42976f3cf137c89295 Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Sat, 9 Sep 2023 04:01:02 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Remove=20`unwrap`=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This run-time null check is effectively now replaced by the static type-checking. --- sphinx_needs/api/need.py | 4 ++-- sphinx_needs/builder.py | 13 ++++++------- sphinx_needs/diagrams_common.py | 4 ++-- sphinx_needs/directives/need.py | 11 ++++------- sphinx_needs/directives/needbar.py | 5 ++--- sphinx_needs/directives/needextend.py | 5 ++--- sphinx_needs/directives/needextract.py | 4 ++-- sphinx_needs/directives/needfilter.py | 6 +++--- sphinx_needs/directives/needflow.py | 4 ++-- sphinx_needs/directives/needgantt.py | 2 +- sphinx_needs/directives/needlist.py | 6 +++--- sphinx_needs/directives/needpie.py | 4 +--- sphinx_needs/directives/needsequence.py | 5 ++--- sphinx_needs/directives/needservice.py | 5 +++-- sphinx_needs/directives/needtable.py | 10 ++-------- sphinx_needs/directives/needuml.py | 2 +- sphinx_needs/environment.py | 12 ++++++------ sphinx_needs/layout.py | 12 ++++++------ sphinx_needs/roles/need_count.py | 4 +--- sphinx_needs/roles/need_func.py | 4 +--- sphinx_needs/roles/need_incoming.py | 6 +++--- sphinx_needs/roles/need_outgoing.py | 6 +++--- sphinx_needs/roles/need_part.py | 5 ++--- sphinx_needs/roles/need_ref.py | 6 +++--- sphinx_needs/utils.py | 18 +++++------------- sphinx_needs/warnings.py | 3 +-- tests/test_needs_id_builder.py | 7 ++----- 27 files changed, 71 insertions(+), 102 deletions(-) diff --git a/sphinx_needs/api/need.py b/sphinx_needs/api/need.py index caf269cd9..f3415cc5b 100644 --- a/sphinx_needs/api/need.py +++ b/sphinx_needs/api/need.py @@ -28,7 +28,7 @@ from sphinx_needs.logging import get_logger from sphinx_needs.nodes import Need from sphinx_needs.roles.need_part import find_parts, update_need_with_parts -from sphinx_needs.utils import jinja_parse, unwrap +from sphinx_needs.utils import jinja_parse logger = get_logger(__name__) @@ -512,7 +512,7 @@ def del_need(app: Sphinx, need_id: str) -> None: :param app: Sphinx application object. :param need_id: Sphinx need id. """ - env = unwrap(app.env) + env = app.env needs = SphinxNeedsData(env).get_or_create_needs() if need_id in needs: del needs[need_id] diff --git a/sphinx_needs/builder.py b/sphinx_needs/builder.py index 1fc3255b1..56bed77cc 100644 --- a/sphinx_needs/builder.py +++ b/sphinx_needs/builder.py @@ -10,7 +10,6 @@ from sphinx_needs.data import NeedsInfoType, SphinxNeedsData from sphinx_needs.logging import get_logger from sphinx_needs.needsfile import NeedsList -from sphinx_needs.utils import unwrap log = get_logger(__name__) @@ -25,7 +24,7 @@ def write_doc(self, docname: str, doctree: nodes.document) -> None: pass def finish(self) -> None: - env = unwrap(self.env) + env = self.env data = SphinxNeedsData(env) filters = data.get_or_create_filters() version = getattr(env.config, "version", "unset") @@ -82,7 +81,7 @@ def get_target_uri(self, _docname: str, _typ: Optional[str] = None) -> str: def build_needs_json(app: Sphinx, _exception: Exception) -> None: - env = unwrap(app.env) + env = app.env if not NeedsSphinxConfig(env.config).build_json: return @@ -107,7 +106,7 @@ def write_doc(self, docname: str, doctree: nodes.document) -> None: pass def finish(self) -> None: - env = unwrap(self.env) + env = self.env needumls = SphinxNeedsData(env).get_or_create_umls().values() for needuml in needumls: @@ -140,7 +139,7 @@ def get_target_uri(self, _docname: str, _typ: Optional[str] = None) -> str: def build_needumls_pumls(app: Sphinx, _exception: Exception) -> None: - env = unwrap(app.env) + env = app.env config = NeedsSphinxConfig(env.config) if not config.build_needumls: @@ -174,7 +173,7 @@ def write_doc(self, docname: str, doctree: nodes.document) -> None: pass def finish(self) -> None: - env = unwrap(self.env) + env = self.env data = SphinxNeedsData(env) needs = data.get_or_create_needs().values() # We need a list of needs for later filter checks version = getattr(env.config, "version", "unset") @@ -216,7 +215,7 @@ def get_target_uri(self, _docname: str, _typ: Optional[str] = None) -> str: def build_needs_id_json(app: Sphinx, _exception: Exception) -> None: - env = unwrap(app.env) + env = app.env if not NeedsSphinxConfig(env.config).build_json_per_id: return diff --git a/sphinx_needs/diagrams_common.py b/sphinx_needs/diagrams_common.py index 243f17533..57167d310 100644 --- a/sphinx_needs/diagrams_common.py +++ b/sphinx_needs/diagrams_common.py @@ -18,7 +18,7 @@ from sphinx_needs.data import NeedsFilteredBaseType, NeedsPartsInfoType from sphinx_needs.errors import NoUri from sphinx_needs.logging import get_logger -from sphinx_needs.utils import get_scale, split_link_types, unwrap +from sphinx_needs.utils import get_scale, split_link_types try: from typing import TypedDict @@ -169,7 +169,7 @@ def calculate_link(app: Sphinx, need_info: NeedsPartsInfoType, _fromdocname: str :param fromdocname: :return: """ - builder = unwrap(app.builder) + builder = app.builder try: if need_info["is_external"]: assert need_info["external_url"] is not None, "external_url must be set for external needs" diff --git a/sphinx_needs/directives/need.py b/sphinx_needs/directives/need.py index 5381004c7..3f4385a95 100644 --- a/sphinx_needs/directives/need.py +++ b/sphinx_needs/directives/need.py @@ -28,7 +28,7 @@ from sphinx_needs.logging import get_logger from sphinx_needs.need_constraints import process_constraints from sphinx_needs.nodes import Need -from sphinx_needs.utils import add_doc, profile, unwrap +from sphinx_needs.utils import add_doc, profile logger = get_logger(__name__) @@ -296,8 +296,7 @@ def analyse_need_locations(app: Sphinx, doctree: nodes.document) -> None: (i.e. ones that should not be rendered in the output) are removed from the doctree. """ - builder = unwrap(app.builder) - env = unwrap(builder.env) + env = app.env needs = SphinxNeedsData(env).get_or_create_needs() @@ -376,8 +375,7 @@ def process_need_nodes(app: Sphinx, doctree: nodes.document, fromdocname: str) - node.parent.remove(node) # type: ignore return - builder = unwrap(app.builder) - env = unwrap(builder.env) + env = app.env # If no needs were defined, we do not need to do anything if not hasattr(env, "needs_all_needs"): @@ -430,8 +428,7 @@ def print_need_nodes(app: Sphinx, doctree: nodes.document, fromdocname: str, fou :param fromdocname: :return: """ - builder = unwrap(app.builder) - env = unwrap(builder.env) + env = app.env needs = SphinxNeedsData(env).get_or_create_needs() # We try to avoid findall as much as possibles. so we reuse the already found need nodes in the current document. diff --git a/sphinx_needs/directives/needbar.py b/sphinx_needs/directives/needbar.py index 777b07525..a61daab6a 100644 --- a/sphinx_needs/directives/needbar.py +++ b/sphinx_needs/directives/needbar.py @@ -10,7 +10,7 @@ from sphinx_needs.config import NeedsSphinxConfig from sphinx_needs.data import SphinxNeedsData from sphinx_needs.filter_common import FilterBase, filter_needs, prepare_need_list -from sphinx_needs.utils import add_doc, save_matplotlib_figure, unwrap +from sphinx_needs.utils import add_doc, save_matplotlib_figure if not os.environ.get("DISPLAY"): matplotlib.use("Agg") @@ -168,8 +168,7 @@ def run(self) -> Sequence[nodes.Node]: # 9. final storage # 10. cleanup matplotlib def process_needbar(app: Sphinx, doctree: nodes.document, fromdocname: str, found_nodes: List[nodes.Element]) -> None: - builder = unwrap(app.builder) - env = unwrap(builder.env) + env = app.env needs_config = NeedsSphinxConfig(env.config) # NEEDFLOW diff --git a/sphinx_needs/directives/needextend.py b/sphinx_needs/directives/needextend.py index 1b2c7f75d..8be299274 100644 --- a/sphinx_needs/directives/needextend.py +++ b/sphinx_needs/directives/needextend.py @@ -15,7 +15,7 @@ from sphinx_needs.data import SphinxNeedsData from sphinx_needs.filter_common import filter_needs from sphinx_needs.logging import get_logger -from sphinx_needs.utils import add_doc, unwrap +from sphinx_needs.utils import add_doc logger = get_logger(__name__) @@ -75,8 +75,7 @@ def process_needextend(app: Sphinx, doctree: nodes.document, fromdocname: str) - """ Perform all modifications on needs """ - builder = unwrap(app.builder) - env = unwrap(builder.env) + env = app.env needs_config = NeedsSphinxConfig(env.config) data = SphinxNeedsData(env) workflow = data.get_or_create_workflow() diff --git a/sphinx_needs/directives/needextract.py b/sphinx_needs/directives/needextract.py index d4cc0406e..dc397a5a5 100644 --- a/sphinx_needs/directives/needextract.py +++ b/sphinx_needs/directives/needextract.py @@ -19,7 +19,7 @@ ) from sphinx_needs.filter_common import FilterBase, process_filters from sphinx_needs.layout import create_need -from sphinx_needs.utils import add_doc, unwrap +from sphinx_needs.utils import add_doc class Needextract(nodes.General, nodes.Element): @@ -75,7 +75,7 @@ def process_needextract( """ Replace all needextract nodes with a list of the collected needs. """ - env = unwrap(app.env) + env = app.env needs_config = NeedsSphinxConfig(app.config) for node in found_nodes: diff --git a/sphinx_needs/directives/needfilter.py b/sphinx_needs/directives/needfilter.py index ef196bb73..d1d6326f0 100644 --- a/sphinx_needs/directives/needfilter.py +++ b/sphinx_needs/directives/needfilter.py @@ -12,7 +12,7 @@ from sphinx_needs.data import SphinxNeedsData from sphinx_needs.diagrams_common import create_legend from sphinx_needs.filter_common import FilterBase, process_filters -from sphinx_needs.utils import add_doc, row_col_maker, unwrap +from sphinx_needs.utils import add_doc, row_col_maker class Needfilter(nodes.General, nodes.Element): @@ -74,8 +74,8 @@ def process_needfilters( ) -> None: # Replace all needlist nodes with a list of the collected needs. # Augment each need with a backlink to the original location. - builder = unwrap(app.builder) - env = unwrap(builder.env) + builder = app.builder + env = app.env needs_config = NeedsSphinxConfig(env.config) all_needs = SphinxNeedsData(env).get_or_create_needs() diff --git a/sphinx_needs/directives/needflow.py b/sphinx_needs/directives/needflow.py index 19315e33c..98ead1bd6 100644 --- a/sphinx_needs/directives/needflow.py +++ b/sphinx_needs/directives/needflow.py @@ -21,7 +21,7 @@ from sphinx_needs.diagrams_common import calculate_link, create_legend from sphinx_needs.filter_common import FilterBase, filter_single_need, process_filters from sphinx_needs.logging import get_logger -from sphinx_needs.utils import add_doc, get_scale, split_link_types, unwrap +from sphinx_needs.utils import add_doc, get_scale, split_link_types logger = get_logger(__name__) @@ -271,7 +271,7 @@ def cal_needs_node( def process_needflow(app: Sphinx, doctree: nodes.document, fromdocname: str, found_nodes: List[nodes.Element]) -> None: # Replace all needflow nodes with a list of the collected needs. # Augment each need with a backlink to the original location. - env = unwrap(app.env) + env = app.env needs_config = NeedsSphinxConfig(app.config) env_data = SphinxNeedsData(env) all_needs = env_data.get_or_create_needs() diff --git a/sphinx_needs/directives/needgantt.py b/sphinx_needs/directives/needgantt.py index 05a66862b..abec283bd 100644 --- a/sphinx_needs/directives/needgantt.py +++ b/sphinx_needs/directives/needgantt.py @@ -135,7 +135,7 @@ def get_link_type_option(self, name: str, default: str = "") -> List[str]: def process_needgantt(app: Sphinx, doctree: nodes.document, fromdocname: str, found_nodes: List[nodes.Element]) -> None: # Replace all needgantt nodes with a list of the collected needs. - env = app.builder.env + env = app.env needs_config = NeedsSphinxConfig(app.config) # link_types = needs_config.extra_links diff --git a/sphinx_needs/directives/needlist.py b/sphinx_needs/directives/needlist.py index f35f4a904..ea8979560 100644 --- a/sphinx_needs/directives/needlist.py +++ b/sphinx_needs/directives/needlist.py @@ -15,7 +15,7 @@ used_filter_paragraph, ) from sphinx_needs.filter_common import FilterBase, process_filters -from sphinx_needs.utils import add_doc, check_and_calc_base_url_rel_path, unwrap +from sphinx_needs.utils import add_doc, check_and_calc_base_url_rel_path class Needlist(nodes.General, nodes.Element): @@ -64,8 +64,8 @@ def process_needlist(app: Sphinx, doctree: nodes.document, fromdocname: str, fou Replace all needlist nodes with a list of the collected needs. Augment each need with a backlink to the original location. """ - builder = unwrap(app.builder) - env = unwrap(builder.env) + builder = app.builder + env = app.env include_needs = NeedsSphinxConfig(env.config).include_needs # for node in doctree.findall(Needlist): diff --git a/sphinx_needs/directives/needpie.py b/sphinx_needs/directives/needpie.py index 1dcb2982a..08250f621 100644 --- a/sphinx_needs/directives/needpie.py +++ b/sphinx_needs/directives/needpie.py @@ -24,7 +24,6 @@ add_doc, check_and_get_external_filter_func, save_matplotlib_figure, - unwrap, ) logger = get_logger(__name__) @@ -111,8 +110,7 @@ def run(self) -> Sequence[nodes.Node]: @measure_time("needpie") def process_needpie(app: Sphinx, doctree: nodes.document, fromdocname: str, found_nodes: List[nodes.Element]) -> None: - builder = unwrap(app.builder) - env = unwrap(builder.env) + env = app.env needs_data = SphinxNeedsData(env) # NEEDFLOW diff --git a/sphinx_needs/directives/needsequence.py b/sphinx_needs/directives/needsequence.py index c98e885c8..c250921df 100644 --- a/sphinx_needs/directives/needsequence.py +++ b/sphinx_needs/directives/needsequence.py @@ -21,7 +21,7 @@ ) from sphinx_needs.filter_common import FilterBase from sphinx_needs.logging import get_logger -from sphinx_needs.utils import add_doc, unwrap +from sphinx_needs.utils import add_doc logger = get_logger(__name__) @@ -76,8 +76,7 @@ def process_needsequence( app: Sphinx, doctree: nodes.document, fromdocname: str, found_nodes: List[nodes.Element] ) -> None: # Replace all needsequence nodes with a list of the collected needs. - builder = unwrap(app.builder) - env = unwrap(builder.env) + env = app.env needs_data = SphinxNeedsData(env) all_needs_dict = needs_data.get_or_create_needs() diff --git a/sphinx_needs/directives/needservice.py b/sphinx_needs/directives/needservice.py index 072a27a16..2adb2cf64 100644 --- a/sphinx_needs/directives/needservice.py +++ b/sphinx_needs/directives/needservice.py @@ -12,7 +12,7 @@ from sphinx_needs.directives.need import NeedDirective from sphinx_needs.logging import get_logger from sphinx_needs.services.base import BaseService -from sphinx_needs.utils import add_doc, unwrap +from sphinx_needs.utils import add_doc class Needservice(nodes.General, nodes.Element): @@ -58,7 +58,8 @@ def run(self) -> Sequence[nodes.Node]: needs_services: Dict[str, BaseService] = getattr(app, "needs_services", {}) service_name = self.arguments[0] - service = unwrap(needs_services.get(service_name)) + service = needs_services.get(service_name) + assert service is not None section = [] if "debug" not in self.options: diff --git a/sphinx_needs/directives/needtable.py b/sphinx_needs/directives/needtable.py index 304ee48ce..7cf4faf06 100644 --- a/sphinx_needs/directives/needtable.py +++ b/sphinx_needs/directives/needtable.py @@ -17,7 +17,7 @@ ) from sphinx_needs.filter_common import FilterBase, process_filters from sphinx_needs.functions.functions import check_and_get_content -from sphinx_needs.utils import add_doc, profile, row_col_maker, unwrap +from sphinx_needs.utils import add_doc, profile, row_col_maker class Needtable(nodes.General, nodes.Element): @@ -117,14 +117,8 @@ def process_needtables( ) -> None: """ Replace all needtables nodes with a table of filtered nodes. - - :param app: - :param doctree: - :param fromdocname: - :return: """ - builder = unwrap(app.builder) - env = unwrap(builder.env) + env = app.env needs_config = NeedsSphinxConfig(app.config) needs_data = SphinxNeedsData(env) diff --git a/sphinx_needs/directives/needuml.py b/sphinx_needs/directives/needuml.py index 30e3b96a5..e3ea4365b 100644 --- a/sphinx_needs/directives/needuml.py +++ b/sphinx_needs/directives/needuml.py @@ -405,7 +405,7 @@ def is_element_of_need(node: nodes.Element) -> str: @measure_time("needuml") def process_needuml(app: Sphinx, doctree: nodes.document, fromdocname: str, found_nodes: List[nodes.Element]) -> None: - env = app.builder.env + env = app.env # for node in doctree.findall(Needuml): for node in found_nodes: diff --git a/sphinx_needs/environment.py b/sphinx_needs/environment.py index 09cb7c9a2..2b78a170f 100644 --- a/sphinx_needs/environment.py +++ b/sphinx_needs/environment.py @@ -9,7 +9,7 @@ from sphinx.util.osutil import copyfile from sphinx_needs.config import NeedsSphinxConfig -from sphinx_needs.utils import logger, unwrap +from sphinx_needs.utils import logger IMAGE_DIR_NAME = "_static" @@ -26,7 +26,7 @@ def safe_add_file(filename: Path, app: Sphinx) -> None: :param app: app object :return: None """ - builder = unwrap(app.builder) + builder = app.builder # Use PurePosixPath, so that the path can be used as "web"-path pure_path = PurePosixPath(filename) static_data_file = PurePosixPath("_static") / pure_path @@ -74,7 +74,7 @@ def _remove_file(file: PurePosixPath, attribute: str) -> None: # Base implementation from sphinxcontrib-images # https://github.com/spinus/sphinxcontrib-images/blob/master/sphinxcontrib/images.py#L203 def install_styles_static_files(app: Sphinx, env: BuildEnvironment) -> None: - builder = unwrap(app.builder) + builder = app.builder # Do not copy static_files for our "needs" builder if builder.name == "needs": return @@ -131,7 +131,7 @@ def install_static_files( files_to_copy: List[Path], message: str, ) -> None: - builder = unwrap(app.builder) + builder = app.builder # Do not copy static_files for our "needs" builder if builder.name == "needs": return @@ -165,7 +165,7 @@ def install_lib_static_files(app: Sphinx, env: BuildEnvironment) -> None: :param env: :return: """ - builder = unwrap(app.builder) + builder = app.builder # Do not copy static_files for our "needs" builder if builder.name == "needs": return @@ -197,7 +197,7 @@ def install_permalink_file(app: Sphinx, env: BuildEnvironment) -> None: :param env: :return: """ - builder = unwrap(app.builder) + builder = app.builder # Do not copy static_files for our "needs" builder if builder.name == "needs": return diff --git a/sphinx_needs/layout.py b/sphinx_needs/layout.py index 4bc206285..887d2fea8 100644 --- a/sphinx_needs/layout.py +++ b/sphinx_needs/layout.py @@ -25,7 +25,7 @@ from sphinx_needs.config import NeedsSphinxConfig from sphinx_needs.data import NeedsInfoType, SphinxNeedsData from sphinx_needs.debug import measure_time -from sphinx_needs.utils import INTERNALS, match_string_link, unwrap +from sphinx_needs.utils import INTERNALS, match_string_link @measure_time("need") @@ -44,7 +44,7 @@ def create_need( :param docname: Needed for calculating references :return: """ - env = app.builder.env + env = app.env needs = SphinxNeedsData(env).get_or_create_needs() if need_id not in needs.keys(): @@ -145,7 +145,7 @@ def build_need( The level structure must be kept, otherwise docutils can not handle it! """ - env = app.builder.env + env = app.env needs = SphinxNeedsData(env).get_or_create_needs() node_container = nodes.container() @@ -763,8 +763,8 @@ def image( :return: An inline docutils node element :rtype: :class: docutils.nodes.inline """ - builder = unwrap(self.app.builder) - env = unwrap(builder.env) + builder = self.app.builder + env = self.app.env data_container = nodes.inline() if prefix: @@ -934,7 +934,7 @@ def collapse_button( :param initial: If True, initial status will hide rows after loading page. :return: docutils nodes """ - builder = unwrap(self.app.builder) + builder = self.app.builder if any(x in builder.name.upper() for x in ["PDF", "LATEX"]): # PDF/Latex output do not support collapse functions return None diff --git a/sphinx_needs/roles/need_count.py b/sphinx_needs/roles/need_count.py index f4aa8dd41..cc5336580 100644 --- a/sphinx_needs/roles/need_count.py +++ b/sphinx_needs/roles/need_count.py @@ -13,7 +13,6 @@ from sphinx_needs.data import SphinxNeedsData from sphinx_needs.filter_common import filter_needs, prepare_need_list from sphinx_needs.logging import get_logger -from sphinx_needs.utils import unwrap log = get_logger(__name__) @@ -25,8 +24,7 @@ class NeedCount(nodes.Inline, nodes.Element): # type: ignore def process_need_count( app: Sphinx, doctree: nodes.document, _fromdocname: str, found_nodes: List[nodes.Element] ) -> None: - builder = unwrap(app.builder) - env = unwrap(builder.env) + env = app.env # for node_need_count in doctree.findall(NeedCount): for node_need_count in found_nodes: all_needs = list(SphinxNeedsData(env).get_or_create_needs().values()) diff --git a/sphinx_needs/roles/need_func.py b/sphinx_needs/roles/need_func.py index 8cc346897..7d730b1a9 100644 --- a/sphinx_needs/roles/need_func.py +++ b/sphinx_needs/roles/need_func.py @@ -9,7 +9,6 @@ from sphinx_needs.functions.functions import check_and_get_content from sphinx_needs.logging import get_logger -from sphinx_needs.utils import unwrap log = get_logger(__name__) @@ -21,8 +20,7 @@ class NeedFunc(nodes.Inline, nodes.Element): # type: ignore def process_need_func( app: Sphinx, doctree: nodes.document, _fromdocname: str, found_nodes: List[nodes.Element] ) -> None: - builder = unwrap(app.builder) - env = unwrap(builder.env) + env = app.env # for node_need_func in doctree.findall(NeedFunc): for node_need_func in found_nodes: result = check_and_get_content(node_need_func.attributes["reftarget"], {"id": "need_func_dummy"}, env) diff --git a/sphinx_needs/roles/need_incoming.py b/sphinx_needs/roles/need_incoming.py index e32900d10..bbeb9b141 100644 --- a/sphinx_needs/roles/need_incoming.py +++ b/sphinx_needs/roles/need_incoming.py @@ -7,7 +7,7 @@ from sphinx_needs.config import NeedsSphinxConfig from sphinx_needs.data import SphinxNeedsData from sphinx_needs.errors import NoUri -from sphinx_needs.utils import check_and_calc_base_url_rel_path, logger, unwrap +from sphinx_needs.utils import check_and_calc_base_url_rel_path, logger class NeedIncoming(nodes.Inline, nodes.Element): # type: ignore @@ -17,8 +17,8 @@ class NeedIncoming(nodes.Inline, nodes.Element): # type: ignore def process_need_incoming( app: Sphinx, doctree: nodes.document, fromdocname: str, found_nodes: List[nodes.Element] ) -> None: - builder = unwrap(app.builder) - env = unwrap(builder.env) + builder = app.builder + env = app.env needs_config = NeedsSphinxConfig(env.config) all_needs = SphinxNeedsData(env).get_or_create_needs() diff --git a/sphinx_needs/roles/need_outgoing.py b/sphinx_needs/roles/need_outgoing.py index 38a05b8cd..3a7ba1c9e 100644 --- a/sphinx_needs/roles/need_outgoing.py +++ b/sphinx_needs/roles/need_outgoing.py @@ -8,7 +8,7 @@ from sphinx_needs.data import SphinxNeedsData from sphinx_needs.errors import NoUri from sphinx_needs.logging import get_logger -from sphinx_needs.utils import check_and_calc_base_url_rel_path, unwrap +from sphinx_needs.utils import check_and_calc_base_url_rel_path log = get_logger(__name__) @@ -20,8 +20,8 @@ class NeedOutgoing(nodes.Inline, nodes.Element): # type: ignore def process_need_outgoing( app: Sphinx, doctree: nodes.document, fromdocname: str, found_nodes: List[nodes.Element] ) -> None: - builder = unwrap(app.builder) - env = unwrap(app.env) + builder = app.builder + env = app.env needs_config = NeedsSphinxConfig(app.config) report_dead_links = needs_config.report_dead_links # for node_need_ref in doctree.findall(NeedOutgoing): diff --git a/sphinx_needs/roles/need_part.py b/sphinx_needs/roles/need_part.py index 0246069ce..e6be611c5 100644 --- a/sphinx_needs/roles/need_part.py +++ b/sphinx_needs/roles/need_part.py @@ -16,7 +16,6 @@ from sphinx_needs.data import NeedsInfoType from sphinx_needs.logging import get_logger -from sphinx_needs.utils import unwrap log = get_logger(__name__) @@ -33,8 +32,8 @@ def process_need_part(app: Sphinx, doctree: nodes.document, fromdocname: str, fo def update_need_with_parts(env: BuildEnvironment, need: NeedsInfoType, part_nodes: List[NeedPart]) -> None: - app = unwrap(env.app) - builder = unwrap(app.builder) + app = env.app + builder = app.builder for part_node in part_nodes: content = cast(str, part_node.children[0].children[0]) # ->inline->Text result = part_pattern.match(content) diff --git a/sphinx_needs/roles/need_ref.py b/sphinx_needs/roles/need_ref.py index d7f839ec5..d054766e6 100644 --- a/sphinx_needs/roles/need_ref.py +++ b/sphinx_needs/roles/need_ref.py @@ -10,7 +10,7 @@ from sphinx_needs.data import NeedsInfoType, SphinxNeedsData from sphinx_needs.errors import NoUri from sphinx_needs.logging import get_logger -from sphinx_needs.utils import check_and_calc_base_url_rel_path, unwrap +from sphinx_needs.utils import check_and_calc_base_url_rel_path log = get_logger(__name__) @@ -51,8 +51,8 @@ def transform_need_to_dict(need: NeedsInfoType) -> Dict[str, str]: def process_need_ref(app: Sphinx, doctree: nodes.document, fromdocname: str, found_nodes: List[nodes.Element]) -> None: - builder = unwrap(app.builder) - env = unwrap(builder.env) + builder = app.builder + env = app.env needs_config = NeedsSphinxConfig(env.config) all_needs = SphinxNeedsData(env).get_or_create_needs() # for node_need_ref in doctree.findall(NeedRef): diff --git a/sphinx_needs/utils.py b/sphinx_needs/utils.py index 2ad3db321..5790c552e 100644 --- a/sphinx_needs/utils.py +++ b/sphinx_needs/utils.py @@ -130,8 +130,8 @@ def row_col_maker( :param prefix: string, which is used as prefix for the text output :return: column object (nodes.entry) """ - builder = unwrap(app.builder) - env = unwrap(builder.env) + builder = app.builder + env = app.env needs_config = NeedsSphinxConfig(env.config) row_col = nodes.entry(classes=["needs_" + need_key]) @@ -238,7 +238,7 @@ def rstjinja(app: Sphinx, docname: str, source: List[str]) -> None: """ Render our pages as a jinja template for fancy templating goodness. """ - builder = unwrap(app.builder) + builder = app.builder # Make sure we're outputting HTML if builder.format != "html": return @@ -383,8 +383,8 @@ def jinja_parse(context: Dict[str, Any], jinja_string: str) -> str: def save_matplotlib_figure(app: Sphinx, figure: FigureBase, basename: str, fromdocname: str) -> nodes.image: - builder = unwrap(app.builder) - env = unwrap(builder.env) + builder = app.builder + env = app.env image_folder = os.path.join(builder.outdir, builder.imagedir) os.makedirs(image_folder, exist_ok=True) @@ -573,14 +573,6 @@ def clean_log(data: str) -> str: return clean_credentials -T = TypeVar("T") - - -def unwrap(obj: Optional[T]) -> T: - assert obj is not None - return obj - - def node_match(node_types: Union[Type[nodes.Element], List[Type[nodes.Element]]]) -> Callable[[nodes.Node], bool]: """ Returns a condition function for doctuils.nodes.findall() diff --git a/sphinx_needs/warnings.py b/sphinx_needs/warnings.py index 950f138a0..4e237cb60 100644 --- a/sphinx_needs/warnings.py +++ b/sphinx_needs/warnings.py @@ -11,7 +11,6 @@ from sphinx_needs.data import NeedsInfoType, SphinxNeedsData from sphinx_needs.filter_common import filter_needs from sphinx_needs.logging import get_logger -from sphinx_needs.utils import unwrap logger = get_logger(__name__) @@ -32,7 +31,7 @@ def process_warnings(app: Sphinx, exception: Optional[Exception]) -> None: if exception: return - env = unwrap(app.env) + env = app.env # If no needs were defined, we do not need to do anything if not hasattr(env, "needs_all_needs"): return diff --git a/tests/test_needs_id_builder.py b/tests/test_needs_id_builder.py index 6f0f2ec8c..cbf86ad46 100644 --- a/tests/test_needs_id_builder.py +++ b/tests/test_needs_id_builder.py @@ -1,4 +1,5 @@ import json +import os from pathlib import Path import pytest @@ -11,14 +12,10 @@ "test_app", [{"buildername": "needs_id", "srcdir": "doc_test/doc_needs_builder"}], indirect=True ) def test_doc_needs_id_builder(test_app): - import os - - from sphinx_needs.utils import unwrap - app = test_app app.build() out_dir = app.outdir - env = unwrap(app.env) + env = app.env data = SphinxNeedsData(env) needs_config = NeedsSphinxConfig(env.config) needs = data.get_or_create_needs().values() # We need a list of needs for later filter checks