Skip to content

Commit c3b763b

Browse files
committed
Cleanup: use a type for associated products rather than Dict[str, object]
1 parent 2bfc616 commit c3b763b

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

snooty/parser.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,13 @@
7373
from .page import Page, PendingTask
7474
from .postprocess import Postprocessor, PostprocessorResult
7575
from .target_database import ProjectInterface, TargetDatabase
76-
from .types import BuildIdentifierSet, ParsedBannerConfig, ProjectConfig, StaticAsset
76+
from .types import (
77+
AssociatedProduct,
78+
BuildIdentifierSet,
79+
ParsedBannerConfig,
80+
ProjectConfig,
81+
StaticAsset,
82+
)
7783
from .util import RST_EXTENSIONS
7884

7985
NO_CHILDREN = (n.SubstitutionReference,)
@@ -1078,16 +1084,15 @@ def __make_child_visitor(self) -> "JSONVisitor":
10781084

10791085
def validate_toc_entries(
10801086
node_entries: List[TocTreeDirectiveEntry],
1081-
associated_products: List[Dict[str, object]],
1087+
associated_products: List[AssociatedProduct],
10821088
line: int,
10831089
) -> List[Diagnostic]:
10841090
"""
10851091
validates that external toc node exists as one of the associated products
10861092
if not found, removes this node and emits a warning
1087-
associated_products come in form of {name: str, versions: List[str]}
10881093
"""
10891094
diagnostics: List[Diagnostic] = []
1090-
associated_product_names = [product["name"] for product in associated_products]
1095+
associated_product_names = [product.name for product in associated_products]
10911096
for toc_entry in node_entries:
10921097
if (
10931098
toc_entry.ref_project

snooty/postprocess.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,9 @@ def generate_metadata(cls, context: Context) -> n.SerializedNode:
15811581
if project_config.deprecated_versions:
15821582
document["deprecated_versions"] = project_config.deprecated_versions
15831583
if project_config.associated_products:
1584-
document["associated_products"] = project_config.associated_products
1584+
document["associated_products"] = [
1585+
product.serialize() for product in project_config.associated_products
1586+
]
15851587
# Update metadata document with key-value pairs defined in event parser
15861588
document["slugToTitle"] = {
15871589
k: [node.serialize() for node in v]

snooty/test_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ def test_project() -> None:
3535
project_config, project_diganostics = ProjectConfig.open(path)
3636
assert len(project_diganostics) == 0
3737
assert len(project_config.associated_products) > 0
38-
assert project_config.associated_products[0]["name"] == "test-name"
39-
assert project_config.associated_products[0]["versions"] == ["v1.0", "v1.2"]
38+
assert project_config.associated_products[0].name == "test-name"
39+
assert project_config.associated_products[0].versions == ["v1.0", "v1.2"]
4040
assert project_config.name == "test_data"
4141

4242

snooty/types.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
UnmarshallingError,
1919
)
2020
from .flutter import LoadError, check_type, checked
21-
from .n import FileId
21+
from .n import FileId, SerializableType
2222

2323
FileSource = Union[Path, str]
2424
PAT_VARIABLE = re.compile(r"{\+([\w-]+)\+}")
@@ -123,6 +123,16 @@ class BundleConfig:
123123
manpages: Optional[str] = field(default=None)
124124

125125

126+
@checked
127+
@dataclass
128+
class AssociatedProduct:
129+
name: str
130+
versions: List[str]
131+
132+
def serialize(self) -> SerializableType:
133+
return {"name": self.name, "versions": self.versions}
134+
135+
126136
@checked
127137
@dataclass
128138
class ProjectConfig:
@@ -148,7 +158,7 @@ class ProjectConfig:
148158
manpages: Dict[str, ManPageConfig] = field(default_factory=dict)
149159
bundle: BundleConfig = field(default_factory=BundleConfig)
150160
data: Dict[str, object] = field(default_factory=dict)
151-
associated_products: List[Dict[str, object]] = field(default_factory=list)
161+
associated_products: List[AssociatedProduct] = field(default_factory=list)
152162

153163
@property
154164
def source_path(self) -> Path:

0 commit comments

Comments
 (0)