Skip to content

Commit 30d454d

Browse files
committed
[mypyc] feat: support constant folding in StringFormatterChecker.checkers_for_c_type
1 parent 053c054 commit 30d454d

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

mypy/checkstrformat.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import mypy.errorcodes as codes
2121
from mypy import message_registry
2222
from mypy.checker_shared import TypeCheckerSharedApi
23+
from mypy.constant_fold import constant_fold_expr
2324
from mypy.errors import Errors
2425
from mypy.maptype import map_instance_to_supertype
2526
from mypy.messages import MessageBuilder
@@ -1005,8 +1006,13 @@ def check_expr(expr: Expression) -> None:
10051006
and len(expr.value) != 1
10061007
):
10071008
self.msg.requires_int_or_single_byte(context)
1008-
elif isinstance(expr, (StrExpr, BytesExpr)) and len(expr.value) != 1:
1009-
self.msg.requires_int_or_char(context)
1009+
else:
1010+
if isinstance(folded := constant_fold_expr(expr, "unused"), str):
1011+
value = folded
1012+
elif isinstance(expr, BytesExpr):
1013+
value = expr.value
1014+
if len(value) != 1:
1015+
self.msg.requires_int_or_char(context)
10101016

10111017
return check_expr, check_type
10121018

0 commit comments

Comments
 (0)