为何Python文档字符串在VS Code悬浮提示中无换行?如何修复?
解决VS Code中Docstring参数行不换行的问题
要让max_attempts参数在VS Code悬浮提示里单独占一行,只需在每个参数的描述行之后添加空行,确保每个参数项作为独立段落存在。修改后的文档字符串如下:
def get_crypto_price(crypto: str, currency: str, max_attempts: int = 3) -> float | None: """ Retrieves the price of a cryptocurrency in a specified currency using the CoinGecko API. Args: crypto (str): The cryptocurrency to retrieve the price for (e.g., 'bitcoin'). currency (str): The currency to retrieve the price in (e.g., 'usd'). max_attempts (int): The maximum number of attempts to make before giving up (default: 3). Returns: float | None: The price of the cryptocurrency in the specified currency, or None if the price could not be retrieved. """
原理说明
VS Code的Python语言服务(基于Pyright)解析Google风格Docstring时,会把连续的非空行判定为同一段落。给每个参数描述行后添加空行,能明确告知解析器每个参数是独立条目,从而在悬浮提示中正确换行显示。
另外也可检查参数行末尾是否有多余空格——这类冗余字符可能干扰段落识别,确保行尾干净也能辅助解决问题,但添加空行的方式更可靠,同时符合规范的Docstring写法。
内容的提问来源于stack exchange,提问作者GollyJer
相关产品推荐
相关产品推荐

