Skip to content
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions mypy/checkstrformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import mypy.errorcodes as codes
from mypy import message_registry
from mypy.checker_shared import TypeCheckerSharedApi
from mypy.constant_fold import constant_fold_expr
from mypy.errors import Errors
from mypy.maptype import map_instance_to_supertype
from mypy.messages import MessageBuilder
Expand Down Expand Up @@ -1005,8 +1006,12 @@ def check_expr(expr: Expression) -> None:
and len(expr.value) != 1
):
self.msg.requires_int_or_single_byte(context)
elif isinstance(expr, (StrExpr, BytesExpr)) and len(expr.value) != 1:
self.msg.requires_int_or_char(context)
elif isinstance(folded := constant_fold_expr(expr, "<unused>"), str):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

note to self: can I get cur_mod_id from context?

if len(folded) != 1:
self.msg.requires_int_or_char(context)
elif isinstance(expr, BytesExpr):
if len(expr.value) != 1:
self.msg.requires_int_or_char(context)

return check_expr, check_type

Expand Down