Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix string formatting for string enums #16555

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,17 @@ def check_str_format_call(self, e: CallExpr) -> None:
if isinstance(e.callee.expr, StrExpr):
format_value = e.callee.expr.value
elif self.chk.has_type(e.callee.expr):
base_typ = try_getting_literal(self.chk.lookup_type(e.callee.expr))
typ = get_proper_type(self.chk.lookup_type(e.callee.expr))
if (
isinstance(typ, Instance)
and typ.type.is_enum
and isinstance(typ.last_known_value, LiteralType)
and isinstance(typ.last_known_value.value, str)
):
value_type = typ.type.names[typ.last_known_value.value].type
if isinstance(value_type, Type):
typ = get_proper_type(value_type)
base_typ = try_getting_literal(typ)
if isinstance(base_typ, LiteralType) and isinstance(base_typ.value, str):
format_value = base_typ.value
if format_value is not None:
Expand Down
42 changes: 42 additions & 0 deletions test-data/unit/check-formatting.test
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,45 @@ class S:
'{:%}'.format(0.001)
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-medium.pyi]

[case testEnumWithStringToFormatValue]
from enum import Enum

class Responses(str, Enum):
TEMPLATED = 'insert {} here'
TEMPLATED_WITH_KW = 'insert {value} here'
NORMAL = 'something'

Responses.TEMPLATED.format(42)
Responses.TEMPLATED_WITH_KW.format(value=42)
Responses.TEMPLATED.format() # E: Cannot find replacement for positional format specifier 0
Responses.TEMPLATED_WITH_KW.format() # E: Cannot find replacement for named format specifier "value"
Responses.NORMAL.format(42) # E: Not all arguments converted during string formatting
Responses.NORMAL.format(value=42) # E: Not all arguments converted during string formatting
[builtins fixtures/primitives.pyi]

[case testNonStringEnumToFormatValue]
from enum import Enum

class Responses(Enum):
TEMPLATED = 'insert {value} here'

Responses.TEMPLATED.format(value=42) # E: "Responses" has no attribute "format"
[builtins fixtures/primitives.pyi]

[case testStrEnumWithStringToFormatValue]
# flags: --python-version 3.11
from enum import StrEnum

class Responses(StrEnum):
TEMPLATED = 'insert {} here'
TEMPLATED_WITH_KW = 'insert {value} here'
NORMAL = 'something'

Responses.TEMPLATED.format(42)
Responses.TEMPLATED_WITH_KW.format(value=42)
Responses.TEMPLATED.format() # E: Cannot find replacement for positional format specifier 0
Responses.TEMPLATED_WITH_KW.format() # E: Cannot find replacement for named format specifier "value"
Responses.NORMAL.format(42) # E: Not all arguments converted during string formatting
Responses.NORMAL.format(value=42) # E: Not all arguments converted during string formatting
[builtins fixtures/primitives.pyi]