Skip to content

Prefactor some parts to reduce size of other PR#109

Merged
davepeck merged 8 commits intot-strings:mainfrom
ianjosephwilson:ian/prefactor_for_new_processor
Mar 24, 2026
Merged

Prefactor some parts to reduce size of other PR#109
davepeck merged 8 commits intot-strings:mainfrom
ianjosephwilson:ian/prefactor_for_new_processor

Conversation

@ianjosephwilson
Copy link
Copy Markdown
Contributor

  • new modules
    • start preparing for more html spec constants by creating new module: htmlspec.py
    • move html dunder protocol into a module so we can more easily share it: protocols.py
    • Add sentinel for use in the project (until Python standardizes one, in 3.15? maybe): sentinel.py
  • TemplateRef additions
    • add helper method for TemplateRef that iterates parts, omitting empty strings, like Template.__iter__ does.
    • add helper method to TemplateRef that resolves the interpolation values from a paired Template object in between the TemplateRef strings based on the i_indexes.
  • new feature: "callback" format_spec triggers new formatter
    • Add callback formatter that executes a zero-arg callable before resolving the template values. This would be opt-in instead of any Callable passed into the Template being called automatically.

Copy link
Copy Markdown
Contributor

@davepeck davepeck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a good pre-factor. But I'm a little unclear on where we're heading with component invocation?


with pytest.raises(TypeError):
_ = html(t"<p>The sum is {add}.</p>")
_ = html(t"<p>The sum is {add:callback}.</p>")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. How should I think about :callback?

Our dataclass-style components historically depend on tdom always calling callables if it finds them in content position. Has this also changed?

Copy link
Copy Markdown
Contributor Author

@ianjosephwilson ianjosephwilson Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe: "call back to this function to get the effective value to plug into this interpolation whenever you end up processing this template". It is general purpose but by making it opt-in we don't have to check every single interpolation value to see if it is a callable or not.

Since this is a mixed bag of changes that were more easily extractable it might seem a little out of place but the other PR removes processing callables automatically and you HAVE to opt-in except for components which are their own flow.

Instead of going back out to start all over again ...

return _node_from_value(result)

... if the component callable is a factory then it is just immediately called a second time to get the Template.

https://github.com/t-strings/tdom/pull/105/changes#diff-4045942bbefd0bbd3a5a7e792e3dc8a234c7a9cfb0a7c136d38ce00fdfedd8c3R203

So instead of running everything through the same node_from_value function we have more dedicated flows that resolve values with more context.

Copy link
Copy Markdown
Contributor

@davepeck davepeck Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the component callable is a factory then it is just immediately called a second time to get the Template

Ah, that makes sense. That's a better approach. Basically, a "component" can return a Template or a (zero-arg) callable. Fine.

@davepeck
Copy link
Copy Markdown
Contributor

@ianjosephwilson This feels mergable. Agreed?

@ianjosephwilson
Copy link
Copy Markdown
Contributor Author

Yes, should be just rearranging things and adding things, I don't think anything really changes.

@davepeck davepeck merged commit 5a08e18 into t-strings:main Mar 24, 2026
1 check passed
@davepeck
Copy link
Copy Markdown
Contributor

Thank you thank you!

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.

2 participants