【问题标题】:Can Pylint error checking be customized?可以自定义 Pylint 错误检查吗?
【发布时间】:2012-04-13 10:08:14
【问题描述】:

我在设置 pylint 的地方使用 pydev。 问题是即使在 cmets 内部,pylint 也会报告警告。我希望禁用任何行或块注释内的任何类型的检查。 另外,我希望在我的代码中遵循 camelCase 命名约定而不是下划线来表示变量和参数。 有没有办法在不使用任何 pylint 插入我的代码的情况下指定这样的规则:禁用 cmets?

【问题讨论】:

  • 请不要使用 camelCase。
  • 虽然您的评论是有道理的,但我的团队主要由 Java 开发人员组成,为了提高其可维护性/接受度,我不得不偏离 Python 代码。
  • 在某些情况下骆驼案是合理的。例如在使用 PySide 或 PyQt 时。将 Qt-ish camel case 与 python underscore-ish 风格混合起来看起来很糟糕。
  • @SumitBisht 胜过所有代码风格规则的规则是“保持一致”。因此你做了正确的事:)。
  • 是的,混合样式是一个很大的禁忌,实际上是代码异味的标志。保持任何代码库的一致性是关键。

标签: python pydev pylint


【解决方案1】:

您可以使用全局禁用某个类的警告

pylint --disable=W1234

或使用特殊的 PyLint 配置文件

pylint --rcfile=/path/to/config.file

下面给出了一个示例配置文件:

[MESSAGES CONTROL]
# C0111 Missing docstring 
# I0011 Warning locally suppressed using disable-msg
# I0012 Warning locally suppressed using disable-msg
# W0704 Except doesn't do anything Used when an except clause does nothing but "pass" and there is no "else" clause
# W0142 Used * or * magic* Used when a function or method is called using *args or **kwargs to dispatch arguments.
# W0212 Access to a protected member %s of a client class
# W0232 Class has no __init__ method Used when a class has no __init__ method, neither its parent classes.
# W0613 Unused argument %r Used when a function or method argument is not used.
# W0702 No exception's type specified Used when an except clause doesn't specify exceptions type to catch.
# R0201 Method could be a function
# W0614 Unused import XYZ from wildcard import
# R0914 Too many local variables
# R0912 Too many branches
# R0915 Too many statements
# R0913 Too many arguments
# R0904 Too many public methods
disable=C0111,I0011,I0012,W0704,W0142,W0212,W0232,W0613,W0702,R0201,W0614,R0914,R0912,R0915,R0913,R0904,R0801

Pylint's dedicated site 上查看the documentation

【讨论】:

  • 很好的答案,但还有更多。如果您想跨多个项目和更复杂的场景对 pylint 进行细粒度控制,请参阅我的回答。 stackoverflow.com/a/32672068/763269
  • 我在终端上尝试了pylint --disable=C0111,得到了Usage: pylint [options] module_or_package。我错过了什么吗?
  • @MelvicYbanez 是的,你确实错过了一些东西;)你有 pylint [options] 部分,但没有 module_or_package 部分。
【解决方案2】:

正如 cfedermann 所说,您可以在 ~/.pylintrc 文件中指定要禁用的消息(请注意,如果您不想使用内联 cmets,可以使用 pylint --generate-rcfile 生成存根文件。

您还将在生成的文件中的 [BASIC] 部分中看到“method-rgx”、“function-rgx”等选项,您可以根据需要配置这些选项以支持驼峰式风格而不是pep8 下划线样式。

【讨论】:

    【解决方案3】:

    虽然这是一个老问题,但应该提到现在可以指定他们的own regex for matching with names

    那么你匹配骆驼大小写的正则表达式会是这样的:

    [a-z][a-zA-Z0-9]{2,30}$
    

    【讨论】:

      【解决方案4】:

      Here is the custom check exampleanother example easier to understand

      我遇到了和你类似的问题。以下代码是我的解决方案。我定制了一个检查器来禁止导入datetime.now。大家可以参考一下:

      class TestChecker(BaseChecker):
          """
          find the check type in the following url:
          https://github.com/PyCQA/pylint/blob/63eb8c4663a77d0caf2a842b716e4161f9763a16/pylint/checkers/typecheck.py
          """
          __implements__ = IAstroidChecker
      
          name = 'test-checker'
          priority = -1
          msgs = {
              'W0001': (
                  'You should not import "%s"',
                  'import-datetime-returns',
                  'Should not import datetime'
              ),
          }
      
          def __init__(self, linter):
              super().__init__(linter)
              # I use original pylint's ImportsChecker as a property
              # from  import **
              self.forbidden_import = ['datetime.datetime.now']
              self.forbidden_import_from = ['datetime.now', 'now']
              self.forbidden_import_attribute = ['datetime.now', 'now', 'datetime.datetime.now']
      
          #the function will be rewrited
          def visit_importfrom(self, node):
              names = [name for name, _alias in node.names]
              for item in names:
                  for check in self.forbidden_import_from:
                      if item == check:
                          self.add_message('W0001', node=node, args=item)
      
          def visit_import(self, node):
              names = [name for name, _ in node.names]
              for item in names:
                  for check in self.forbidden_import:
                      if check == item:
                          self.add_message('W0001', node=node, args=item)
      
          def visit_attribute(self, node):
              for check_attr in self.forbidden_import_attribute:
                  if check_attr == node.as_string():
                      self.add_message('W0001', node=node, args=check_attr)
      
      
      def register(linter):
          linter.register_checker(TestChecker(linter))
      

      【讨论】:

        【解决方案5】:

        我有两种方式自定义pylint

        使用配置文件

        第一种方式是你在哪里

        • 调用pylint生成模板配置文件
        • 然后根据您的需要/想要定制配置文件
        • 然后将配置文件放在默认的 pylint 配置文件位置,或者始终调用 pylint 并指定配置文件路径

        使用包装脚本

        第二种方法是创建一个调用 pylint 的包装器脚本,在包装器脚本中你有一堆看起来像这样的行:

        pylint \
               ${options_here} \
               --disable=xyz1 \
               --disable=xyz_2 \
               ${more_options} \
               --disable=xyz_N \
               --disable=abc \
               $@
        

        目前我正在使用包装脚本方法,因为我希望按行号对问题进行排序,并且我编写了一些 shell 脚本来获得该排序顺序。

        【讨论】:

          【解决方案6】:

          除了 pradyunsg 的上述回答之外,这里是 CamelCase 的另一个正则表达式:

          ^([a-z]\w+[A-Z]+\w+)
          

          (取自 PyLint 的 spelling.py 检查器,位于:%APPDATA% - Local - Programs - Python - [PythonVersion] - Lib - site-packages - pylint - checkers 文件夹)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-02-11
            • 1970-01-01
            • 1970-01-01
            • 2021-10-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-09-01
            相关资源
            最近更新 更多