Skip to content

Commit 5bb9664

Browse files
authored
Merge pull request #2410 from strictdoc-project/stanislaw/markers
Code climate: mypy: fix several remaining union-attr issues
2 parents 38a7ba5 + e3c1d25 commit 5bb9664

File tree

8 files changed

+43
-12
lines changed

8 files changed

+43
-12
lines changed

strictdoc/backend/excel/export/excel_generator.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
from strictdoc.backend.sdoc.models.document import SDocDocument
1515
from strictdoc.backend.sdoc.models.grammar_element import ReferenceType
16-
from strictdoc.backend.sdoc.models.model import SDocElementIF
16+
from strictdoc.backend.sdoc.models.model import (
17+
SDocIteratedElementIF,
18+
)
1719
from strictdoc.backend.sdoc.models.node import SDocNode
1820
from strictdoc.backend.sdoc.models.reference import (
1921
FileReference,
@@ -236,7 +238,9 @@ def _export_single_document(
236238
os.unlink(document_out_file)
237239

238240
@staticmethod
239-
def _lookup_refs(document_contents: List[SDocElementIF]) -> Dict[str, int]:
241+
def _lookup_refs(
242+
document_contents: List[SDocIteratedElementIF],
243+
) -> Dict[str, int]:
240244
refs: Dict[str, int] = {}
241245
row = 1
242246

strictdoc/backend/sdoc/error_handling.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# mypy: disable-error-code="union-attr"
21
from typing import Optional, Union
32

43
from textx import TextXSyntaxError
@@ -334,7 +333,7 @@ def invalid_marker_role(
334333
) -> "StrictDocSemanticError":
335334
role_and_type = marker.role if marker.role is not None else "Any"
336335
return StrictDocSemanticError(
337-
title=(f"File marker role is not registered: {role_and_type}."),
336+
title=f"File marker role is not registered: {role_and_type}.",
338337
hint=f"Problematic requirement: {node.reserved_uid}.",
339338
example=None,
340339
line=marker.ng_source_line_begin,
@@ -368,6 +367,7 @@ def view_references_nonexisting_grammar_element(
368367
view_element: ViewElement,
369368
object_type: str,
370369
) -> "StrictDocSemanticError":
370+
assert document.meta is not None
371371
return StrictDocSemanticError(
372372
title=(
373373
f"View element '{view_element.view_id}' references a non-existing"
@@ -391,6 +391,7 @@ def view_references_nonexisting_field(
391391
object_type: str,
392392
field_name: str,
393393
) -> "StrictDocSemanticError":
394+
assert document.meta is not None
394395
return StrictDocSemanticError(
395396
title=(
396397
f"View element '{view_element.view_id}' references a non-existing"
@@ -412,6 +413,8 @@ def default_view_doesnt_exist(
412413
document_config: DocumentConfig,
413414
default_view: str,
414415
) -> "StrictDocSemanticError":
416+
assert document.meta is not None
417+
415418
filename = document.meta.input_doc_full_path
416419

417420
return StrictDocSemanticError(

strictdoc/backend/sdoc/models/document_grammar.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# mypy: disable-error-code="union-attr"
21
from typing import Dict, List, Optional, Set
32

43
from strictdoc.backend.sdoc.models.grammar_element import (

strictdoc/backend/sdoc/models/model.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class SDocNodeFieldIF(ABC):
5151

5252
class SDocNodeIF(ABC):
5353
reserved_mid: MID
54+
mid_permanent: bool
5455
node_type: str
5556
section_contents: List["SDocElementIF"]
5657
ng_level: Optional[int]
@@ -96,6 +97,8 @@ def get_prefix(self) -> Optional[str]:
9697

9798

9899
class SDocSectionIF(ABC):
100+
reserved_mid: MID
101+
mid_permanent: bool
99102
parent: Union["SDocDocumentIF", "SDocSectionIF"]
100103
section_contents: List["SDocElementIF"]
101104
ng_level: Optional[int]
@@ -124,6 +127,8 @@ class SDocGrammarIF:
124127

125128

126129
class SDocDocumentIF(ABC):
130+
reserved_mid: MID
131+
mid_permanent: bool
127132
section_contents: List["SDocElementIF"]
128133
included_documents: List["SDocDocumentIF"]
129134
config: DocumentConfig
@@ -181,6 +186,13 @@ def section_contents(self) -> List[SDocDocumentIF]:
181186
]
182187

183188

189+
SDocIteratedElementIF = Union[
190+
SDocNodeIF,
191+
SDocSectionIF,
192+
SDocDocumentIF,
193+
]
194+
195+
184196
SDocExtendedElementIF = Union[
185197
SDocElementIF,
186198
SourceFileTraceabilityInfo,

strictdoc/backend/sdoc_source_code/models/range_marker.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ def __init__(
9393

9494
# Line number of the marker in the source code.
9595
self.ng_source_line_begin: Optional[int] = None
96+
# The column number is never used but keeping for compatibility with
97+
# the other markers when used for error reporting.
98+
self.ng_source_column_begin: Optional[int] = None
9699

97100
self.ng_range_line_begin: Optional[int] = None
98101
self.ng_range_line_end: Optional[int] = None

strictdoc/core/document_iterator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
SDocDocumentFromFileIF,
88
SDocDocumentIF,
99
SDocElementIF,
10+
SDocIteratedElementIF,
1011
SDocNodeIF,
1112
SDocSectionIF,
1213
)
@@ -71,7 +72,7 @@ def table_of_contents(
7172
def all_content(
7273
self,
7374
print_fragments: bool = False,
74-
) -> Iterator[Tuple[SDocElementIF, DocumentIterationContext]]:
75+
) -> Iterator[Tuple[SDocIteratedElementIF, DocumentIterationContext]]:
7576
root_node = self.document
7677

7778
yield from self.all_node_content(
@@ -87,7 +88,7 @@ def all_node_content(
8788
print_fragments: bool = False,
8889
level_stack: Tuple[int, ...] = (),
8990
custom_level: bool = False,
90-
) -> Iterator[Tuple[SDocElementIF, DocumentIterationContext]]:
91+
) -> Iterator[Tuple[SDocIteratedElementIF, DocumentIterationContext]]:
9192
if isinstance(node, SDocSection):
9293
# If node is not whitelisted, we ignore it. Also, note that due to
9394
# this early return, all child nodes of this node are ignored

strictdoc/export/html/html_generator.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# mypy: disable-error-code="arg-type,union-attr"
21
import importlib
32
import os
43
import sys
@@ -50,6 +49,7 @@
5049
from strictdoc.export.html.renderers.link_renderer import LinkRenderer
5150
from strictdoc.export.html.renderers.markup_renderer import MarkupRenderer
5251
from strictdoc.export.html.tools.html_embedded import HTMLEmbedder
52+
from strictdoc.helpers.cast import assert_cast
5353
from strictdoc.helpers.exception import StrictDocException
5454
from strictdoc.helpers.file_modification_time import get_file_modification_time
5555
from strictdoc.helpers.file_system import sync_dir
@@ -268,9 +268,12 @@ def export_assets(
268268

269269
redundant_assets: Dict[str, List[SDocRelativePath]] = {}
270270
for document_ in traceability_index.document_tree.document_list:
271+
assert document_.meta is not None
271272
for (
272273
included_document_
273274
) in document_.iterate_included_documents_depth_first():
275+
assert included_document_.meta is not None
276+
274277
redundant_assets.setdefault(
275278
document_.meta.input_doc_assets_dir_rel_path.relative_path_posix,
276279
[],
@@ -279,6 +282,8 @@ def export_assets(
279282
document_.meta.input_doc_assets_dir_rel_path.relative_path_posix
280283
].append(included_document_.meta.input_doc_assets_dir_rel_path)
281284

285+
assert traceability_index.asset_manager is not None
286+
282287
asset_dir_: AssetDir
283288
for asset_dir_ in traceability_index.asset_manager.iterate():
284289
source_path = asset_dir_.full_path
@@ -322,14 +327,16 @@ def export_single_document_with_performance(
322327
if specific_documents is None:
323328
specific_documents = DocumentType.all()
324329

325-
input_doc_full_path = document.meta.input_doc_full_path
326-
output_doc_full_path = document.meta.output_document_full_path
330+
document_meta = assert_cast(document.meta, DocumentMeta)
331+
332+
input_doc_full_path = document_meta.input_doc_full_path
333+
output_doc_full_path = document_meta.output_document_full_path
327334

328335
if os.path.isfile(output_doc_full_path) and (
329336
get_file_modification_time(input_doc_full_path)
330337
< get_file_modification_time(output_doc_full_path)
331338
and not traceability_index.file_dependency_manager.must_generate(
332-
document.meta.input_doc_full_path
339+
document_meta.input_doc_full_path
333340
)
334341
):
335342
with measure_performance(f"Skip: {document.title}"):

strictdoc/git/project_diff_analyzer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# mypy: disable-error-code="union-attr"
21
import hashlib
32
import statistics
43
from dataclasses import dataclass, field
@@ -356,6 +355,7 @@ def _iterate_one_index(
356355
document.reserved_mid
357356
]
358357
else:
358+
assert document.meta is not None
359359
other_document_or_none = (
360360
other_stats.map_rel_paths_to_docs.get(
361361
document.meta.input_doc_rel_path.relative_path
@@ -952,6 +952,8 @@ def analyze_document(
952952
document: SDocDocument,
953953
document_tree_stats: ProjectTreeDiffStats,
954954
) -> None:
955+
assert document.meta is not None
956+
955957
document_iterator = DocumentCachingIterator(document)
956958

957959
map_nodes_to_hashers: Dict[Any, Any] = {document: hashlib.md5()}

0 commit comments

Comments
 (0)