• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pyta-uoft / pyta / 21491585406

29 Jan 2026 07:08PM UTC coverage: 94.136% (-0.04%) from 94.171%
21491585406

Pull #1291

github

web-flow
Merge 7366504e1 into 1fbc5d121
Pull Request #1291: Issue 1288

11 of 13 new or added lines in 1 file covered. (84.62%)

3564 of 3786 relevant lines covered (94.14%)

17.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.59
/packages/python-ta/src/python_ta/checkers/unnecessary_f_string_checker.py
1
"""Checker to check for unncessary f-strings that only consist of single expressions"""
2

3
from __future__ import annotations
20✔
4

5
from astroid import InferenceError, nodes
20✔
6
from pylint.checkers import BaseChecker
20✔
7
from pylint.checkers.utils import only_required_for_messages
20✔
8
from pylint.lint import PyLinter
20✔
9

10

11
class FormattedStringChecker(BaseChecker):
20✔
12
    """A checker class that reports unnecessary uses of f-strings when they can be replaced
13
    with the expression directly"""
14

15
    name = "unnecessary-f-string"
20✔
16
    msgs = {
20✔
17
        "E9920": (
18
            'Unnecessary use of an f-string in the expression f"{%s}". Use %s instead.',
19
            "unnecessary-f-string",
20
            "Used when the use of an f-string is unnecessary and can be replaced with the variable directly",
21
        ),
22
    }
23

24
    @only_required_for_messages("unnecessary-f-string")
20✔
25
    def visit_joinedstr(self, node: nodes.JoinedStr) -> None:
20✔
26
        if (
20✔
27
            len(node.values) == 1
28
            and isinstance(node.values[0], nodes.FormattedValue)
29
            and node.values[0].conversion == -1
30
            and node.values[0].format_spec is None
31
        ):
32
            expression = node.values[0].value.as_string()
20✔
33
            str_call_needed = False
20✔
34

35
            for inferred in node.values[0].value.inferred():
20✔
36
                if isinstance(inferred, nodes.Const):
20✔
37
                    source = inferred.as_string()
20✔
38
                    if not (source.startswith(("'", '"'))):
20✔
39
                        str_call_needed = True
20✔
40
                        break
20✔
41
                else:
NEW
42
                    str_call_needed = True
×
NEW
43
                    break
×
44

45
            message = expression
20✔
46
            if str_call_needed:
20✔
47
                message = f"str({expression})"
20✔
48

49
            self.add_message(
20✔
50
                "unnecessary-f-string",
51
                node=node,
52
                args=(expression, message),
53
                line=node.lineno,
54
                col_offset=node.col_offset,
55
            )
56

57

58
def register(linter: PyLinter) -> None:
20✔
59
    linter.register_checker(FormattedStringChecker(linter))
20✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc