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
相关产品推荐
相关产品推荐

