Skip to content

Commit

Permalink
Merge pull request strictdoc-project#1923 from strictdoc-project/stan…
Browse files Browse the repository at this point in the history
…islaw/fix_type_issue

Code climate: document_iterator: add SDocAnyNode type hint
  • Loading branch information
stanislaw authored Jul 21, 2024
2 parents 82cecbf + beab30c commit 0cc71cd
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 48 deletions.
28 changes: 15 additions & 13 deletions strictdoc/backend/reqif/p01_sdoc/sdoc_to_reqif_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,21 +235,22 @@ def convert_document_tree(
current_hierarchy = root_hierarchy

# FIXME: ReqIF must export complete documents including fragments.
for node in document_iterator.all_content(
for node_ in document_iterator.all_content(
print_fragments=False, print_fragments_from_files=False
):
if node.is_composite_requirement:
if node_.is_composite_requirement:
raise NotImplementedError(
"Exporting composite requirements is not "
"supported yet.",
node,
node_,
)
if node.is_section:
if node_.is_section:
section: SDocSection = assert_cast(node_, SDocSection)
# fmt: off
spec_object = (
P01_SDocToReqIFObjectConverter
._convert_section_to_spec_object(
section=node,
section=section,
context=context,
)
)
Expand All @@ -264,14 +265,14 @@ def convert_document_tree(
spec_object=spec_object.identifier,
children=[],
ref_then_children_order=True,
level=node.ng_level,
level=section.ng_level,
)
if node.ng_level > current_hierarchy.level:
if section.ng_level > current_hierarchy.level:
parents[hierarchy] = current_hierarchy
current_hierarchy.add_child(hierarchy)
elif node.ng_level < current_hierarchy.level:
elif section.ng_level < current_hierarchy.level:
for _ in range(
0, (current_hierarchy.level - node.ng_level + 1)
0, (current_hierarchy.level - section.ng_level + 1)
):
current_hierarchy = parents[current_hierarchy]
current_hierarchy.add_child(hierarchy)
Expand All @@ -282,9 +283,10 @@ def convert_document_tree(
parents[hierarchy] = current_hierarchy_parent
current_hierarchy = hierarchy

elif node.is_requirement:
elif node_.is_requirement:
requirement = assert_cast(node_, SDocNode)
spec_object = cls._convert_requirement_to_spec_object(
requirement=node,
requirement=requirement,
grammar=assert_cast(document.grammar, DocumentGrammar),
context=context,
data_types=data_types,
Expand All @@ -302,10 +304,10 @@ def convert_document_tree(
spec_object=spec_object.identifier,
children=None,
ref_then_children_order=True,
level=node.ng_level,
level=requirement.ng_level,
)
for _ in range(
0, (current_hierarchy.level - node.ng_level + 1)
0, (current_hierarchy.level - requirement.ng_level + 1)
):
current_hierarchy = parents[current_hierarchy]
parents[hierarchy] = current_hierarchy
Expand Down
12 changes: 12 additions & 0 deletions strictdoc/backend/sdoc/models/any_node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from typing import Union

from strictdoc.backend.sdoc.models.document import SDocDocument
from strictdoc.backend.sdoc.models.node import SDocCompositeNode, SDocNode
from strictdoc.backend.sdoc.models.section import SDocSection

SDocAnyNode = Union[
SDocCompositeNode,
SDocSection,
SDocNode,
SDocDocument,
]
2 changes: 1 addition & 1 deletion strictdoc/backend/sdoc/models/document_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def get_requirement_style_mode(self) -> str:
return "zebra"
raise NotImplementedError(self.requirement_style)

def is_requirement_in_toc(self):
def is_requirement_in_toc(self) -> bool:
return (
self.requirement_in_toc is None or self.requirement_in_toc == "True"
)
Expand Down
69 changes: 39 additions & 30 deletions strictdoc/core/document_iterator.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
# mypy: disable-error-code="arg-type,attr-defined,no-any-return,no-untyped-call,no-untyped-def,operator,type-arg"
from typing import Optional, Tuple
# mypy: disable-error-code="attr-defined"
from typing import Iterator, Tuple, Union

from strictdoc.backend.sdoc.models.any_node import SDocAnyNode
from strictdoc.backend.sdoc.models.document import SDocDocument
from strictdoc.backend.sdoc.models.document_from_file import DocumentFromFile
from strictdoc.backend.sdoc.models.node import (
SDocCompositeNode,
SDocNode,
)
from strictdoc.backend.sdoc.models.section import FreeText, SDocSection
from strictdoc.backend.sdoc.models.section import SDocSection
from strictdoc.helpers.cast import assert_cast


class DocumentCachingIterator:
def __init__(self, document):
def __init__(self, document: SDocDocument) -> None:
assert isinstance(document, SDocDocument)

self.document = document
self.document: SDocDocument = document

def table_of_contents(self):
def table_of_contents(self) -> Iterator[SDocAnyNode]:
nodes_to_skip = (
(FreeText, SDocNode)
(SDocNode,)
if not self.document.config.is_requirement_in_toc()
else FreeText
else ()
)

for node in self.all_content(
Expand All @@ -37,7 +39,7 @@ def all_content(
self,
print_fragments: bool = False,
print_fragments_from_files: bool = False,
):
) -> Iterator[SDocAnyNode]:
root_node = self.document

yield from self._all_content(
Expand All @@ -48,30 +50,17 @@ def all_content(
custom_level=not root_node.config.auto_levels,
)

def all_content_from_node(
self,
node,
print_fragments: bool = False,
print_fragments_from_files: bool = False,
):
document = node if isinstance(node, SDocDocument) else node.document
yield from self._all_content(
node,
print_fragments=print_fragments,
print_fragments_from_files=print_fragments_from_files,
level_stack=(),
custom_level=not document.config.auto_levels,
)

def _all_content(
self,
node,
node: Union[SDocAnyNode, DocumentFromFile],
print_fragments: bool = False,
print_fragments_from_files: bool = False,
level_stack: Optional[Tuple] = (),
level_stack: Tuple[int, ...] = (),
custom_level: bool = False,
):
def get_level_string_(node_) -> str:
) -> Iterator[SDocAnyNode]:
def get_level_string_(
node_: Union[SDocSection, SDocNode, SDocCompositeNode],
) -> str:
if isinstance(node_, SDocNode) and node_.requirement_type == "TEXT":
return ""

Expand Down Expand Up @@ -108,7 +97,17 @@ def get_level_string_(node_) -> str:
current_number += 1

yield from self._all_content(
subnode_,
# FIXME: sections_contents(SDocObject) shall be changed to SDocAnyNode.
assert_cast(
subnode_,
(
SDocNode,
SDocCompositeNode,
SDocSection,
SDocDocument,
DocumentFromFile,
),
),
print_fragments=print_fragments,
print_fragments_from_files=print_fragments_from_files,
level_stack=level_stack + (current_number,),
Expand Down Expand Up @@ -181,7 +180,17 @@ def get_level_string_(node_) -> str:
):
current_number += 1
yield from self._all_content(
subnode_,
# FIXME: sections_contents(SDocObject) shall be changed to SDocAnyNode.
assert_cast(
subnode_,
(
SDocNode,
SDocCompositeNode,
SDocSection,
SDocDocument,
DocumentFromFile,
),
),
print_fragments=print_fragments,
print_fragments_from_files=print_fragments_from_files,
level_stack=level_stack + (current_number,),
Expand Down
8 changes: 5 additions & 3 deletions strictdoc/core/traceability_index_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from strictdoc.backend.sdoc.models.document_from_file import DocumentFromFile
from strictdoc.backend.sdoc.models.document_grammar import DocumentGrammar
from strictdoc.backend.sdoc.models.inline_link import InlineLink
from strictdoc.backend.sdoc.models.node import SDocNode
from strictdoc.backend.sdoc.models.node import SDocCompositeNode, SDocNode
from strictdoc.backend.sdoc.models.reference import (
ChildReqReference,
ParentReqReference,
Expand Down Expand Up @@ -465,7 +465,7 @@ def create_from_document_tree(
),
)
if node.is_requirement:
requirement: SDocNode = node
requirement: SDocNode = assert_cast(node, SDocNode)
if requirement.reserved_tags is not None:
for tag in requirement.reserved_tags:
document_tags.setdefault(tag, 0)
Expand Down Expand Up @@ -671,7 +671,9 @@ def create_from_document_tree(
):
if not node.is_requirement:
continue
requirement = node
requirement: Union[SDocNode, SDocCompositeNode] = assert_cast(
node, (SDocNode, SDocCompositeNode)
)
if requirement.reserved_uid is None:
continue

Expand Down
2 changes: 1 addition & 1 deletion tests/end2end/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Dummy comment to trigger CI
# Dummy comment to trigger CI.

0 comments on commit 0cc71cd

Please sign in to comment.