From 5c13f8bbceaf6525b48add5b89b29f298980079c Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sun, 8 May 2022 19:42:38 -0700 Subject: [PATCH] ast: FormattedValue.conversion cannot be None (#7810) ``` In [4]: ast.dump(ast.parse('f"{x!r}"')) Out[4]: "Module(body=[Expr(value=JoinedStr(values=[FormattedValue(value=Name(id='x', ctx=Load()), conversion=114)]))], type_ignores=[])" In [5]: ast.dump(ast.parse('f"{x}"')) Out[5]: "Module(body=[Expr(value=JoinedStr(values=[FormattedValue(value=Name(id='x', ctx=Load()), conversion=-1)]))], type_ignores=[])" ``` (On 3.9 but I don't think this has changed since 3.6.) The stdlib also assumes this: https://github.com/python/cpython/blob/main/Lib/ast.py#L1211 (`chr(None)` doesn't work). --- stdlib/_ast.pyi | 2 +- tests/stubtest_allowlists/py39.txt | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/stdlib/_ast.pyi b/stdlib/_ast.pyi index cb13c7452..1305b0c94 100644 --- a/stdlib/_ast.pyi +++ b/stdlib/_ast.pyi @@ -319,7 +319,7 @@ class FormattedValue(expr): if sys.version_info >= (3, 10): __match_args__ = ("value", "conversion", "format_spec") value: expr - conversion: int | None + conversion: int format_spec: expr | None class JoinedStr(expr): diff --git a/tests/stubtest_allowlists/py39.txt b/tests/stubtest_allowlists/py39.txt index 47992c880..b947ee091 100644 --- a/tests/stubtest_allowlists/py39.txt +++ b/tests/stubtest_allowlists/py39.txt @@ -164,3 +164,7 @@ typing._SpecialForm.__mro_entries__ unicodedata.UCD.is_normalized xml.parsers.expat.XMLParserType.SkippedEntityHandler xml.parsers.expat.XMLParserType.intern + +# None on the class, but never None on instances +ast.FormattedValue.conversion +_ast.FormattedValue.conversion