代码之家  ›  专栏  ›  技术社区  ›  damd

在docstring中使用输入模块

  •  0
  • damd  · 技术社区  · 7 年前

    假设我有一个带有docstring的函数,其中我将返回类型声明为带有两个字符串的元组:

    def foo():
        """
        Returns:
            Tuple[str, str]: Tuple of first name and last name
        """
    

    我应该进口吗 Tuple typing 如果我在任何地方都不使用它,除了在docstring中?

    2 回复  |  直到 7 年前
        1
  •  4
  •   Martijn Pieters    7 年前

    PyCharm对类型提示的docstring支持实际上并不使用 typing . 您不需要导入模块。

    打字 模块只支持在运行时执行注释的事实;以开头的语句 def foo() -> Tuple[str, str]: Tuple[str, str] 所以希望能够解析这些名字(从Python3.7开始,您可以禁用(或者更确切地说, from __future__ import annotations

    但是docstring通常不会被计算,并且不能包含可运行的Python代码。

    实际注释 :

    from typing import Tuple
    
    
    # type aliases
    firstname = str
    lastname = str
    
    
    def foo() -> Tuple[firstname, lastname]:
        """Descriptive line documenting this function's purpose"""
        # ...
    
        2
  •  1
  •   blue_note    7 年前

    pylint

    回纹 在docstring中查找实际代码的部分。如

    """
    Returns:
        `Tuple[str, str]`: Tuple of first name and last name
    """
    

    推荐文章