【问题标题】:VSCode python type hint complaning when NoneVSCode python类型提示在无时出现
【发布时间】:2022-01-22 17:44:18
【问题描述】:

在某些带有类型提示的函数上,当可以返回多种类型时,如果 None 可用

(method) get_instance: (config_dict: Unknown) -> Self@DBConnector | None

如果它使用它

    db_obj = DBConnector.get_instance(configUtil.config)
    connection = db_obj.get_connection()

db_obj.get_connection() 是红色的,鼠标悬停时的消息是

无法访问“无”类型的成员“get_connection” 成员“get_connection”未知PylancereportGeneralTypeIssues

我试图找到这方面的内容,但一无所获

【问题讨论】:

  • 我建议将if db_obj is not None 添加到您的代码中。该错误不在类型检查器中,而是在您自己的代码中。 Pylance 是正确的
  • 好吧,我照你说的做了,它起作用了,我不知道这一点,但现在,这对我来说似乎很明显(在你给我看灯之后),但我还有其他问题pylance 在抱怨。我认为这是合法的,但不知何故我不明白这个问题Cannot access member "info" for type "list[str]" Member "info" is unknownPylancereportGeneralTypeIssues info: Unknown | Any 使用current_app.logger.info("show me the money")
  • 看起来您的 current_app.logger 记录为 list[str] 而不是 logging.Log 或类似的东西
  • 我同意你的观点,但我应用了这个:stackoverflow.com/questions/16994174/…。据我了解,current_app 是“主”应用程序的代理,它应该以某种方式声明记录器。但就我而言,我没有在主程序中声明它,这可能是问题所在,我仍然会挖掘

标签: python visual-studio-code type-hinting linter


【解决方案1】:

在this https://github.com/microsoft/pylance-release/issues/371上找到了修复

这取决于 pylance 的行为方式:enter link description here

我已经编辑了我的 vscode 配置

“python.analysis.typeCheckingMode”:“关闭”

【讨论】:

    猜你喜欢
    • 2013-10-12
    • 2021-09-16
    • 1970-01-01
    • 2022-09-29
    • 2022-01-04
    • 1970-01-01
    • 2017-02-03
    • 2021-10-28
    相关资源
    最近更新 更多