Using Python 3.10, but Pyright LSP throws error "Pyright: Alternative syntax for unions requires Python 3.10 or newer"

huangapple go评论64阅读模式
英文:

Using Python 3.10, but Pyright LSP throws error "Pyright: Alternative syntax for unions requires Python 3.10 or newer"

问题

Pyright LSP 抛出以下错误:

Pyright: 使用联合类型时需要 Python 3.10 或更新版本的备用语法

在输入 Python 代码时使用联合类型时发生此错误。示例:

class Example:
    def method(self) -> str | None:

我该如何解决这个问题?

英文:

Pyright LSP throws the following error:

Pyright: Alternative syntax for unions requires Python 3.10 or newer

when using unions while typing Python code. Example:

class Example:
    def method(self) -> str | None:

How do I solve this?

答案1

得分: 1

要解决此问题,请在项目根目录中创建一个名为 pyrightconfig.json 的文件,如果尚未创建,请将 pythonVersion 设置为您正在使用的Python版本。必须是 3.10 或更高版本。

示例 pyrightconfig.json 文件:

{
  "include": [
    "src",
    "tests"
  ],
  "exclude": [
    "**/__pycache__"
  ],
  "venv": ".venv",
  "venvPath": "./.venv",
  "reportMissingImports": true,
  "reportMissingTypeStubs": false,
  "pythonVersion": "3.11.3",
  "pythonPlatform": "Linux"
}
英文:

To solve this, create a pyrightconfig.json in your project root, if you haven't already, and set the pythonVersion to the version of Python you are using. Must be 3.10 and up.

Example pyrightconfig.json file:

{
  "include": [
    "src",
    "tests"
  ],
  "exclude": [
    "**/__pycache__"
  ],
  "venv": ".venv",
  "venvPath": "./.venv",
  "reportMissingImports": true,
  "reportMissingTypeStubs": false,
  "pythonVersion": "3.11.3",
  "pythonPlatform": "Linux"
}

huangapple
  • 本文由 发表于 2023年6月25日 22:08:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550806.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定