Repository navigation
refactor(languages): step 4 — resolution rules per pack #601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e0637a8
docs(plan): language-packs step 4 — resolution rules per pack
zaebee c6d4ee8
refactor(languages): cross_file_metadata is declared per manifest
zaebee b4f83a6
feat(languages): the resolution contract (ResolutionView, ResolutionR…
zaebee 962d067
refactor(languages): import reconciliation is each pack's rule
zaebee 2d2399b
refactor(languages): _package_call is the TypeScript pack's after:imp…
zaebee 16a994d
refactor(languages): the JS-builtins fallback is the TypeScript pack's
zaebee b0cf760
refactor(languages): Python's Annotated normalizer and star expansion…
zaebee ed57fd3
docs: step 4 implementation notes; inventory
zaebee e568141
docs(spec): step 4 note says what moved, and what language knowledge …
zaebee 15af381
perf(languages): type_normalizers() is collected once
zaebee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| """What a language pack contributes to resolution (spec: Resolution). | ||
|
|
||
| The engine runs a fixed chain — local type (with type normalizers), import map, | ||
| steps positioned `after:import_map`, global symbols, fallback — and asks the | ||
| source file's pack at each of those points. A rule reads the index through | ||
| `ResolutionView`, never the resolver itself, so `cgis.languages` stays clear of | ||
| `cgis.resolver` (language-packs step 4). | ||
| """ | ||
|
|
||
| from collections.abc import Callable, Mapping | ||
| from dataclasses import dataclass | ||
| from typing import Protocol | ||
|
|
||
| from cgis.core.index import IndexFacts | ||
| from cgis.core.models import Node | ||
|
|
||
| #: The only position the engine runs pack steps at today: after the import map, | ||
| #: before the global symbol index (TS `_package_call`). | ||
| AFTER_IMPORT_MAP = "after:import_map" | ||
| _POSITIONS = frozenset({AFTER_IMPORT_MAP}) | ||
|
|
||
|
|
||
| class ResolutionView(Protocol): | ||
| """What a resolution rule may read; `SymbolIndex` satisfies it.""" | ||
|
|
||
| @property | ||
| def nodes(self) -> Mapping[str, Node]: | ||
| """Every node, by id.""" | ||
| ... | ||
|
|
||
| @property | ||
| def facts(self) -> Mapping[str, IndexFacts]: | ||
| """Each pack's index facts, by pack name.""" | ||
| ... | ||
|
|
||
| def map_to_node_fqn(self, imported_fqn: str) -> str | None: | ||
| """An imported FQN reconciled to a node id, or None.""" | ||
| ... | ||
|
|
||
| def strip_layout_prefixes(self, fqn: str) -> str | None: | ||
| """The node `fqn` names once corroborated layout prefixes are dropped, or None.""" | ||
| ... | ||
|
|
||
| def is_variable_node(self, fqn: str) -> bool: | ||
| """Whether `fqn` is a VARIABLE node.""" | ||
| ... | ||
|
|
||
|
|
||
| TypeNormalizer = Callable[[str, ResolutionView], str] | ||
| CallStep = Callable[[str, str, ResolutionView], str | None] | ||
| Fallback = Callable[[str, str, ResolutionView], tuple[str, float] | None] | ||
| ImportReconciler = Callable[[str, str | None, ResolutionView], str | None] | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class ResolutionRules: | ||
| """One pack's contribution to the resolver's chain.""" | ||
|
|
||
| #: Applied to a declared type before its methods are looked up (e.g. Annotated alias → T). | ||
| type_normalizers: tuple[TypeNormalizer, ...] = () | ||
| #: (position, step): a call step run at that point of the chain. | ||
| steps: tuple[tuple[str, CallStep], ...] = () | ||
| #: When nothing resolved a call: (target, confidence), or None. | ||
| fallback: Fallback | None = None | ||
| #: An import target with no node of that name: the node it means, or None. | ||
| reconcile_import: ImportReconciler | None = None | ||
|
|
||
| def __post_init__(self) -> None: | ||
| """Refuse a step at a position the engine never runs.""" | ||
| unknown = sorted({position for position, _ in self.steps} - _POSITIONS) | ||
| if unknown: | ||
| msg = f"unknown resolution step position(s): {unknown}" | ||
| raise ValueError(msg) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
type_normalizers()function is called on the hot path of resolution (e.g., inside_normalizedinsymbols.py). Currently, it rebuilds the tuple of normalizers on every single call, which can be a performance bottleneck (taking ~4.6 ms on large codebases likeowner-apias noted in the PR description). Since the manifests and rules are static/cached, we can cache the result oftype_normalizers()using@cacheto avoid this overhead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adopted in 15af381:
type_normalizers()is@cached — the packs' rules never change once loaded. Pinned bytest_the_normalizers_are_collected_once(type_normalizers() is type_normalizers()), which failed before the change.