microsoft / pyright

Static Type Checker for Python
Other
13.28k stars 1.44k forks source link

pyright cannot parse mixed list TOML 1.0 configuration #9286

Open Stealthii opened 19 hours ago

Stealthii commented 19 hours ago

Describe the bug Utilizing any list in TOML that contains a mix of strings, and dictionaries (such as Tox's modern TOML format specification, or dependency-groups includes) will result in pyright being unable to read the configuration file.

Code or Screenshots Consider the following, TOML 1.0.0 compliant, dependency-groups section of a pyproject.toml:

[dependency-groups]
dev = [
  { include-group = "fix" },
  { include-group = "test" },
  { include-group = "type" },
]
fix = [
  "pre-commit-uv>=4.1.3",
]
pkg-meta = [
  "check-wheel-contents>=0.6",
  "twine>=5.1.1",
  "uv>=0.4.17",
]
test = [
  "anyio>=4",
  "covdefaults>=2.3",
  "devpi-process>=1.0.2",
  "diff-cover>=9.2",
  "pytest>=8.3.3",
  "pytest-cov>=5",
  "pytest-mock>=3.14",
  "trio>=0.25",
  "typing-extensions>=4.0.1",
]
type = [
  "mypy",
  "pyright",
  { include-group = "test" },
]

VS Code extension or command-line

❯ pyright --version
pyright 1.1.385
❯ pyright src
Pyproject file parse attempt 1 error: {"name":"TomlError","fromTOML":true,"wrapped":null,"line":135,"col":28,"pos":4100}
Pyproject file parse attempt 2 error: {"name":"TomlError","fromTOML":true,"wrapped":null,"line":135,"col":28,"pos":4100}
Pyproject file parse attempt 3 error: {"name":"TomlError","fromTOML":true,"wrapped":null,"line":135,"col":28,"pos":4100}
Pyproject file parse attempt 4 error: {"name":"TomlError","fromTOML":true,"wrapped":null,"line":135,"col":28,"pos":4100}
Pyproject file parse attempt 5 error: {"name":"TomlError","fromTOML":true,"wrapped":null,"line":135,"col":28,"pos":4100}
Pyproject file parse attempt 6 error: {"name":"TomlError","fromTOML":true,"wrapped":null,"line":135,"col":28,"pos":4100}
Config file "/Users/pyright/python-example-apiclient-httpx/pyproject.toml" could not be parsed. Verify that format is correct.
0 errors, 0 warnings, 0 informations

Lines 133-137, for reference:

type = [
  "mypy",
  "pyright",
  { include-group = "test" },
]
erictraut commented 19 hours ago

Pyright is currently using a toml parser that targets toml 0.5.0. It looks like the toml standard has evolved since 0.5.0, so we may need to adopt a new parser library. This carries risk with it, as there are likely to be new bugs in other libraries.

This library claims to support toml 1.0.0, and it has a fair amount of usage — although not as much as the library that pyright is currently using.

I'll need to do some additional investigation and consider the pros and cons of switching to a new toml parser library.

In the meantime, you can use pyright's native configuration file format "pyrightconfig.json" if you want to use newer syntax in your pyproject.toml file.

Stealthii commented 12 hours ago

Thanks @erictraut. I can confirm that pyright with the presence of a "pyrightconfig.json" file, no longer makes attempts to parse "pyproject.toml" and will function correctly.