You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit e3e3d99
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: docs/architecture.rst
+37-44Lines changed: 37 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,8 @@ Two-Pass Algorithm
6
6
7
7
Generating executable source requires knowing import aliases before emitting code. But aliases depend on detecting collisions, which requires visiting all types first. This creates a dependency: code generation requires alias resolution, but alias resolution requires traversing the object graph.
8
8
9
-
pycodify solves this with two passes:
9
+
pycodify solves this with two passes, preceded by a path-discovery pass
10
+
(see *Path factoring* below):
10
11
11
12
1. **Collection pass**: Traverse the object, emit code fragments, and collect
12
13
``(module, name)`` import pairs.
@@ -31,12 +32,26 @@ Each type maps to a ``SourceFormatter`` that emits a ``SourceFragment(code, impo
Priority-based dispatch selects the most specific formatter. Domain extensions add formatters without modifying core code.
38
+
Priority-based dispatch selects the most specific formatter. Domain extensions add formatters without modifying core code. A value no formatter accepts raises ``TypeError``: pycodify never emits ``repr`` text it cannot load back.
39
+
40
+
Qualified names
41
+
---------------
42
+
43
+
``imported_name(module, qualname, context)`` is the one rule for spelling an
44
+
importable object. It imports the root of ``__qualname__`` and spells the rest as
45
+
attribute access, so ``Outer.Mode.FAST`` imports ``Outer``. Enum members,
46
+
classes, functions, class-bound methods, named tuples and dataclass constructors
47
+
all use it. Objects defined inside a function raise ``TypeError``.
48
+
49
+
Values that own their spelling
50
+
------------------------------
51
+
52
+
A value whose source is not its constructor subclasses ``PythonSourceLiteral``
53
+
and implements ``source_fragment(context)``, typically building on
54
+
``imported_name`` so collision aliases apply.
40
55
41
56
Core Components
42
57
---------------
@@ -49,7 +64,7 @@ Core Components
49
64
50
65
**FormatContext**
51
66
Carries indentation depth, ``clean_mode``, the ``(module, name)`` to
52
-
local-name mapping, and caller-owned typed extensions. ``indented()`` returns
67
+
local-name mapping, and the pass's ``PathRendering``. ``indented()`` returns
53
68
a copied context and does not mutate its caller.
54
69
55
70
**resolve_imports**
@@ -60,27 +75,17 @@ Core Components
60
75
Orchestrates collection, resolution, and regeneration to produce a complete
61
76
source string.
62
77
63
-
Immutable typed context extensions
64
-
----------------------------------
65
-
66
-
``FormatContext`` is frozen. Its default mappings are read-only mapping proxies,
67
-
and context evolution uses dataclass replacement rather than mutation. A host
68
-
may supply an ``extensions`` mapping keyed by the exact nominal type of each
69
-
rendering policy or state record. A formatter retrieves one value with
70
-
``context.extension(ExtensionType)``; pycodify does not search base classes,
71
-
string keys, or a fallback chain.
72
-
73
-
``generate_python_source(..., context=context)`` threads the caller's context
74
-
through collection and regeneration. Import resolution replaces only
75
-
``name_mappings`` for the second pass, so typed extensions and all other context
76
-
fields survive. When a context is supplied, its ``clean_mode`` is authoritative;
77
-
the separate ``clean_mode=`` argument is used only when pycodify constructs the
78
-
context.
78
+
Path factoring
79
+
--------------
79
80
80
-
Extensions provide formatting context, not formatter dispatch. New value
81
-
families still extend the ``SourceFormatter`` registry, while the exact extension
82
-
type allows that formatter to obtain host-owned rendering information without
83
-
adding host imports or fields to pycodify core.
81
+
``generate_python_source`` first renders the document with a ``PathCollector``
82
+
on ``FormatContext.paths`` to record every ``pathlib.Path`` value. A
83
+
``PathFactoring`` plan then binds each directory shared by two or more absolute
84
+
paths (``path_root = Path('/data/screen')``) and each repeated absolute path
85
+
(``path_1 = path_root / 'plate_A'``) once, at the top of the document, and the
86
+
two rendering passes spell those paths through the bindings. Relative paths and
87
+
strings are never factored. ``to_source`` alone uses the base ``PathRendering``,
88
+
which spells each path as ``Path('...')``.
84
89
85
90
Clean Mode
86
91
----------
@@ -107,25 +112,13 @@ not sandboxing or validation of generated programs. Hosts must restrict inputs
107
112
and custom formatters to trusted sources, review persisted code before running
108
113
it, and define their own compatibility policy for imported domain APIs.
109
114
110
-
Lazy Dataclass Integration
111
-
---------------------------
112
-
113
-
For dataclasses with ``__getattribute__`` interception (used for hierarchical config inheritance), formatters use ``object.__getattribute__`` to access raw field values without triggering lazy resolution:
114
-
115
-
.. code-block:: python
116
-
117
-
# In DataclassFormatter
118
-
ifhasattr(instance, "_resolve_field_value"):
119
-
# Bypass __getattribute__ to get raw None vs concrete value
0 commit comments