Skip to content

Use phase from Copier 9.5 to execute context hook only when rendering files#8

Closed
pawamoy wants to merge 8 commits intomainfrom
use-phase
Closed

Use phase from Copier 9.5 to execute context hook only when rendering files#8
pawamoy wants to merge 8 commits intomainfrom
use-phase

Conversation

@pawamoy
Copy link
Collaborator

@pawamoy pawamoy commented Jan 27, 2025

Fixes #7. Needs copier-org/copier#1948.

@pawamoy
Copy link
Collaborator Author

pawamoy commented Jan 29, 2025

After a lot of confusion, it turns out the key error originally mentioned in #7 is due to the fact that a new question was introduced, and the context in the "prompt" phase does not contain this question (and its answer) yet (or something like that 😵).

Instead of "fixing" this by preventing context hooks from running in other phases than "render", I suggest we simply document that the hook will run in all these different phases (and that the phase will be available in context["_copier_phase"] starting with Copier 9.5) and that extension authors should make sure to handle key errors, or can use conditionals based on the phase.

I note that we could add syntactic sugar (@noirbizarre 😉) in the form of parameter injection, allowing extension authors to accept phase: Phase or other kind of parameters in their context hook signature, so that these values are typed and easily available (instead of having to find them in the context dict). Alternatively, we could use a TypedDict to type the context, at least the parts that do not move (i.e. everything except answers IIUC).

@pawamoy pawamoy closed this Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: ContextHook is executed for before each question since copier 9.4

1 participant