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

PyMassSpec / notebook2script / 15393658829

02 Jun 2025 01:33PM UTC coverage: ?%. First build
15393658829

Pull #55

github

web-flow
Merge acf93d2b8 into 278e3df88
Pull Request #55: [repo-helper] Configuration Update

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

70.59
/notebook2script/pointless_checker/base.py
1
#!/usr/bin/env python3
2
#
3
#  base.py
4
"""
5
Checker for pointless statements.
6
"""
7
#
8
#  Copyright © 2020-2021 Dominic Davis-Foster <dominic@davis-foster.co.uk>
9
#  Based on pylint
10
#  See notebook2script/pointless.py for full copyright information
11
#
12
#  This program is free software; you can redistribute it and/or modify
13
#  it under the terms of the GNU General Public License version 2
14
#  as published by the Free Software Foundation.
15
#
16
#  This program is distributed in the hope that it will be useful,
17
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
18
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
#  GNU General Public License for more details.
20
#
21
#  You should have received a copy of the GNU General Public License
22
#  along with this program; if not, write to the Free Software
23
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24
#  MA 02110-1301, USA.
25
#
26

27
# stdlib
28
from typing import Dict, Tuple
1✔
29

30
# 3rd party
31
import astroid  # type: ignore[import-untyped]
1✔
32
import astroid.bases  # type: ignore[import-untyped]
1✔
33
import astroid.scoped_nodes  # type: ignore[import-untyped]
1✔
34
from astroid import Expr
1✔
35
from pylint import interfaces  # type: ignore[import-untyped]
1✔
36
from pylint.checkers import utils  # type: ignore[import-untyped]
1✔
37
from pylint.checkers.base_checker import BaseChecker  # type: ignore[import-untyped]
1✔
38
from pylint.lint.pylinter import PyLinter  # type: ignore[import-untyped]
1✔
39

40
__all__ = ["BasicChecker", "register"]
1✔
41

42

43
class BasicChecker(BaseChecker):  # noqa: D101
1✔
44

45
        __implements__ = interfaces.IAstroidChecker
1✔
46

47
        msgs: Dict[str, Tuple[str, str, str]] = {
1✔
48
                        "W0104": (
49
                                        "Statement seems to have no effect",
50
                                        "pointless-statement",
51
                                        "Used when a statement doesn't have (or at least seems to) any effect.",
52
                                        ),
53
                        "W0105": (
54
                                        "String statement has no effect",
55
                                        "pointless-string-statement",
56
                                        "Used when a string is used as a statement (which of course "
57
                                        "has no effect). This is a particular case of W0104 with its "
58
                                        "own message so you can easily disable it if you're using "
59
                                        "those strings as documentation, instead of comments.",
60
                                        ),
61
                        "W0106": (
62
                                        "Expression '%s' is assigned to nothing",
63
                                        "expression-not-assigned",
64
                                        "Used when an expression that is not a function call is assigned "
65
                                        "to nothing. Probably something else was intended.",
66
                                        ),
67
                        }
68

69
        reports = ()
1✔
70

71
        @utils.check_messages("pointless-statement", "pointless-string-statement", "expression-not-assigned")
1✔
72
        def visit_expr(self, node: Expr) -> None:
1✔
73
                """
74
                Check for various kinds of statements without effect.
75
                """
76

77
                expr = node.value
1✔
78

79
                if isinstance(expr, astroid.Const) and isinstance(expr.value, str):
1✔
80
                        # treat string statement in a separated message
81
                        # Handle PEP-257 attribute docstrings.
82
                        # An attribute docstring is defined as being a string right after
83
                        # an assignment at the module level, class level or __init__ level.
84
                        scope = expr.scope()
×
85
                        if isinstance(scope, (astroid.ClassDef, astroid.Module, astroid.FunctionDef)):
×
86
                                if isinstance(scope, astroid.FunctionDef) and scope.name != "__init__":
×
87
                                        pass
×
88
                                else:
89
                                        sibling = expr.previous_sibling()
×
90
                                        if (
×
91
                                                        sibling is not None and sibling.scope() is scope
92
                                                        and isinstance(sibling, (astroid.Assign, astroid.AnnAssign))
93
                                                        ):
94
                                                return
×
95
                        self.add_message("pointless-string-statement", node=node)
×
96
                        return
×
97

98
                # Ignore if this is :
99
                # * a direct function call
100
                # * the unique child of a try/except body
101
                # * a yield statement
102
                # * an ellipsis (which can be used on Python 3 instead of pass)
103
                # warn W0106 if we have any underlying function call (we can't predict
104
                # side effects), else pointless-statement
105
                if (
1✔
106
                                isinstance(expr, (astroid.Yield, astroid.Await, astroid.Ellipsis, astroid.Call))
107
                                or (isinstance(node.parent, astroid.TryExcept) and node.parent.body == [node])
108
                                or (isinstance(expr, astroid.Const) and expr.value is Ellipsis)
109
                                ):
110
                        return
1✔
111
                if any(expr.nodes_of_class(astroid.Call)):
1✔
112
                        self.add_message("expression-not-assigned", node=node, args=expr.as_string())
×
113
                else:
114
                        self.add_message("pointless-statement", node=node)
1✔
115

116

117
def register(linter: PyLinter) -> None:
1✔
118
        """
119
        Required function to auto register this checker.
120
        """
121

122
        linter.register_checker(BasicChecker(linter))
1✔
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

© 2025 Coveralls, Inc