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

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

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

问题

Pyright LSP 抛出以下错误:

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

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

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

我该如何解决这个问题?

英文:

Pyright LSP throws the following error:

  1. Pyright: Alternative syntax for unions requires Python 3.10 or newer

when using unions while typing Python code. Example:

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

How do I solve this?

答案1

得分: 1

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

示例 pyrightconfig.json 文件:

  1. {
  2. "include": [
  3. "src",
  4. "tests"
  5. ],
  6. "exclude": [
  7. "**/__pycache__"
  8. ],
  9. "venv": ".venv",
  10. "venvPath": "./.venv",
  11. "reportMissingImports": true,
  12. "reportMissingTypeStubs": false,
  13. "pythonVersion": "3.11.3",
  14. "pythonPlatform": "Linux"
  15. }
英文:

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:

  1. {
  2. "include": [
  3. "src",
  4. "tests"
  5. ],
  6. "exclude": [
  7. "**/__pycache__"
  8. ],
  9. "venv": ".venv",
  10. "venvPath": "./.venv",
  11. "reportMissingImports": true,
  12. "reportMissingTypeStubs": false,
  13. "pythonVersion": "3.11.3",
  14. "pythonPlatform": "Linux"
  15. }

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:

确定