Skip to content

Commit

Permalink
Handle inference ambiguity for invalid-format-index
Browse files Browse the repository at this point in the history
Close #2752
  • Loading branch information
PCManticore committed Sep 17, 2019
1 parent e457c45 commit 0547852
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Expand Up @@ -12,6 +12,10 @@ Release date: TBA
This check warns when modules are imported from places other than a
module toplevel, e.g. inside a function or a class.

* Handle inference ambiguity for ``invalid-format-index``

Close #2752

* Removed Python 2 specific checks such as ``relative-import``,
``invalid-encoded-data``, ``missing-super-argument``.

Expand Down
4 changes: 2 additions & 2 deletions pylint/checkers/strings.py
Expand Up @@ -471,10 +471,10 @@ def _check_new_format_specifiers(self, node, fields, named):
if argname in (astroid.Uninferable, None):
continue
try:
argument = next(argname.infer())
argument = utils.safe_infer(argname)
except astroid.InferenceError:
continue
if not specifiers or argument is astroid.Uninferable:
if not specifiers or not argument:
# No need to check this key if it doesn't
# use attribute / item access
continue
Expand Down
12 changes: 11 additions & 1 deletion tests/functional/s/string_formatting.py
Expand Up @@ -2,9 +2,9 @@
"""
# pylint: disable=too-few-public-methods, import-error, unused-argument, line-too-long, no-absolute-import, useless-object-inheritance
import os
import sys
from missing import Missing

__revision__ = 1

class Custom(object):
""" Has a __getattr__ """
Expand Down Expand Up @@ -186,3 +186,13 @@ def avoid_empty_attribute():
"""The following string is invalid, avoid crashing."""

return "There are {.:2f} undiscovered errors.".format(1) # [bad-format-string]


def invalid_format_index_on_inference_ambiguity():
"""Test inference bug for invalid-format-index"""
options = []
if len(sys.argv) > 1:
options = [["Woof!"]]
else:
options = [["Barf!"]]
return 'Why is this bad? {options[0][0]}'.format(options=options)

0 comments on commit 0547852

Please sign in to comment.