Skip to content
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

PEP 750: Updates after community feedback #4124

Open
wants to merge 28 commits into
base: main
Choose a base branch
from

Conversation

davepeck
Copy link
Contributor

@davepeck davepeck commented Nov 18, 2024

Updates include:

  • Addition of typed convenience accessors to Template, including strings, interpolations, and values.
  • New discussion of the relationship between t-strings and old-style format strings, along with example code
  • Improved discussion of laziness/delayed binding
  • Describe Template.__hash__()
  • Make Template.args explicitly be a tuple
  • Bugfix the description of debug specifier behavior

Thanks to @lysnikolaou @koxudaxi @pauleveritt @jimbaker and everyone in recent discussion threads for all the useful feedback.

I opened a new Discourse thread for this updated PR.


📚 Documentation preview 📚: https://pep-previews--4124.org.readthedocs.build/

@hugovk hugovk changed the title Updates to PEP 750 after community feedback PEP 750: Updates after community feedback Nov 18, 2024
peps/pep-0750.rst Outdated Show resolved Hide resolved
@AA-Turner AA-Turner self-requested a review November 18, 2024 18:40
Copy link

@gvanrossum-ms gvanrossum-ms left a comment

Choose a reason for hiding this comment

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

I was going to do a full review of this PR but ran out of time. Here are a few comments I have. In any case, I offer to just merge this PR, so readers of the thread won't have to wonder if their changes are accepted. You can always merge another PR -- there's no particular process to be followed here.

peps/pep-0750.rst Outdated Show resolved Hide resolved
@@ -138,11 +144,23 @@ Template strings evaluate to an instance of a new type, ``templatelib.Template``
.. code-block:: python

class Template:
args: Sequence[str | Interpolation]
args: tuple[str | Interpolation, ...]

Choose a reason for hiding this comment

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

This makes args appear writable. Maybe it should also be a property? You can always use __args for the real storage.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(Will address when we decide the final layout for Template, since args may be going away.)

peps/pep-0750.rst Outdated Show resolved Hide resolved

The ``Template.strings`` and ``Template.interpolations`` properties provide
convenient access to the ``str`` and ``Interpolation`` instances
found in ``Template.args``.

Choose a reason for hiding this comment

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

Why not add that they return args[0::2] and args[1::2]?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(Will address when we decide the final layout for Template, since args may be going away.)

Comment on lines +438 to +441
An ``Interpolation`` instance is hashable if its ``value`` attribute
is also hashable. Likewise, a ``Template`` instance is hashable if
all of its ``Interpolation`` instances are hashable. In all other cases,
``TypeError`` is raised.

Choose a reason for hiding this comment

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

I kinda see why you're doing it this way, but it means that a hypothetical caching HTML template processor will have a hard time caching templates reliably (e.g. if a dict of attributes is given as in the example, the template can't be cached, at least its natural hash and eq won't let it). And in such use cases presumably the values in the cache are ignored -- when a new template is processed, its values are combined with the cached processed template.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My expectation is that the Template.strings tuple will be the common cache key in these scenarios.

Copy link
Member

Choose a reason for hiding this comment

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

So then I would consider dropping hash and eq.

peps/pep-0750.rst Outdated Show resolved Hide resolved
Comment on lines +521 to +529
No ``Template.__str__()`` Implementation
----------------------------------------

The ``Template`` type does not provide a specialized ``__str__()`` implementation;
it inherits the default implementation from the ``object`` class.

This is because ``Template`` instances are intended to be used by template processing
code, which may return a string or any other type. There is no canonical way to
convert a Template to a string.

Choose a reason for hiding this comment

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

Is there a repr()? It should return the entire template literal (including t" and ") as a string, preserving the text between the {} -- possibly normalizing cases where the template doesn't preserve the exact text inside those.

Copy link
Contributor Author

@davepeck davepeck Dec 21, 2024

Choose a reason for hiding this comment

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

Ah, good question. I don't see why not to do this, or something like it. @lysnikolaou are there any implementation issues that would come up?

Copy link
Member

Choose a reason for hiding this comment

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

IIRC if there is a repr but no str, str falls back to the repr instead of inheriting from object.

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.

5 participants