Skip to content

Make string interpolation checking less unnecessarily permissive - #2519

Closed
ddfisher wants to merge 1 commit into
masterfrom
strformat-fixup
Closed

ddfisher wants to merge 1 commit into
masterfrom
strformat-fixup

Conversation

@ddfisher

@ddfisher ddfisher commented Dec 1, 2016

Copy link
Copy Markdown
Collaborator

No description provided.

Comment thread mypy/checkstrformat.py
if (isinstance(replacements, DictExpr) and
all(isinstance(k, (StrExpr, BytesExpr))
for k, v in replacements.items)):
if isinstance(replacements, DictExpr):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is generally not right. Consider this example:

x = 'q'
'%(q)d' % {'a': 1, x: 2}   # actually okay

What about still using the all(...) check (include also unicode), but only for the key_not_in_mapping error? Item types can still be checked even if some keys are not literals -- we can assume that there are no duplicate keys.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What about the case with a non-literal key? It might be pretty rare, but here's one example where it would be kind of reasonable:

# constants.py
key1 = 'key1'
key2 = 'key2'

# code.py
from constants import key1, key2
...
'%(key1)d, %(key2)d' % {key1: 1, key2: 2}

Not sure whether we are likely to encounter code like that in practice.

@gvanrossum

Copy link
Copy Markdown
Member

Let me just close this; it's over 4 months old. If you're still interested just create a new PR (or reopen this one if GitHub supports that).

@gvanrossum gvanrossum closed this Apr 12, 2017
@gvanrossum
gvanrossum deleted the strformat-fixup branch April 22, 2017 05:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants