pycforge 0.15.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pycforge/__init__.py +8 -0
- pycforge/__main__.py +2 -0
- pycforge/_version.py +1 -0
- pycforge/converter/__init__.py +0 -0
- pycforge/converter/analysis/__init__.py +5 -0
- pycforge/converter/analysis/functions.py +723 -0
- pycforge/converter/analysis/model.py +158 -0
- pycforge/converter/analysis/planning.py +464 -0
- pycforge/converter/analysis/stage.py +1976 -0
- pycforge/converter/analysis/symbols.py +237 -0
- pycforge/converter/analysis/validation.py +2166 -0
- pycforge/converter/c_output/__init__.py +2 -0
- pycforge/converter/c_output/conformance.py +297 -0
- pycforge/converter/c_output/renderer.py +220 -0
- pycforge/converter/conditional_regions/__init__.py +71 -0
- pycforge/converter/conditional_regions/analysis.py +481 -0
- pycforge/converter/conditional_regions/lowering.py +579 -0
- pycforge/converter/conditional_regions/model.py +243 -0
- pycforge/converter/conditional_regions/validation.py +690 -0
- pycforge/converter/containers/__init__.py +19 -0
- pycforge/converter/containers/analysis.py +542 -0
- pycforge/converter/containers/lowering.py +290 -0
- pycforge/converter/containers/model.py +146 -0
- pycforge/converter/contracts/__init__.py +0 -0
- pycforge/converter/contracts/configuration.py +178 -0
- pycforge/converter/contracts/identifiers.py +68 -0
- pycforge/converter/contracts/versions.py +71 -0
- pycforge/converter/core/__init__.py +0 -0
- pycforge/converter/core/artifact_io.py +87 -0
- pycforge/converter/core/cancellation.py +7 -0
- pycforge/converter/core/canonicalization.py +254 -0
- pycforge/converter/core/context.py +13 -0
- pycforge/converter/core/diagnostics.py +95 -0
- pycforge/converter/core/enums.py +23 -0
- pycforge/converter/core/fingerprint.py +22 -0
- pycforge/converter/core/pipeline.py +8 -0
- pycforge/converter/core/progress.py +25 -0
- pycforge/converter/core/request.py +50 -0
- pycforge/converter/core/resource_policy.py +31 -0
- pycforge/converter/core/result.py +23 -0
- pycforge/converter/core/serialization.py +36 -0
- pycforge/converter/core/stage.py +10 -0
- pycforge/converter/core/stage_artifact.py +80 -0
- pycforge/converter/core/stage_outcome.py +15 -0
- pycforge/converter/decision_trace/__init__.py +0 -0
- pycforge/converter/decision_trace/recorder.py +52 -0
- pycforge/converter/facade.py +459 -0
- pycforge/converter/frontend/__init__.py +4 -0
- pycforge/converter/frontend/normalizer.py +59 -0
- pycforge/converter/frontend/parser.py +20 -0
- pycforge/converter/frontend/source_document.py +93 -0
- pycforge/converter/frontend/stages.py +518 -0
- pycforge/converter/frontend/validation.py +76 -0
- pycforge/converter/io/__init__.py +0 -0
- pycforge/converter/io/atomic_writer.py +33 -0
- pycforge/converter/ir/__init__.py +1 -0
- pycforge/converter/ir/c_ir/__init__.py +4 -0
- pycforge/converter/ir/c_ir/builder.py +26 -0
- pycforge/converter/ir/c_ir/model.py +318 -0
- pycforge/converter/ir/c_ir/serialization.py +88 -0
- pycforge/converter/ir/c_ir/validation.py +834 -0
- pycforge/converter/ir/python_ir/__init__.py +17 -0
- pycforge/converter/ir/python_ir/nodes.py +86 -0
- pycforge/converter/keyword_calls/__init__.py +68 -0
- pycforge/converter/keyword_calls/analysis.py +458 -0
- pycforge/converter/keyword_calls/lowering.py +271 -0
- pycforge/converter/keyword_calls/model.py +304 -0
- pycforge/converter/keyword_calls/validation.py +802 -0
- pycforge/converter/keyword_only_calls/__init__.py +68 -0
- pycforge/converter/keyword_only_calls/analysis.py +640 -0
- pycforge/converter/keyword_only_calls/lowering.py +376 -0
- pycforge/converter/keyword_only_calls/model.py +328 -0
- pycforge/converter/keyword_only_calls/validation.py +1357 -0
- pycforge/converter/lowering.py +1000 -0
- pycforge/converter/modules/__init__.py +13 -0
- pycforge/converter/modules/analysis.py +1250 -0
- pycforge/converter/modules/lowering.py +200 -0
- pycforge/converter/modules/model.py +73 -0
- pycforge/converter/modules/stage.py +192 -0
- pycforge/converter/numeric_semantics/__init__.py +23 -0
- pycforge/converter/numeric_semantics/analysis.py +420 -0
- pycforge/converter/numeric_semantics/lowering.py +153 -0
- pycforge/converter/numeric_semantics/model.py +123 -0
- pycforge/converter/records/__init__.py +34 -0
- pycforge/converter/records/analysis.py +1394 -0
- pycforge/converter/records/lowering.py +393 -0
- pycforge/converter/records/model.py +319 -0
- pycforge/converter/rules/__init__.py +3 -0
- pycforge/converter/support_templates/__init__.py +49 -0
- pycforge/converter/support_templates/assembly.py +106 -0
- pycforge/converter/support_templates/factories.py +348 -0
- pycforge/converter/support_templates/model.py +159 -0
- pycforge/converter/support_templates/registry.py +388 -0
- pycforge/converter/telemetry/__init__.py +0 -0
- pycforge/converter/telemetry/sink.py +39 -0
- pycforge/ide/__init__.py +19 -0
- pycforge/ide/__main__.py +5 -0
- pycforge/ide/_worker_event_codec.py +276 -0
- pycforge/ide/_worker_protocol_json.py +197 -0
- pycforge/ide/_worker_protocol_types.py +90 -0
- pycforge/ide/_worker_request_codec.py +200 -0
- pycforge/ide/_worker_request_data.py +260 -0
- pycforge/ide/_worker_result_data.py +343 -0
- pycforge/ide/action_contract.py +469 -0
- pycforge/ide/action_validation.py +229 -0
- pycforge/ide/command_palette.py +223 -0
- pycforge/ide/controller.py +904 -0
- pycforge/ide/controller_conversion.py +546 -0
- pycforge/ide/controller_io.py +205 -0
- pycforge/ide/editor.py +951 -0
- pycforge/ide/editor_commands_qt.py +274 -0
- pycforge/ide/editor_lexical.py +43 -0
- pycforge/ide/editor_sidebars.py +236 -0
- pycforge/ide/editor_syntax.py +243 -0
- pycforge/ide/find_replace.py +535 -0
- pycforge/ide/icons.py +91 -0
- pycforge/ide/io_service.py +196 -0
- pycforge/ide/model.py +164 -0
- pycforge/ide/panels.py +903 -0
- pycforge/ide/positions.py +220 -0
- pycforge/ide/process_worker.py +130 -0
- pycforge/ide/qt.py +230 -0
- pycforge/ide/qt_actions.py +418 -0
- pycforge/ide/qt_close.py +169 -0
- pycforge/ide/qt_command_palette.py +297 -0
- pycforge/ide/qt_contract.py +220 -0
- pycforge/ide/qt_documents.py +590 -0
- pycforge/ide/qt_editor_buffers.py +113 -0
- pycforge/ide/qt_editor_surfaces.py +112 -0
- pycforge/ide/qt_menus.py +324 -0
- pycforge/ide/qt_projection.py +500 -0
- pycforge/ide/qt_shell.py +348 -0
- pycforge/ide/qt_shell_interactions.py +320 -0
- pycforge/ide/qt_state.py +393 -0
- pycforge/ide/qt_workspace_features.py +398 -0
- pycforge/ide/qt_workspace_navigation.py +208 -0
- pycforge/ide/qt_workspace_observers.py +173 -0
- pycforge/ide/qt_workspace_panels.py +545 -0
- pycforge/ide/qt_workspace_widgets.py +228 -0
- pycforge/ide/resources/icons/about.svg +1 -0
- pycforge/ide/resources/icons/add-document.svg +1 -0
- pycforge/ide/resources/icons/brand-mark.svg +1 -0
- pycforge/ide/resources/icons/bundle-search.svg +1 -0
- pycforge/ide/resources/icons/cancel.svg +1 -0
- pycforge/ide/resources/icons/check.svg +1 -0
- pycforge/ide/resources/icons/chevron-right.svg +1 -0
- pycforge/ide/resources/icons/close.svg +1 -0
- pycforge/ide/resources/icons/collapse-all.svg +1 -0
- pycforge/ide/resources/icons/command-palette.svg +1 -0
- pycforge/ide/resources/icons/convert.svg +1 -0
- pycforge/ide/resources/icons/copy.svg +1 -0
- pycforge/ide/resources/icons/cut.svg +1 -0
- pycforge/ide/resources/icons/decision-trace.svg +1 -0
- pycforge/ide/resources/icons/details.svg +1 -0
- pycforge/ide/resources/icons/diagnostics.svg +1 -0
- pycforge/ide/resources/icons/duplicate-line.svg +1 -0
- pycforge/ide/resources/icons/exit.svg +1 -0
- pycforge/ide/resources/icons/expand-all.svg +1 -0
- pycforge/ide/resources/icons/export.svg +1 -0
- pycforge/ide/resources/icons/find.svg +1 -0
- pycforge/ide/resources/icons/go-to-line.svg +1 -0
- pycforge/ide/resources/icons/go-to-output.svg +1 -0
- pycforge/ide/resources/icons/go-to-source.svg +1 -0
- pycforge/ide/resources/icons/history.svg +1 -0
- pycforge/ide/resources/icons/indent.svg +1 -0
- pycforge/ide/resources/icons/link-c.svg +1 -0
- pycforge/ide/resources/icons/mappings.svg +1 -0
- pycforge/ide/resources/icons/module.svg +1 -0
- pycforge/ide/resources/icons/move-down.svg +1 -0
- pycforge/ide/resources/icons/move-line-down.svg +1 -0
- pycforge/ide/resources/icons/move-line-up.svg +1 -0
- pycforge/ide/resources/icons/move-up.svg +1 -0
- pycforge/ide/resources/icons/next.svg +1 -0
- pycforge/ide/resources/icons/open.svg +1 -0
- pycforge/ide/resources/icons/outdent.svg +1 -0
- pycforge/ide/resources/icons/outline.svg +1 -0
- pycforge/ide/resources/icons/paste.svg +1 -0
- pycforge/ide/resources/icons/previous.svg +1 -0
- pycforge/ide/resources/icons/primary-module.svg +1 -0
- pycforge/ide/resources/icons/redo.svg +1 -0
- pycforge/ide/resources/icons/remove-document.svg +1 -0
- pycforge/ide/resources/icons/replace.svg +1 -0
- pycforge/ide/resources/icons/save-as.svg +1 -0
- pycforge/ide/resources/icons/save.svg +1 -0
- pycforge/ide/resources/icons/select-all.svg +1 -0
- pycforge/ide/resources/icons/settings.svg +1 -0
- pycforge/ide/resources/icons/show-c.svg +1 -0
- pycforge/ide/resources/icons/split-view.svg +1 -0
- pycforge/ide/resources/icons/summary.svg +1 -0
- pycforge/ide/resources/icons/telemetry.svg +1 -0
- pycforge/ide/resources/icons/toggle-comment.svg +1 -0
- pycforge/ide/resources/icons/toggle-fold.svg +1 -0
- pycforge/ide/resources/icons/undo.svg +1 -0
- pycforge/ide/resources/icons/whitespace.svg +1 -0
- pycforge/ide/revisions.py +400 -0
- pycforge/ide/search_service.py +280 -0
- pycforge/ide/session_history.py +150 -0
- pycforge/ide/source_editing.py +417 -0
- pycforge/ide/source_structure.py +558 -0
- pycforge/ide/source_structure_async.py +191 -0
- pycforge/ide/supervisor.py +503 -0
- pycforge/ide/theme.py +119 -0
- pycforge/ide/theme_stylesheet.py +598 -0
- pycforge/ide/theme_workspace_stylesheet.py +155 -0
- pycforge/ide/visual_tokens.py +142 -0
- pycforge/ide/worker_protocol.py +80 -0
- pycforge/ide/workspace_search.py +462 -0
- pycforge/ide/workspace_session.py +256 -0
- pycforge/laboratory/__init__.py +6 -0
- pycforge/laboratory/audits.py +2244 -0
- pycforge/laboratory/checkpoint_e.py +2685 -0
- pycforge/laboratory/cli.py +119 -0
- pycforge/laboratory/exit_codes.py +11 -0
- pycforge/laboratory/keyword_audit.py +661 -0
- pycforge/laboratory/keyword_only_audit.py +481 -0
- pycforge-0.15.2.dist-info/METADATA +209 -0
- pycforge-0.15.2.dist-info/RECORD +221 -0
- pycforge-0.15.2.dist-info/WHEEL +5 -0
- pycforge-0.15.2.dist-info/entry_points.txt +5 -0
- pycforge-0.15.2.dist-info/licenses/LICENSE +674 -0
- pycforge-0.15.2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,640 @@
|
|
|
1
|
+
"""Closed deterministic analysis for required keyword-only direct calls."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any, Mapping, Sequence
|
|
6
|
+
|
|
7
|
+
from pycforge.converter.analysis.model import ValueCategory
|
|
8
|
+
|
|
9
|
+
from .model import (
|
|
10
|
+
KEYWORD_ONLY_CALL_LOWERING_SHAPE,
|
|
11
|
+
KeywordOnlyArgumentBindingFact,
|
|
12
|
+
KeywordOnlyCallAnalysis,
|
|
13
|
+
KeywordOnlyCallAnalysisCanceled,
|
|
14
|
+
KeywordOnlyCallAnalysisError,
|
|
15
|
+
KeywordOnlyCallBindingFact,
|
|
16
|
+
KeywordOnlyCallRejection,
|
|
17
|
+
keyword_only_call_binding_id,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _sequence(value: Any) -> bool:
|
|
22
|
+
return isinstance(value, (list, tuple))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _value(item: Any, name: str, default: Any = None) -> Any:
|
|
26
|
+
if isinstance(item, Mapping):
|
|
27
|
+
return item.get(name, default)
|
|
28
|
+
return getattr(item, name, default)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _category(value: ValueCategory | str | None) -> str:
|
|
32
|
+
return value.value if isinstance(value, ValueCategory) else str(value or "unknown")
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _source_span(item: Any) -> dict[str, object] | None:
|
|
36
|
+
if not isinstance(item, Mapping):
|
|
37
|
+
return None
|
|
38
|
+
provenance = item.get("provenance")
|
|
39
|
+
span = provenance.get("source_span") if isinstance(provenance, Mapping) else None
|
|
40
|
+
return span if isinstance(span, dict) else None
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class KeywordOnlyCallAnalyzer:
|
|
44
|
+
"""Classify direct calls to exact required-keyword-only signatures."""
|
|
45
|
+
|
|
46
|
+
def __init__(
|
|
47
|
+
self,
|
|
48
|
+
module: dict[str, Any],
|
|
49
|
+
*,
|
|
50
|
+
bindings: Sequence[Any],
|
|
51
|
+
signatures: Sequence[Any],
|
|
52
|
+
categories: Mapping[str, ValueCategory | str],
|
|
53
|
+
cancellation: Any,
|
|
54
|
+
ignored_call_node_ids: frozenset[str] = frozenset(),
|
|
55
|
+
) -> None:
|
|
56
|
+
if not isinstance(module, Mapping):
|
|
57
|
+
raise KeywordOnlyCallAnalysisError(
|
|
58
|
+
"PYC2912",
|
|
59
|
+
"Keyword-only call analysis requires a normalized module mapping",
|
|
60
|
+
"",
|
|
61
|
+
None,
|
|
62
|
+
)
|
|
63
|
+
self.module = module
|
|
64
|
+
self.cancellation = cancellation
|
|
65
|
+
self.ignored_call_node_ids = ignored_call_node_ids
|
|
66
|
+
self.nodes: dict[str, dict[str, Any]] = {}
|
|
67
|
+
self.node_order: dict[str, int] = {}
|
|
68
|
+
node_values = module.get("nodes")
|
|
69
|
+
if not _sequence(node_values):
|
|
70
|
+
self._malformed(
|
|
71
|
+
"Keyword-only call analysis requires a normalized node sequence",
|
|
72
|
+
module,
|
|
73
|
+
)
|
|
74
|
+
self.node_values = tuple(node_values)
|
|
75
|
+
for ordinal, node in enumerate(self.node_values):
|
|
76
|
+
self._check_cancellation()
|
|
77
|
+
node_id = node.get("node_id") if isinstance(node, Mapping) else None
|
|
78
|
+
if not isinstance(node_id, str) or not node_id or node_id in self.nodes:
|
|
79
|
+
raise KeywordOnlyCallAnalysisError(
|
|
80
|
+
"PYC2912",
|
|
81
|
+
"Keyword-only analysis requires unique normalized node identities",
|
|
82
|
+
str(node_id or module.get("root_node_id") or ""),
|
|
83
|
+
_source_span(node),
|
|
84
|
+
)
|
|
85
|
+
self.nodes[node_id] = node
|
|
86
|
+
self.node_order[node_id] = ordinal
|
|
87
|
+
if not isinstance(categories, Mapping):
|
|
88
|
+
self._malformed("Keyword-only call categories are malformed", module)
|
|
89
|
+
self.categories = {key: _category(value) for key, value in categories.items()}
|
|
90
|
+
if any(not isinstance(key, str) for key in self.categories):
|
|
91
|
+
self._malformed("Keyword-only category identities are malformed", module)
|
|
92
|
+
if not _sequence(bindings):
|
|
93
|
+
self._malformed("Keyword-only binding evidence is malformed", module)
|
|
94
|
+
self.binding_by_occurrence: dict[str, Any] = {}
|
|
95
|
+
for binding in bindings:
|
|
96
|
+
self._check_cancellation()
|
|
97
|
+
binding_id = _value(binding, "binding_id")
|
|
98
|
+
occurrences = _value(binding, "occurrence_node_ids")
|
|
99
|
+
if (
|
|
100
|
+
not isinstance(binding_id, str)
|
|
101
|
+
or not binding_id
|
|
102
|
+
or not _sequence(occurrences)
|
|
103
|
+
or any(not isinstance(item, str) for item in occurrences)
|
|
104
|
+
):
|
|
105
|
+
self._malformed("Keyword-only binding evidence is malformed", module)
|
|
106
|
+
for occurrence in occurrences:
|
|
107
|
+
existing = self.binding_by_occurrence.get(occurrence)
|
|
108
|
+
if existing is not None and _value(existing, "binding_id") != binding_id:
|
|
109
|
+
self._malformed(
|
|
110
|
+
"Keyword-only occurrence has conflicting binding evidence",
|
|
111
|
+
self.nodes.get(occurrence, module),
|
|
112
|
+
)
|
|
113
|
+
self.binding_by_occurrence[occurrence] = binding
|
|
114
|
+
if not _sequence(signatures):
|
|
115
|
+
self._malformed("Keyword-only signature evidence is malformed", module)
|
|
116
|
+
self.signature_by_binding: dict[str, Any] = {}
|
|
117
|
+
for signature in signatures:
|
|
118
|
+
self._check_cancellation()
|
|
119
|
+
binding_id = _value(signature, "binding_id")
|
|
120
|
+
parameters = _value(signature, "parameters")
|
|
121
|
+
if (
|
|
122
|
+
not isinstance(binding_id, str)
|
|
123
|
+
or not binding_id
|
|
124
|
+
or not _sequence(parameters)
|
|
125
|
+
or binding_id in self.signature_by_binding
|
|
126
|
+
):
|
|
127
|
+
self._malformed("Keyword-only signature evidence is malformed", module)
|
|
128
|
+
self.signature_by_binding[binding_id] = signature
|
|
129
|
+
|
|
130
|
+
def analyze(self) -> KeywordOnlyCallAnalysis:
|
|
131
|
+
facts: list[KeywordOnlyCallBindingFact] = []
|
|
132
|
+
rejections: list[KeywordOnlyCallRejection] = []
|
|
133
|
+
for node in self.node_values:
|
|
134
|
+
self._check_cancellation()
|
|
135
|
+
if not self._candidate(node):
|
|
136
|
+
continue
|
|
137
|
+
fact = self._fact(node)
|
|
138
|
+
facts.append(fact)
|
|
139
|
+
if not fact.supported:
|
|
140
|
+
rejection_node = self.nodes.get(
|
|
141
|
+
fact.rejection_node_id or fact.call_node_id,
|
|
142
|
+
node,
|
|
143
|
+
)
|
|
144
|
+
rejections.append(
|
|
145
|
+
KeywordOnlyCallRejection(
|
|
146
|
+
fact.call_node_id,
|
|
147
|
+
fact.diagnostic_code or "PYC2910",
|
|
148
|
+
fact.reason or "Unsupported required keyword-only call",
|
|
149
|
+
fact.rejection_node_id or fact.call_node_id,
|
|
150
|
+
_source_span(rejection_node),
|
|
151
|
+
)
|
|
152
|
+
)
|
|
153
|
+
return KeywordOnlyCallAnalysis(tuple(facts), tuple(rejections))
|
|
154
|
+
|
|
155
|
+
def _candidate(self, node: dict[str, Any]) -> bool:
|
|
156
|
+
if (
|
|
157
|
+
node.get("kind") != "Call"
|
|
158
|
+
or node.get("node_id") in self.ignored_call_node_ids
|
|
159
|
+
):
|
|
160
|
+
return False
|
|
161
|
+
fields = node.get("fields")
|
|
162
|
+
if (
|
|
163
|
+
not isinstance(fields, Mapping)
|
|
164
|
+
or not _sequence(fields.get("args"))
|
|
165
|
+
or not _sequence(fields.get("keywords"))
|
|
166
|
+
):
|
|
167
|
+
self._malformed("Call has malformed normalized argument evidence", node)
|
|
168
|
+
callee_id = fields.get("func")
|
|
169
|
+
if not isinstance(callee_id, str):
|
|
170
|
+
self._malformed("Call callee identity is malformed", node)
|
|
171
|
+
callee = self.nodes.get(callee_id)
|
|
172
|
+
if not callee or callee.get("kind") != "Name":
|
|
173
|
+
return False
|
|
174
|
+
binding = self.binding_by_occurrence.get(callee_id)
|
|
175
|
+
signature = self.signature_by_binding.get(_value(binding, "binding_id"))
|
|
176
|
+
if signature is None or _value(signature, "eligible") is not True:
|
|
177
|
+
return False
|
|
178
|
+
target = self.nodes.get(str(_value(signature, "function_node_id", "")))
|
|
179
|
+
if not target or target.get("kind") != "FunctionDef":
|
|
180
|
+
return False
|
|
181
|
+
target_fields = target.get("fields")
|
|
182
|
+
args = (
|
|
183
|
+
self.nodes.get(target_fields.get("args"))
|
|
184
|
+
if isinstance(target_fields, Mapping)
|
|
185
|
+
else None
|
|
186
|
+
)
|
|
187
|
+
argument_fields = args.get("fields") if isinstance(args, Mapping) else None
|
|
188
|
+
return bool(
|
|
189
|
+
isinstance(argument_fields, Mapping)
|
|
190
|
+
and _sequence(argument_fields.get("kwonlyargs"))
|
|
191
|
+
and len(argument_fields["kwonlyargs"]) > 0
|
|
192
|
+
and _sequence(argument_fields.get("kw_defaults"))
|
|
193
|
+
and len(argument_fields["kw_defaults"])
|
|
194
|
+
== len(argument_fields["kwonlyargs"])
|
|
195
|
+
and all(item is None for item in argument_fields["kw_defaults"])
|
|
196
|
+
and _sequence(argument_fields.get("defaults"))
|
|
197
|
+
and not argument_fields["defaults"]
|
|
198
|
+
and not argument_fields.get("vararg")
|
|
199
|
+
and not argument_fields.get("kwarg")
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
def _fact(self, call: dict[str, Any]) -> KeywordOnlyCallBindingFact:
|
|
203
|
+
self._check_cancellation()
|
|
204
|
+
fields = call.get("fields")
|
|
205
|
+
if not isinstance(fields, Mapping):
|
|
206
|
+
self._malformed("Call fields are malformed", call)
|
|
207
|
+
call_id = call["node_id"]
|
|
208
|
+
callee_id = fields.get("func")
|
|
209
|
+
if not isinstance(callee_id, str):
|
|
210
|
+
self._malformed("Call callee identity is malformed", call)
|
|
211
|
+
binding = self.binding_by_occurrence[callee_id]
|
|
212
|
+
target_binding_id = str(_value(binding, "binding_id"))
|
|
213
|
+
signature = self.signature_by_binding[target_binding_id]
|
|
214
|
+
target_function_id = str(_value(signature, "function_node_id"))
|
|
215
|
+
target = self.nodes.get(target_function_id)
|
|
216
|
+
if not target or target.get("kind") != "FunctionDef":
|
|
217
|
+
self._error(
|
|
218
|
+
"PYC2912",
|
|
219
|
+
"Exact keyword-only binding requires the normalized target function",
|
|
220
|
+
call,
|
|
221
|
+
)
|
|
222
|
+
target_fields = target.get("fields")
|
|
223
|
+
if not isinstance(target_fields, Mapping):
|
|
224
|
+
self._malformed("Target function fields are malformed", target)
|
|
225
|
+
arguments_node = self.nodes.get(target_fields.get("args"))
|
|
226
|
+
argument_fields = (
|
|
227
|
+
arguments_node.get("fields")
|
|
228
|
+
if isinstance(arguments_node, Mapping)
|
|
229
|
+
else None
|
|
230
|
+
)
|
|
231
|
+
if not isinstance(argument_fields, Mapping):
|
|
232
|
+
self._error(
|
|
233
|
+
"PYC2912",
|
|
234
|
+
"Exact keyword-only binding requires normalized parameter evidence",
|
|
235
|
+
target,
|
|
236
|
+
)
|
|
237
|
+
for name in (
|
|
238
|
+
"posonlyargs",
|
|
239
|
+
"args",
|
|
240
|
+
"kwonlyargs",
|
|
241
|
+
"kw_defaults",
|
|
242
|
+
"defaults",
|
|
243
|
+
):
|
|
244
|
+
if not _sequence(argument_fields.get(name)):
|
|
245
|
+
self._malformed(
|
|
246
|
+
f"Normalized keyword-only {name} evidence is malformed",
|
|
247
|
+
arguments_node,
|
|
248
|
+
)
|
|
249
|
+
posonly_ids = tuple(argument_fields["posonlyargs"])
|
|
250
|
+
positional_or_keyword_ids = tuple(argument_fields["args"])
|
|
251
|
+
keyword_only_ids = tuple(argument_fields["kwonlyargs"])
|
|
252
|
+
if (
|
|
253
|
+
not keyword_only_ids
|
|
254
|
+
or len(argument_fields["kw_defaults"]) != len(keyword_only_ids)
|
|
255
|
+
or any(item is not None for item in argument_fields["kw_defaults"])
|
|
256
|
+
or argument_fields["defaults"]
|
|
257
|
+
or argument_fields.get("vararg")
|
|
258
|
+
or argument_fields.get("kwarg")
|
|
259
|
+
):
|
|
260
|
+
self._error(
|
|
261
|
+
"PYC2911",
|
|
262
|
+
"Keyword-only profile requires one or more required keyword-only "
|
|
263
|
+
"parameters and forbids defaults and variadics",
|
|
264
|
+
target,
|
|
265
|
+
)
|
|
266
|
+
formal_ids = posonly_ids + positional_or_keyword_ids + keyword_only_ids
|
|
267
|
+
parameters_value = _value(signature, "parameters")
|
|
268
|
+
if not _sequence(parameters_value):
|
|
269
|
+
self._malformed("Eligible target signature parameters are malformed", target)
|
|
270
|
+
parameters = tuple(parameters_value)
|
|
271
|
+
parameter_node_ids = tuple(
|
|
272
|
+
str(_value(item, "parameter_node_id", "")) for item in parameters
|
|
273
|
+
)
|
|
274
|
+
parameter_names = tuple(
|
|
275
|
+
str(_value(item, "source_name", "")) for item in parameters
|
|
276
|
+
)
|
|
277
|
+
parameter_categories = tuple(
|
|
278
|
+
_category(_value(item, "category")) for item in parameters
|
|
279
|
+
)
|
|
280
|
+
parameter_kinds = (
|
|
281
|
+
("positional-only",) * len(posonly_ids)
|
|
282
|
+
+ ("positional-or-keyword",) * len(positional_or_keyword_ids)
|
|
283
|
+
+ ("keyword-only",) * len(keyword_only_ids)
|
|
284
|
+
)
|
|
285
|
+
if (
|
|
286
|
+
parameter_node_ids != formal_ids
|
|
287
|
+
or any(not value for value in parameter_names)
|
|
288
|
+
or len(parameter_names) != len(set(parameter_names))
|
|
289
|
+
or any(value not in self.nodes for value in parameter_node_ids)
|
|
290
|
+
):
|
|
291
|
+
self._error(
|
|
292
|
+
"PYC2912",
|
|
293
|
+
"Eligible target signature has incomplete keyword-only evidence",
|
|
294
|
+
target,
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
positional_ids = tuple(fields.get("args", ()))
|
|
298
|
+
keyword_ids = tuple(fields.get("keywords", ()))
|
|
299
|
+
if any(not isinstance(item, str) for item in (*positional_ids, *keyword_ids)):
|
|
300
|
+
self._malformed("Call argument identities are malformed", call)
|
|
301
|
+
keyword_nodes: list[dict[str, Any]] = []
|
|
302
|
+
for keyword_id in keyword_ids:
|
|
303
|
+
self._check_cancellation()
|
|
304
|
+
keyword = self.nodes.get(keyword_id)
|
|
305
|
+
keyword_fields = keyword.get("fields") if isinstance(keyword, Mapping) else None
|
|
306
|
+
if not keyword or keyword.get("kind") != "keyword" or not isinstance(
|
|
307
|
+
keyword_fields,
|
|
308
|
+
Mapping,
|
|
309
|
+
):
|
|
310
|
+
self._error(
|
|
311
|
+
"PYC2912",
|
|
312
|
+
"Call contains malformed normalized keyword evidence",
|
|
313
|
+
call,
|
|
314
|
+
)
|
|
315
|
+
value_id = keyword_fields.get("value")
|
|
316
|
+
if not isinstance(value_id, str) or value_id not in self.nodes:
|
|
317
|
+
self._error(
|
|
318
|
+
"PYC2912",
|
|
319
|
+
"Call keyword has no normalized value expression",
|
|
320
|
+
keyword,
|
|
321
|
+
)
|
|
322
|
+
keyword_nodes.append(keyword)
|
|
323
|
+
|
|
324
|
+
positional_entries: list[dict[str, Any]] = []
|
|
325
|
+
for position, argument_id in enumerate(positional_ids):
|
|
326
|
+
self._check_cancellation()
|
|
327
|
+
if argument_id not in self.nodes:
|
|
328
|
+
self._error("PYC2912", "Call argument node is absent", call)
|
|
329
|
+
positional_entries.append(
|
|
330
|
+
{
|
|
331
|
+
"entry_kind": "positional",
|
|
332
|
+
"list_ordinal": position,
|
|
333
|
+
"source_argument_node_id": argument_id,
|
|
334
|
+
"keyword_node_id": None,
|
|
335
|
+
"keyword_name": None,
|
|
336
|
+
"order_node_id": argument_id,
|
|
337
|
+
}
|
|
338
|
+
)
|
|
339
|
+
keyword_entries = [
|
|
340
|
+
{
|
|
341
|
+
"entry_kind": "keyword",
|
|
342
|
+
"list_ordinal": position,
|
|
343
|
+
"source_argument_node_id": keyword["fields"]["value"],
|
|
344
|
+
"keyword_node_id": keyword["node_id"],
|
|
345
|
+
"keyword_name": keyword["fields"].get("arg"),
|
|
346
|
+
"order_node_id": keyword["node_id"],
|
|
347
|
+
}
|
|
348
|
+
for position, keyword in enumerate(keyword_nodes)
|
|
349
|
+
]
|
|
350
|
+
source_entries = self._merge_source_entries(
|
|
351
|
+
positional_entries,
|
|
352
|
+
keyword_entries,
|
|
353
|
+
)
|
|
354
|
+
for source_ordinal, entry in enumerate(source_entries):
|
|
355
|
+
entry["source_ordinal"] = source_ordinal
|
|
356
|
+
entry_by_position = {
|
|
357
|
+
(item["entry_kind"], item["list_ordinal"]): item
|
|
358
|
+
for item in source_entries
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
positional_capacity = len(posonly_ids) + len(positional_or_keyword_ids)
|
|
362
|
+
parameter_by_name = {
|
|
363
|
+
name: ordinal
|
|
364
|
+
for ordinal, name in enumerate(parameter_names)
|
|
365
|
+
if ordinal >= len(posonly_ids)
|
|
366
|
+
}
|
|
367
|
+
parameter_argument_ids: list[str | None] = [None] * len(parameters)
|
|
368
|
+
parameter_source_ordinals: list[int | None] = [None] * len(parameters)
|
|
369
|
+
source_parameter_ordinals: dict[tuple[str, int], int | None] = {}
|
|
370
|
+
binding_problems: list[tuple[int, str, str]] = []
|
|
371
|
+
unpacking: list[tuple[int, str]] = []
|
|
372
|
+
excess: list[tuple[int, str]] = []
|
|
373
|
+
|
|
374
|
+
saw_starred = False
|
|
375
|
+
for position, argument_id in enumerate(positional_ids):
|
|
376
|
+
self._check_cancellation()
|
|
377
|
+
entry = entry_by_position[("positional", position)]
|
|
378
|
+
argument = self.nodes[argument_id]
|
|
379
|
+
if argument.get("kind") == "Starred":
|
|
380
|
+
saw_starred = True
|
|
381
|
+
source_parameter_ordinals[("positional", position)] = None
|
|
382
|
+
unpacking.append((entry["source_ordinal"], argument_id))
|
|
383
|
+
continue
|
|
384
|
+
if saw_starred:
|
|
385
|
+
source_parameter_ordinals[("positional", position)] = None
|
|
386
|
+
continue
|
|
387
|
+
if position >= positional_capacity:
|
|
388
|
+
source_parameter_ordinals[("positional", position)] = None
|
|
389
|
+
excess.append((entry["source_ordinal"], argument_id))
|
|
390
|
+
continue
|
|
391
|
+
source_parameter_ordinals[("positional", position)] = position
|
|
392
|
+
parameter_argument_ids[position] = argument_id
|
|
393
|
+
parameter_source_ordinals[position] = entry["source_ordinal"]
|
|
394
|
+
|
|
395
|
+
all_parameter_names = {
|
|
396
|
+
name: ordinal for ordinal, name in enumerate(parameter_names)
|
|
397
|
+
}
|
|
398
|
+
for position, keyword in enumerate(keyword_nodes):
|
|
399
|
+
self._check_cancellation()
|
|
400
|
+
entry = entry_by_position[("keyword", position)]
|
|
401
|
+
keyword_id = keyword["node_id"]
|
|
402
|
+
name = keyword["fields"].get("arg")
|
|
403
|
+
if name is None:
|
|
404
|
+
source_parameter_ordinals[("keyword", position)] = None
|
|
405
|
+
unpacking.append((entry["source_ordinal"], keyword_id))
|
|
406
|
+
continue
|
|
407
|
+
parameter_ordinal = parameter_by_name.get(name)
|
|
408
|
+
if parameter_ordinal is None:
|
|
409
|
+
source_parameter_ordinals[("keyword", position)] = None
|
|
410
|
+
reason = (
|
|
411
|
+
f"Positional-only parameter cannot be bound by keyword: {name}"
|
|
412
|
+
if name in all_parameter_names
|
|
413
|
+
else f"Unknown keyword argument: {name}"
|
|
414
|
+
)
|
|
415
|
+
binding_problems.append((entry["source_ordinal"], keyword_id, reason))
|
|
416
|
+
continue
|
|
417
|
+
source_parameter_ordinals[("keyword", position)] = parameter_ordinal
|
|
418
|
+
if parameter_argument_ids[parameter_ordinal] is not None:
|
|
419
|
+
prior_source = parameter_source_ordinals[parameter_ordinal]
|
|
420
|
+
prior_entry = (
|
|
421
|
+
source_entries[prior_source]
|
|
422
|
+
if isinstance(prior_source, int)
|
|
423
|
+
and 0 <= prior_source < len(source_entries)
|
|
424
|
+
else None
|
|
425
|
+
)
|
|
426
|
+
reason = (
|
|
427
|
+
f"Keyword argument duplicates a positionally bound parameter: {name}"
|
|
428
|
+
if prior_entry and prior_entry["entry_kind"] == "positional"
|
|
429
|
+
else f"Duplicate keyword argument: {name}"
|
|
430
|
+
)
|
|
431
|
+
binding_problems.append((entry["source_ordinal"], keyword_id, reason))
|
|
432
|
+
continue
|
|
433
|
+
parameter_argument_ids[parameter_ordinal] = keyword["fields"]["value"]
|
|
434
|
+
parameter_source_ordinals[parameter_ordinal] = entry["source_ordinal"]
|
|
435
|
+
|
|
436
|
+
argument_bindings: list[KeywordOnlyArgumentBindingFact] = []
|
|
437
|
+
for entry in source_entries:
|
|
438
|
+
self._check_cancellation()
|
|
439
|
+
ordinal = source_parameter_ordinals.get(
|
|
440
|
+
(entry["entry_kind"], entry["list_ordinal"])
|
|
441
|
+
)
|
|
442
|
+
valid_ordinal = isinstance(ordinal, int) and 0 <= ordinal < len(parameters)
|
|
443
|
+
argument_id = entry["source_argument_node_id"]
|
|
444
|
+
argument_bindings.append(
|
|
445
|
+
KeywordOnlyArgumentBindingFact(
|
|
446
|
+
argument_id,
|
|
447
|
+
entry["keyword_node_id"],
|
|
448
|
+
entry["keyword_name"],
|
|
449
|
+
entry["source_ordinal"],
|
|
450
|
+
parameter_node_ids[ordinal] if valid_ordinal else None,
|
|
451
|
+
parameter_names[ordinal] if valid_ordinal else None,
|
|
452
|
+
parameter_kinds[ordinal] if valid_ordinal else None,
|
|
453
|
+
ordinal,
|
|
454
|
+
self.categories.get(argument_id, "unknown"),
|
|
455
|
+
parameter_categories[ordinal] if valid_ordinal else None,
|
|
456
|
+
)
|
|
457
|
+
)
|
|
458
|
+
|
|
459
|
+
missing_ordinals = [
|
|
460
|
+
ordinal
|
|
461
|
+
for ordinal, argument_id in enumerate(parameter_argument_ids)
|
|
462
|
+
if argument_id is None
|
|
463
|
+
]
|
|
464
|
+
missing = [parameter_names[ordinal] for ordinal in missing_ordinals]
|
|
465
|
+
mismatches = [
|
|
466
|
+
item
|
|
467
|
+
for item in argument_bindings
|
|
468
|
+
if item.parameter_ordinal is not None
|
|
469
|
+
and item.expected_category is not None
|
|
470
|
+
and item.category != item.expected_category
|
|
471
|
+
and parameter_source_ordinals[item.parameter_ordinal]
|
|
472
|
+
== item.source_ordinal
|
|
473
|
+
]
|
|
474
|
+
keyword_only_coverage_exact = all(
|
|
475
|
+
parameter_argument_ids[ordinal] is not None
|
|
476
|
+
for ordinal in range(positional_capacity, len(parameters))
|
|
477
|
+
)
|
|
478
|
+
parameter_coverage_exact = bool(
|
|
479
|
+
not unpacking
|
|
480
|
+
and not excess
|
|
481
|
+
and not binding_problems
|
|
482
|
+
and not missing
|
|
483
|
+
and all(value is not None for value in parameter_argument_ids)
|
|
484
|
+
and len(source_entries) == len(parameters)
|
|
485
|
+
)
|
|
486
|
+
|
|
487
|
+
code: str | None = None
|
|
488
|
+
reason: str | None = None
|
|
489
|
+
rejection_node_id: str | None = None
|
|
490
|
+
if unpacking:
|
|
491
|
+
_, rejection_node_id = min(unpacking, key=lambda item: item[0])
|
|
492
|
+
code = "PYC2910"
|
|
493
|
+
reason = (
|
|
494
|
+
"Starred and unpacked arguments are unsupported by the exact "
|
|
495
|
+
"required keyword-only profile"
|
|
496
|
+
)
|
|
497
|
+
elif excess:
|
|
498
|
+
_, rejection_node_id = min(excess, key=lambda item: item[0])
|
|
499
|
+
code, reason = "PYC2904", (
|
|
500
|
+
"Positional argument count exceeds the positional portion of "
|
|
501
|
+
"the target signature"
|
|
502
|
+
)
|
|
503
|
+
elif binding_problems:
|
|
504
|
+
_, rejection_node_id, reason = min(
|
|
505
|
+
binding_problems,
|
|
506
|
+
key=lambda item: item[0],
|
|
507
|
+
)
|
|
508
|
+
code = "PYC2912"
|
|
509
|
+
elif missing:
|
|
510
|
+
rejection_node_id = call_id
|
|
511
|
+
code, reason = "PYC2904", (
|
|
512
|
+
"Every target parameter must be supplied exactly once; missing: "
|
|
513
|
+
+ ", ".join(missing)
|
|
514
|
+
)
|
|
515
|
+
elif mismatches:
|
|
516
|
+
mismatch = mismatches[0]
|
|
517
|
+
rejection_node_id = (
|
|
518
|
+
mismatch.keyword_node_id or mismatch.source_argument_node_id
|
|
519
|
+
)
|
|
520
|
+
code, reason = "PYC2905", (
|
|
521
|
+
"Argument representation does not match parameter "
|
|
522
|
+
f"{mismatch.parameter_name}"
|
|
523
|
+
)
|
|
524
|
+
supported = (
|
|
525
|
+
code is None
|
|
526
|
+
and parameter_coverage_exact
|
|
527
|
+
and keyword_only_coverage_exact
|
|
528
|
+
)
|
|
529
|
+
if code is None and not supported:
|
|
530
|
+
code, reason, rejection_node_id = (
|
|
531
|
+
"PYC2904",
|
|
532
|
+
"Arguments do not exactly cover the required keyword-only signature",
|
|
533
|
+
call_id,
|
|
534
|
+
)
|
|
535
|
+
|
|
536
|
+
source_argument_ids = tuple(
|
|
537
|
+
item.source_argument_node_id for item in argument_bindings
|
|
538
|
+
)
|
|
539
|
+
source_categories = tuple(item.category for item in argument_bindings)
|
|
540
|
+
return KeywordOnlyCallBindingFact(
|
|
541
|
+
keyword_only_call_binding_id(call_id, target_binding_id),
|
|
542
|
+
call_id,
|
|
543
|
+
callee_id,
|
|
544
|
+
target_function_id,
|
|
545
|
+
target_binding_id,
|
|
546
|
+
str(_value(signature, "source_name", target_fields.get("name", ""))),
|
|
547
|
+
len(posonly_ids),
|
|
548
|
+
len(positional_or_keyword_ids),
|
|
549
|
+
len(keyword_only_ids),
|
|
550
|
+
parameter_node_ids,
|
|
551
|
+
parameter_names,
|
|
552
|
+
parameter_kinds,
|
|
553
|
+
parameter_categories,
|
|
554
|
+
parameter_node_ids[positional_capacity:],
|
|
555
|
+
parameter_names[positional_capacity:],
|
|
556
|
+
parameter_categories[positional_capacity:],
|
|
557
|
+
positional_ids,
|
|
558
|
+
keyword_ids,
|
|
559
|
+
tuple(item["fields"].get("arg") for item in keyword_nodes),
|
|
560
|
+
tuple(item["fields"]["value"] for item in keyword_nodes),
|
|
561
|
+
tuple(argument_bindings),
|
|
562
|
+
source_argument_ids,
|
|
563
|
+
source_categories,
|
|
564
|
+
tuple(item.parameter_ordinal for item in argument_bindings),
|
|
565
|
+
tuple(parameter_argument_ids),
|
|
566
|
+
tuple(parameter_source_ordinals),
|
|
567
|
+
source_argument_ids,
|
|
568
|
+
True,
|
|
569
|
+
parameter_coverage_exact,
|
|
570
|
+
keyword_only_coverage_exact,
|
|
571
|
+
KEYWORD_ONLY_CALL_LOWERING_SHAPE,
|
|
572
|
+
"none",
|
|
573
|
+
"none",
|
|
574
|
+
"proved-absent" if supported else "compile-time-rejected",
|
|
575
|
+
supported,
|
|
576
|
+
code,
|
|
577
|
+
reason,
|
|
578
|
+
rejection_node_id,
|
|
579
|
+
)
|
|
580
|
+
|
|
581
|
+
def _merge_source_entries(
|
|
582
|
+
self,
|
|
583
|
+
positional: list[dict[str, Any]],
|
|
584
|
+
keywords: list[dict[str, Any]],
|
|
585
|
+
) -> list[dict[str, Any]]:
|
|
586
|
+
merged: list[dict[str, Any]] = []
|
|
587
|
+
positional_index = 0
|
|
588
|
+
keyword_index = 0
|
|
589
|
+
while positional_index < len(positional) and keyword_index < len(keywords):
|
|
590
|
+
self._check_cancellation()
|
|
591
|
+
positional_item = positional[positional_index]
|
|
592
|
+
keyword_item = keywords[keyword_index]
|
|
593
|
+
if self._source_key(positional_item["order_node_id"]) <= self._source_key(
|
|
594
|
+
keyword_item["order_node_id"]
|
|
595
|
+
):
|
|
596
|
+
merged.append(positional_item)
|
|
597
|
+
positional_index += 1
|
|
598
|
+
else:
|
|
599
|
+
merged.append(keyword_item)
|
|
600
|
+
keyword_index += 1
|
|
601
|
+
merged.extend(positional[positional_index:])
|
|
602
|
+
merged.extend(keywords[keyword_index:])
|
|
603
|
+
return merged
|
|
604
|
+
|
|
605
|
+
def _source_key(self, node_id: str) -> tuple[int, int]:
|
|
606
|
+
node = self.nodes.get(node_id, {})
|
|
607
|
+
provenance = node.get("provenance")
|
|
608
|
+
span = provenance.get("source_span") if isinstance(provenance, Mapping) else None
|
|
609
|
+
start = span.get("start") if isinstance(span, Mapping) else None
|
|
610
|
+
offset = start.get("offset") if isinstance(start, Mapping) else None
|
|
611
|
+
return (
|
|
612
|
+
offset if isinstance(offset, int) else 2**63 - 1,
|
|
613
|
+
self.node_order.get(node_id, 2**63 - 1),
|
|
614
|
+
)
|
|
615
|
+
|
|
616
|
+
def _check_cancellation(self) -> None:
|
|
617
|
+
if self.cancellation is not None and bool(
|
|
618
|
+
getattr(self.cancellation, "is_canceled", False)
|
|
619
|
+
):
|
|
620
|
+
raise KeywordOnlyCallAnalysisCanceled
|
|
621
|
+
|
|
622
|
+
def _error(self, code: str, message: str, node: dict[str, Any]) -> None:
|
|
623
|
+
raise KeywordOnlyCallAnalysisError(
|
|
624
|
+
code,
|
|
625
|
+
message,
|
|
626
|
+
str(node.get("node_id", "")),
|
|
627
|
+
_source_span(node),
|
|
628
|
+
)
|
|
629
|
+
|
|
630
|
+
def _malformed(self, message: str, node: Any) -> None:
|
|
631
|
+
value = node if isinstance(node, Mapping) else {}
|
|
632
|
+
raise KeywordOnlyCallAnalysisError(
|
|
633
|
+
"PYC2912",
|
|
634
|
+
message,
|
|
635
|
+
str(value.get("node_id") or self.module.get("root_node_id") or ""),
|
|
636
|
+
_source_span(value),
|
|
637
|
+
)
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
__all__ = ["KeywordOnlyCallAnalyzer"]
|