-
Notifications
You must be signed in to change notification settings - Fork 0
docs: split the purity contract out of file semantics; digest CompletedProcess #831
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
2 commits
Select commit
Hold shift + click to select a range
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
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 @@ | ||
| ../../notebooks/PathsInContainers.ipynb |
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,101 @@ | ||
| .. _purity: | ||
|
|
||
| Purity and Side Effects | ||
| ======================= | ||
|
|
||
| What fleche assumes about the functions you decorate, and what therefore does | ||
| *not* survive a cache hit. The rules here apply to every argument and result | ||
| type; :doc:`file_semantics` is the same contract seen through ``Path`` values. | ||
|
|
||
| The assumption | ||
| -------------- | ||
|
|
||
| fleche caches **pure** functions: the result is determined by the arguments, | ||
| and anything else the body does is incidental. A cache hit replays the | ||
| *result* and nothing else — so every effect a function has besides returning | ||
| a value happens on cold calls only. | ||
|
|
||
| That is not a restriction fleche can check, and it does not raise if you break | ||
| it. It shows up as a function that behaves differently the second time you | ||
| call it. | ||
|
|
||
| .. _argument-mutation: | ||
|
|
||
| Arguments are keyed as passed | ||
| ----------------------------- | ||
|
|
||
| Argument content is captured **before** the body runs, so a function that | ||
| mutates its own argument is still recorded under the pre-call content and | ||
| honest repeat calls hit. The mutation itself is neither recorded nor | ||
| replayed: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| @fleche | ||
| def append_and_report(xs, n): | ||
| xs.append(n) | ||
| return len(xs) | ||
|
|
||
| a = [1, 2] | ||
| append_and_report(a, 9) # 3 — body runs, and a is now [1, 2, 9] | ||
|
|
||
| b = [1, 2] | ||
| append_and_report(b, 9) # 3 — cache hit; b is still [1, 2] | ||
|
|
||
| Both calls return ``3``, because that is the recorded result. Only the first | ||
| one changed its argument. Nothing about this is specific to lists: a | ||
| ``dict``, a numpy array, and a directory a function writes into all behave the | ||
| same way. | ||
|
|
||
| Return what you mutate | ||
| ~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| An argument that is mutated **and returned** is captured faithfully in its | ||
| final, post-mutation state — the result is stored after the body runs: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| @fleche | ||
| def append_and_return(xs, n): | ||
| xs.append(n) | ||
| return xs | ||
|
|
||
| append_and_return([1, 2], 9) # [1, 2, 9] — body runs | ||
| append_and_return([1, 2], 9) # [1, 2, 9] — cache hit, same value | ||
|
|
||
| If the mutation is the point of the function, return it. Treat arguments you | ||
| receive as read-only otherwise, and build results fresh. | ||
|
|
||
| Other side effects | ||
| ------------------ | ||
|
|
||
| Everything else a body does — printing, logging, writing files outside the | ||
| returned value, sending a request, inserting a row — happens on the cold call | ||
| and never again: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| @fleche | ||
| def record(x): | ||
| db.insert(x) # runs once, ever | ||
| return x * 2 | ||
|
|
||
| If an effect must happen on every call, it belongs outside the cached | ||
| function; keep the cached part to the computation whose result you want | ||
| stored. | ||
|
|
||
| Two related cases | ||
| ----------------- | ||
|
|
||
| * A function returning ``None`` is never cached, so it re-executes every time | ||
| — including its side effects. See :ref:`none-not-cached`. | ||
| * Paths follow all of the above, with the mutation case made concrete: | ||
| a directory a function receives and writes into is recorded under its | ||
| pre-call tree, and the written file does not reappear on a hit. See | ||
| :doc:`file_semantics`. | ||
|
|
||
| See also | ||
| -------- | ||
|
|
||
| * :doc:`file_semantics` — the same contract for files and directories. | ||
| * :doc:`helpers` — ``.rerun()`` for forcing a cold call deliberately. |
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.
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.
any depthmight actually only apply whenremaining_depthis 0 :[=].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.
Checked this rather than trusting the wording — it holds at any
remaining_depth, and the reason is worth stating, so 62a2c3c now says why instead of just asserting "any depth".A
Pathmatches no destructurer, so in_intern_recit falls through the_DESTRUCTURERSloop withdepthstill atfloat("inf"). The inline test isif depth < self.remaining_depth, andinfis never less than anything — so a path is always written out viasuper().save(...), which is precisely wherePathValueMixinintercepts it.remaining_depthonly chooses inline-vs-separate-entry for nodes that are destructurable; it has no lever on a path.(It also propagates: a container holding a path gets
1 + inf, so the parents above it are written out separately too.)Verified empirically —
remaining_depth∈ {0, 1, 3, 10} × nesting depth ∈ {0, 1, 2, 3, 5, 8}, alternating list/dict/dataclass wrappers, deleting the original file before loading so only content addressing can survive. All 24 combinations materialize with the right content.Added to the docs:
Generated by Claude Code