Skip to content

Commit 1b1a727

Browse files
authored
Update all tests to match PEP 750 spec. (#10)
1 parent 59ec1b0 commit 1b1a727

File tree

18 files changed

+214
-401
lines changed

18 files changed

+214
-401
lines changed

.devcontainer/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM koxudaxi/python:3.14.0a1-tstrings-b0dbe27-bullseye
1+
FROM ghcr.io/davepeck/python:davepeck-ts-iter-fix
22

33
# # Allow PYO3 to build even though the configured interpreter version (3.14)
44
# # is greater than PYO3's stated limit (3.12)

pep/__init__.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
_MISSING_TEMPLATE_ADD_RADD = False # 🎉
1818
"""True if the cpython prototype is missing __add__ and __radd__ for Template."""
1919

20-
_MISSING_TEMPLATE_EQ = False # 🎉
21-
"""True if the cpython prototype is missing an updated __eq__ for Template."""
22-
23-
_MISSING_TEMPLATE_HASH = False # 🎉
24-
"""True if the cpython prototype is missing __hash__ for Template."""
25-
2620
_MISSING_INTERLEAVING = False # 🎉
2721
"""True if the cpython prototype doesn't interleave strings and interpolations."""
2822

pep/afstring.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import inspect
88

9-
from templatelib import Interpolation, Template
9+
from string.templatelib import Interpolation, Template
1010

1111
from .fstring import convert
1212

@@ -21,8 +21,8 @@ async def async_f(template: Template) -> str:
2121
formatting it.
2222
"""
2323
parts = []
24-
for arg in template.args:
25-
match arg:
24+
for item in template:
25+
match item:
2626
case str() as s:
2727
parts.append(s)
2828
case Interpolation(value, _, conv, format_spec):

pep/format.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import string
88
from typing import Literal
99

10-
from templatelib import Interpolation, Template
10+
from string.templatelib import Interpolation, Template
1111

1212

1313
def _split_field_name(field_name: str) -> tuple[str, str]:

pep/fstring.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,30 @@
1111

1212
from typing import Literal
1313

14-
from templatelib import Interpolation, Template
14+
from string.templatelib import Interpolation, Template
1515

1616

17-
def convert(value: object, conv: Literal["a", "r", "s"] | None) -> object:
17+
def convert(value: object, conversion: Literal["a", "r", "s"] | None) -> object:
1818
"""Convert the value to a string using the specified conversion."""
1919
# Python has no convert() built-in function, so we have to implement it.
20-
if conv == "a":
20+
if conversion == "a":
2121
return ascii(value)
22-
if conv == "r":
22+
if conversion == "r":
2323
return repr(value)
24-
if conv == "s":
24+
if conversion == "s":
2525
return str(value)
2626
return value
2727

2828

2929
def f(template: Template) -> str:
3030
"""Implement f-string behavior using the PEP 750 t-string behavior."""
3131
parts = []
32-
for arg in template.args:
33-
match arg:
32+
for item in template:
33+
match item:
3434
case str() as s:
3535
parts.append(s)
36-
case Interpolation(value, _, conv, format_spec):
37-
value = convert(value, conv)
36+
case Interpolation(value, _, conversion, format_spec):
37+
value = convert(value, conversion)
3838
value = format(value, format_spec)
3939
parts.append(value)
4040
return "".join(parts)

pep/lazy.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Examples of lazy evaluation in templates."""
22

3-
from templatelib import Template
3+
from string.templatelib import Template
44

55
from .fstring import convert
66

@@ -18,15 +18,15 @@ def format_some(selector: str, template: Template, ignored: str = "***") -> str:
1818
unnecessary.
1919
"""
2020
parts = []
21-
for t_arg in template.args:
22-
if isinstance(t_arg, str):
23-
parts.append(t_arg)
21+
for item in template:
22+
if isinstance(item, str):
23+
parts.append(item)
2424
else:
25-
if t_arg.format_spec == selector:
26-
value = t_arg.value
25+
if item.format_spec == selector:
26+
value = item.value
2727
if callable(value):
2828
value = value()
29-
value = convert(value, t_arg.conv)
29+
value = convert(value, item.conversion)
3030
else:
3131
value = ignored
3232
parts.append(value)

pep/logging.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from logging import Formatter, LogRecord
1111
from typing import Any, Literal, Mapping, Protocol
1212

13-
from templatelib import Interpolation, Template
13+
from string.templatelib import Interpolation, Template
1414

1515
from .fstring import f
1616

@@ -36,9 +36,9 @@ def message(self) -> str:
3636
@property
3737
def values(self) -> Mapping[str, object]:
3838
return {
39-
arg.expr: arg.value
40-
for arg in self.template.args
41-
if isinstance(arg, Interpolation)
39+
item.expression: item.value
40+
for item in self.template
41+
if isinstance(item, Interpolation)
4242
}
4343

4444
@property
@@ -104,9 +104,9 @@ class ValuesFormatter(TemplateFormatterBase):
104104

105105
def values(self, template: Template) -> Mapping[str, object]:
106106
return {
107-
arg.expr: arg.value
108-
for arg in template.args
109-
if isinstance(arg, Interpolation)
107+
item.expression: item.value
108+
for item in template
109+
if isinstance(item, Interpolation)
110110
}
111111

112112
def format(self, record: LogRecord) -> str:

pep/reuse.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
values?
88
"""
99

10-
from templatelib import Interpolation, Template
10+
from string.templatelib import Interpolation, Template
1111

1212
from .fstring import convert
1313

@@ -23,23 +23,23 @@ class Formatter:
2323
def __init__(self, template: Template):
2424
"""Construct a formatter for the provided template."""
2525
# Ensure that all interpolations are strings.
26-
for arg in template.args:
27-
if isinstance(arg, Interpolation):
28-
if not isinstance(arg.value, str):
29-
raise ValueError(f"Non-string interpolation: {arg.value}")
26+
for item in template:
27+
if isinstance(item, Interpolation):
28+
if not isinstance(item.value, str):
29+
raise ValueError(f"Non-string interpolation: {item.value}")
3030
self.template = template
3131

3232
def format(self, **kwargs) -> str:
3333
"""Render the t-string using the given values."""
3434
parts = []
35-
for t_arg in self.template.args:
36-
if isinstance(t_arg, str):
37-
parts.append(t_arg)
35+
for item in self.template:
36+
if isinstance(item, str):
37+
parts.append(item)
3838
else:
39-
assert isinstance(t_arg.value, str)
40-
value = kwargs[t_arg.value]
41-
value = convert(value, t_arg.conv)
42-
value = format(value, t_arg.format_spec)
39+
assert isinstance(item.value, str)
40+
value = kwargs[item.value]
41+
value = convert(value, item.conversion)
42+
value = format(value, item.format_spec)
4343
parts.append(value)
4444
return "".join(parts)
4545

@@ -55,24 +55,24 @@ class Binder:
5555
def __init__(self, template: Template):
5656
"""Construct a binder for the provided template."""
5757
# Ensure that all interpolations are strings.
58-
for arg in template.args:
59-
if isinstance(arg, Interpolation):
60-
if not isinstance(arg.value, str):
61-
raise ValueError(f"Non-string interpolation: {arg.value}")
58+
for item in template:
59+
if isinstance(item, Interpolation):
60+
if not isinstance(item.value, str):
61+
raise ValueError(f"Non-string interpolation: {item.value}")
6262
self.template = template
6363

6464
def bind(self, **kwargs) -> Template:
6565
"""Bind values to the template."""
66-
args = []
67-
for t_arg in self.template.args:
68-
if isinstance(t_arg, str):
69-
args.append(t_arg)
66+
items = []
67+
for item in self.template:
68+
if isinstance(item, str):
69+
items.append(item)
7070
else:
71-
assert isinstance(t_arg.value, str)
72-
value = kwargs[t_arg.value]
73-
expr = repr(t_arg.value)[1:-1] # remove quotes from original expression
71+
assert isinstance(item.value, str)
72+
value = kwargs[item.value]
73+
expr = repr(item.value)[1:-1] # remove quotes from original expression
7474
interpolation = Interpolation(
75-
value, expr, t_arg.conv, t_arg.format_spec
75+
value, expr, item.conversion, item.format_spec
7676
)
77-
args.append(interpolation)
78-
return Template(*args)
77+
items.append(interpolation)
78+
return Template(*items)

0 commit comments

Comments
 (0)