Skip to content

Fix + test for string format with %% and keys #1908

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

Merged
merged 1 commit into from
Jul 19, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion mypy/checkstrformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ def analyze_conversion_specifiers(self, specifiers: List[ConversionSpecifier],
context: Context) -> bool:
has_star = any(specifier.has_star() for specifier in specifiers)
has_key = any(specifier.has_key() for specifier in specifiers)
all_have_keys = all(specifier.has_key() for specifier in specifiers)
all_have_keys = all(
specifier.has_key() or specifier.type == '%' for specifier in specifiers
)

if has_key and has_star:
self.msg.string_interpolation_with_star_and_key(context)
Expand Down Expand Up @@ -145,6 +147,9 @@ def check_mapping_str_interpolation(self, specifiers: List[ConversionSpecifier],
mapping[key_str] = self.accept(v)

for specifier in specifiers:
if specifier.type == '%':
# %% is allowed in mappings, no checking is required
continue
if specifier.key not in mapping:
self.msg.key_not_in_mapping(specifier.key, replacements)
return
Expand Down
2 changes: 2 additions & 0 deletions test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,8 @@ a = None # type: Any
'%()d' % {'': 2}
'%(a)d' % {'a': 1, 'b': 2, 'c': 3}
'%(q)d' % {'a': 1, 'b': 2, 'c': 3} # E: Key 'q' not found in mapping
'%(a)d %%' % {'a': 1}

[builtins fixtures/dict.py]

[case testStringInterpolationMappingDictTypes]
Expand Down