Skip to content

Commit b99948b

Browse files
Fail gracefully on unsupported template strings (PEP 750) (#19700)
Refs #19329 Stopgap until proper support is added
1 parent d1c6904 commit b99948b

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed

mypy/fastparse.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@ def ast3_parse(
188188
ast_TypeVar = Any
189189
ast_TypeVarTuple = Any
190190

191+
if sys.version_info >= (3, 14):
192+
ast_TemplateStr = ast3.TemplateStr
193+
ast_Interpolation = ast3.Interpolation
194+
else:
195+
ast_TemplateStr = Any
196+
ast_Interpolation = Any
197+
191198
N = TypeVar("N", bound=Node)
192199

193200
# There is no way to create reasonable fallbacks at this stage,
@@ -1705,6 +1712,21 @@ def visit_FormattedValue(self, n: ast3.FormattedValue) -> Expression:
17051712
)
17061713
return self.set_line(result_expression, n)
17071714

1715+
# TemplateStr(expr* values)
1716+
def visit_TemplateStr(self, n: ast_TemplateStr) -> Expression:
1717+
self.fail(
1718+
ErrorMessage("PEP 750 template strings are not yet supported"),
1719+
n.lineno,
1720+
n.col_offset,
1721+
blocker=False,
1722+
)
1723+
e = TempNode(AnyType(TypeOfAny.from_error))
1724+
return self.set_line(e, n)
1725+
1726+
# Interpolation(expr value, constant str, int conversion, expr? format_spec)
1727+
def visit_Interpolation(self, n: ast_Interpolation) -> Expression:
1728+
assert False, "Unreachable"
1729+
17081730
# Attribute(expr value, identifier attr, expr_context ctx)
17091731
def visit_Attribute(self, n: Attribute) -> MemberExpr | SuperExpr:
17101732
value = n.value

mypy/test/testcheck.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
typecheck_files.remove("check-python312.test")
4848
if sys.version_info < (3, 13):
4949
typecheck_files.remove("check-python313.test")
50+
if sys.version_info < (3, 14):
51+
typecheck_files.remove("check-python314.test")
5052

5153

5254
class TypeCheckSuite(DataSuite):

mypy/test/testparse.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class ParserSuite(DataSuite):
2727
files.remove("parse-python312.test")
2828
if sys.version_info < (3, 13):
2929
files.remove("parse-python313.test")
30+
if sys.version_info < (3, 14):
31+
files.remove("parse-python314.test")
3032

3133
def run_case(self, testcase: DataDrivenTestCase) -> None:
3234
test_parser(testcase)
@@ -46,6 +48,8 @@ def test_parser(testcase: DataDrivenTestCase) -> None:
4648
options.python_version = (3, 12)
4749
elif testcase.file.endswith("python313.test"):
4850
options.python_version = (3, 13)
51+
elif testcase.file.endswith("python314.test"):
52+
options.python_version = (3, 14)
4953
else:
5054
options.python_version = defaults.PYTHON3_VERSION
5155

test-data/unit/check-python314.test

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[case testTemplateString]
2+
reveal_type(t"mypy") # E: PEP 750 template strings are not yet supported \
3+
# N: Revealed type is "Any"

test-data/unit/parse-python314.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[case testTemplateString]
2+
x = 'mypy'
3+
t'Hello {x}'
4+
[out]
5+
main:2: error: PEP 750 template strings are not yet supported

0 commit comments

Comments
 (0)