python代码中的特殊注释

  1. **# noqa**:

    • 用法:some_code = "example" # noqa
    • 功能:告诉flake8等工具忽略当前行的所有警告。
  2. **# type: ignore**:

    • 用法:import some_module # type: ignore
    • 功能:告诉mypy等类型检查器忽略当前行的类型检查警告。
  3. **# pylint: disable=some-message**:

    • 用法:some_code = "example" # pylint: disable=unused-variable
    • 功能:告诉pylint工具忽略特定的警告消息(例如未使用的变量)。
  4. **# isort: skip**:

    • 用法:import some_module # isort: skip
    • 功能:告诉isort工具在自动排序导入语句时跳过当前行。
  5. # fmt: off 和 **# fmt: on**:

    • 用法:
      1
      2
      3
      # fmt: off
      some_code = "example"
      # fmt: on
    • 功能:告诉black等代码格式化工具在# fmt: off# fmt: on之间的代码不要进行格式化。
  6. **# todo**:

    • 用法:# todo: implement this function
    • 功能:标记待办事项,一些编辑器或工具可能会识别它以列出待办事项。
  7. **# fixme**:

    • 用法:# fixme: handle this exception properly
    • 功能:标记需要修复的问题。
  8. **# ruff: ignore**:

    • 用法:some_code = "example" # ruff: ignore
    • 功能:告诉ruff工具忽略当前行的警告。
  9. # type: Any 或其他类型注释:

    • 用法:def my_function(param: Any) -> None: pass # type: (Any) -> None
    • 功能:用于类型注释,告诉类型检查器某个变量或函数的返回类型。
  10. # coding: utf-8 或其他编码声明:

    • 用法:# coding: utf-8
    • 功能:指定文件的编码格式,通常放在文件的第一行或第二行。
  11. **# mypy: ignore-errors**:

    • 用法:# mypy: ignore-errors
    • 功能:告诉mypy忽略文件中的所有类型错误。
  12. **# mypy: strict**:

    • 用法:# mypy: strict
    • 功能:告诉mypy对文件进行严格类型检查。
  13. **# pragma: no cover**:

    • 用法:def my_function(): # pragma: no cover
    • 功能:告诉代码覆盖率工具(如coverage.py)忽略这一行代码。
  14. **# jshint ignore:line**:

    • 用法:var myVar = 'value'; // jshint ignore:line
    • 功能:告诉JSHint工具忽略当前行的警告(虽然这是针对JavaScript的,但一些Python工具可能也有类似的约定)。
  15. # isort: off 和 **# isort: on**:

    • 用法:
      1
      2
      3
      # isort: off
      import some_module
      # isort: on
    • 功能:告诉isort工具在# isort: off# isort: on之间的代码不要进行导入排序。
  16. **# type: ignore[attr-defined]**:

    • 用法:obj.attr # type: ignore[attr-defined]
    • 功能:告诉mypy等类型检查器忽略特定的属性定义警告。
  17. **# type: ignore[misc]**:

    • 用法:some_code # type: ignore[misc]
    • 功能:告诉mypy等类型检查器忽略特定的杂项警告。
  18. **# cython: language_level=3**:

    • 用法:# cython: language_level=3
    • 功能:告诉Cython编译器使用Python 3的语言级别。
  19. **# flake8: noqa**:

    • 用法:# flake8: noqa
    • 功能:告诉flake8工具忽略当前文件的所有警告(通常放在文件的顶部)。
  20. **# check-arg-names: false**:

    • 用法:def my_function(a, b): # check-arg-names: false
    • 功能:告诉某些代码分析工具忽略参数名称检查。

这些注释通常用于控制代码分析工具的行为,帮助开发者在特定情况下避免不必要的警告或错误。使用这些注释时,应确保它们的目的是明确的,并且不会隐藏代码中的实际问题。

哪些跟编辑器绑定的特殊注释

有一些注释或标记是与特定编辑器或IDE绑定的,用于控制编辑器的行为,如代码折叠、任务列表、代码分析等。以下是一些常见的与编辑器绑定的注释:

  1. **# region# endregion**:

    • 用于定义代码折叠区域。
    • 示例:
      1
      2
      3
      4
      # region My Region
      def my_function():
      print("Hello, World!")
      # endregion
  2. **# todo# fixme# note# hack**:

    • 这些注释用于标记待办事项、需要修复的问题、重要说明或临时解决方案,一些编辑器会自动识别这些标记并提供任务列表视图。
    • 示例:
      1
      2
      3
      4
      # todo: implement this function
      # fixme: handle this exception properly
      # note: this is a workaround for a known issue
      # hack: temporary solution until a better approach is found
  3. # pylint: disable 和 **# pylint: enable**:

    • 用于在特定代码块中禁用或启用pylint的特定警告。
    • 示例:
      1
      2
      3
      # pylint: disable=unused-variable
      some_unused_variable = 42
      # pylint: enable=unused-variable
  4. **# type: ignore**:

    • 用于告诉类型检查器(如mypy)忽略特定行的类型错误。
    • 示例:
      1
      import some_module  # type: ignore
  5. **# isort: skip_file**:

    • 用于告诉isort工具忽略整个文件的导入排序。
    • 示例:
      1
      2
      3
      # isort: skip_file
      import b
      import a
  6. # fmt: off 和 **# fmt: on**:

    • 用于告诉代码格式化工具(如black)在特定代码块中禁用格式化。
    • 示例:
      1
      2
      3
      # fmt: off
      some_code = "example"
      # fmt: on
  7. **# noinspection**:

    • 用于告诉IDE(如PyCharm)忽略特定的代码检查。
    • 示例:
      1
      2
      # noinspection PyUnresolvedReferences
      import some_module
  8. **# mypy: ignore-errors**:

    • 用于告诉mypy忽略整个文件的类型错误。
    • 示例:
      1
      # mypy: ignore-errors

这些注释通常由特定的编辑器或IDE解释,并用于控制编辑器的行为或提供额外的信息。使用这些注释时,应确保了解它们的用途和影响,并且不会隐藏代码中的实际问题。