-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path_validators.py
More file actions
295 lines (255 loc) · 8.73 KB
/
_validators.py
File metadata and controls
295 lines (255 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
# SPDX-License-Identifier: MPL-2.0
# Copyright (c) 2026 Den Rozhnovskiy
from __future__ import annotations
from collections.abc import Mapping, Sequence
from typing import TypeGuard
from .entries import (
ApiParamSpecDict,
BlockDict,
ClassMetricsDict,
DeadCandidateDict,
FileStat,
ModuleApiSurfaceDict,
ModuleDepDict,
ModuleDocstringCoverageDict,
ModuleTypingCoverageDict,
PublicSymbolDict,
RuntimeReachabilityFactDict,
SecuritySurfaceDict,
SegmentDict,
SourceStatsDict,
UnitDict,
)
def _is_file_stat_dict(value: object) -> TypeGuard[FileStat]:
if not isinstance(value, dict):
return False
return isinstance(value.get("mtime_ns"), int) and isinstance(value.get("size"), int)
def _is_source_stats_dict(value: object) -> TypeGuard[SourceStatsDict]:
if not isinstance(value, dict):
return False
lines = value.get("lines")
functions = value.get("functions")
methods = value.get("methods")
classes = value.get("classes")
return (
isinstance(lines, int)
and lines >= 0
and isinstance(functions, int)
and functions >= 0
and isinstance(methods, int)
and methods >= 0
and isinstance(classes, int)
and classes >= 0
)
def _is_unit_dict(value: object) -> TypeGuard[UnitDict]:
if not isinstance(value, dict):
return False
string_keys = ("qualname", "filepath", "fingerprint", "loc_bucket")
int_keys = ("start_line", "end_line", "loc", "stmt_count")
if not _has_typed_fields(value, string_keys=string_keys, int_keys=int_keys):
return False
cyclomatic_complexity = value.get("cyclomatic_complexity", 1)
nesting_depth = value.get("nesting_depth", 0)
risk = value.get("risk", "low")
raw_hash = value.get("raw_hash", "")
return (
isinstance(cyclomatic_complexity, int)
and isinstance(nesting_depth, int)
and isinstance(risk, str)
and risk in {"low", "medium", "high"}
and isinstance(raw_hash, str)
and isinstance(value.get("entry_guard_count", 0), int)
and isinstance(value.get("entry_guard_terminal_profile", "none"), str)
and isinstance(value.get("entry_guard_has_side_effect_before", False), bool)
and isinstance(value.get("terminal_kind", "fallthrough"), str)
and isinstance(value.get("try_finally_profile", "none"), str)
and isinstance(value.get("side_effect_order_profile", "none"), str)
)
def _is_block_dict(value: object) -> TypeGuard[BlockDict]:
if not isinstance(value, dict):
return False
string_keys = ("block_hash", "filepath", "qualname")
int_keys = ("start_line", "end_line", "size")
return _has_typed_fields(value, string_keys=string_keys, int_keys=int_keys)
def _is_segment_dict(value: object) -> TypeGuard[SegmentDict]:
if not isinstance(value, dict):
return False
string_keys = ("segment_hash", "segment_sig", "filepath", "qualname")
int_keys = ("start_line", "end_line", "size")
return _has_typed_fields(value, string_keys=string_keys, int_keys=int_keys)
def _is_module_typing_coverage_dict(
value: object,
) -> TypeGuard[ModuleTypingCoverageDict]:
if not isinstance(value, dict):
return False
string_keys = ("module", "filepath")
int_keys = (
"callable_count",
"params_total",
"params_annotated",
"returns_total",
"returns_annotated",
"any_annotation_count",
)
return _has_typed_fields(value, string_keys=string_keys, int_keys=int_keys)
def _is_module_docstring_coverage_dict(
value: object,
) -> TypeGuard[ModuleDocstringCoverageDict]:
if not isinstance(value, dict):
return False
string_keys = ("module", "filepath")
int_keys = ("public_symbol_total", "public_symbol_documented")
return _has_typed_fields(value, string_keys=string_keys, int_keys=int_keys)
def _is_api_param_spec_dict(value: object) -> TypeGuard[ApiParamSpecDict]:
if not isinstance(value, dict):
return False
return (
isinstance(value.get("name"), str)
and isinstance(value.get("kind"), str)
and isinstance(value.get("has_default"), bool)
and isinstance(value.get("annotation_hash", ""), str)
)
def _is_public_symbol_dict(value: object) -> TypeGuard[PublicSymbolDict]:
if not isinstance(value, dict):
return False
if not _has_typed_fields(
value,
string_keys=("qualname", "kind", "exported_via"),
int_keys=("start_line", "end_line"),
):
return False
params = value.get("params", [])
return (
isinstance(value.get("returns_hash", ""), str)
and isinstance(params, list)
and all(_is_api_param_spec_dict(item) for item in params)
)
def _is_module_api_surface_dict(value: object) -> TypeGuard[ModuleApiSurfaceDict]:
if not isinstance(value, dict):
return False
all_declared = value.get("all_declared", [])
symbols = value.get("symbols", [])
return (
isinstance(value.get("module"), str)
and isinstance(value.get("filepath"), str)
and _is_string_list(all_declared)
and isinstance(symbols, list)
and all(_is_public_symbol_dict(item) for item in symbols)
)
def _is_class_metrics_dict(value: object) -> TypeGuard[ClassMetricsDict]:
if not isinstance(value, dict):
return False
if not _has_typed_fields(
value,
string_keys=(
"qualname",
"filepath",
"risk_coupling",
"risk_cohesion",
),
int_keys=(
"start_line",
"end_line",
"cbo",
"lcom4",
"method_count",
"instance_var_count",
),
):
return False
coupled_classes = value.get("coupled_classes")
if coupled_classes is None:
return True
return _is_string_list(coupled_classes)
def _is_module_dep_dict(value: object) -> TypeGuard[ModuleDepDict]:
if not isinstance(value, dict):
return False
return _has_typed_fields(
value,
string_keys=("source", "target", "import_type"),
int_keys=("line",),
)
def _is_dead_candidate_dict(value: object) -> TypeGuard[DeadCandidateDict]:
if not isinstance(value, dict):
return False
if not _has_typed_fields(
value,
string_keys=("qualname", "local_name", "filepath", "kind"),
int_keys=("start_line", "end_line"),
):
return False
suppressed_rules = value.get("suppressed_rules")
if suppressed_rules is None:
return True
return _is_string_list(suppressed_rules)
def _is_security_surface_dict(value: object) -> TypeGuard[SecuritySurfaceDict]:
if not isinstance(value, dict):
return False
return _has_typed_fields(
value,
string_keys=(
"category",
"capability",
"module",
"filepath",
"qualname",
"location_scope",
"classification_mode",
"evidence_kind",
"evidence_symbol",
),
int_keys=("start_line", "end_line"),
)
def _is_runtime_reachability_fact_dict(
value: object,
) -> TypeGuard[RuntimeReachabilityFactDict]:
if not isinstance(value, dict):
return False
return _has_typed_fields(
value,
string_keys=(
"target_qualname",
"filepath",
"target_kind",
"framework",
"edge_kind",
"confidence",
"evidence",
"evidence_symbol",
"source_qualname",
),
int_keys=("start_line", "end_line"),
)
def _is_string_list(value: object) -> TypeGuard[list[str]]:
return isinstance(value, list) and all(isinstance(item, str) for item in value)
def _has_typed_fields(
value: Mapping[str, object],
*,
string_keys: Sequence[str],
int_keys: Sequence[str],
) -> bool:
return all(isinstance(value.get(key), str) for key in string_keys) and all(
isinstance(value.get(key), int) for key in int_keys
)
__all__ = [
"_has_typed_fields",
"_is_api_param_spec_dict",
"_is_block_dict",
"_is_class_metrics_dict",
"_is_dead_candidate_dict",
"_is_file_stat_dict",
"_is_module_api_surface_dict",
"_is_module_dep_dict",
"_is_module_docstring_coverage_dict",
"_is_module_typing_coverage_dict",
"_is_public_symbol_dict",
"_is_runtime_reachability_fact_dict",
"_is_security_surface_dict",
"_is_segment_dict",
"_is_source_stats_dict",
"_is_string_list",
"_is_unit_dict",
]