You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyCharm中Python方法文档字符串重复显示问题咨询

PyCharm中Docstring悬停重复显示的问题解决

问题成因

  • 双源解析冲突:你的forward_train方法同时使用了类型注解(z: np.array、-> np.array)和手动编写的Docstring(Args/Returns区块)。PyCharm会自动从类型注解提取参数、返回值信息,同时又解析你写的Docstring内容,导致两部分内容重复展示。
  • Docstring缩进异常:代码中forward_train的Docstring开头缩进明显大于方法定义的缩进,PyCharm的文档解析器可能因格式不规范出现识别偏差,加剧了重复显示的问题。

解决办法

1. 修正Docstring缩进并规范格式

将Docstring的缩进调整为与方法内代码一致(通常为4个空格),确保格式符合PyCharm支持的规范(如Google/NumPy风格):

import numpy as np

class ReLu:
    """
    ReLu (Rectified Linear Unit) activation function for neural networks.
    """

    def forward_train(self, z: np.array) -> np.array:
        """Perform the forward pass during training and store the input for use in backpropagation.

        Args:
            z (np.array): The input data to the activation function.

        Returns:
            np.array: The result of applying the ReLU function to the input data.
        """
        self.__z = z
        return self.forward(z)

2. 关闭IDE自动提取类型注解生成文档的功能

如果希望完全使用自定义Docstring,不让IDE自动生成参数/返回值描述:

  • 打开PyCharm设置(File > Settings)
  • 导航到Tools > Python Integrated Tools
  • 在Docstring Format中选择你使用的风格(如Google),关闭“从类型提示提取信息”相关开关(不同版本选项名可能略有差异,找到对应控制项即可)

3. 二选一简化描述

如果不需要重复说明,可以只保留一种方式:

  • 仅用类型注解,简化Docstring:
def forward_train(self, z: np.array) -> np.array:
    """Perform the forward pass during training and store the input for use in backpropagation."""
    self.__z = z
    return self.forward(z)
  • 仅用Docstring描述(不推荐,类型注解能提升代码可读性):
def forward_train(self, z):
    """Perform the forward pass during training and store the input for use in backpropagation.

    Args:
        z (np.array): The input data to the activation function.

    Returns:
        np.array: The result of applying the ReLU function to the input data.
    """
    self.__z = z
    return self.forward(z)

内容的提问来源于stack exchange,提问作者dracule22

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 20:13:12