Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling of toml through ConfigParser causes won't allow %s in pyproject.toml #2674

Closed
jshwi opened this issue Dec 27, 2022 · 2 comments
Closed

Comments

@jshwi
Copy link

jshwi commented Dec 27, 2022

Hi there,
I'm getting ValueError: invalid interpolation syntax in '%s' at position x if I have %s in my pyproject.toml
Steps to reproduce

  • add example tool
# ./pyproject.toml
[tool.some_tool.ignore_strings_from]
"package/module.py" = [
    "miss: %s",
]
  • run codespell
$ codespell
Traceback (most recent call last):
  File "/Users/swhitlock/Library/Caches/pypoetry/virtualenvs/pyaud-MdtoMGJ6-py3.8/bin/codespell", line 8, in <module>
    sys.exit(_script_main())
  File "/Users/swhitlock/Library/Caches/pypoetry/virtualenvs/pyaud-MdtoMGJ6-py3.8/lib/python3.8/site-packages/codespell_lib/_codespell.py", line 767, in _script_main
    return main(*sys.argv[1:])
  File "/Users/swhitlock/Library/Caches/pypoetry/virtualenvs/pyaud-MdtoMGJ6-py3.8/lib/python3.8/site-packages/codespell_lib/_codespell.py", line 772, in main
    options, parser = parse_options(args)
  File "/Users/swhitlock/Library/Caches/pypoetry/virtualenvs/pyaud-MdtoMGJ6-py3.8/lib/python3.8/site-packages/codespell_lib/_codespell.py", line 426, in parse_options
    config.read_dict(data)
  File "/Users/swhitlock/.pyenv/versions/3.8.13/lib/python3.8/configparser.py", line 754, in read_dict
    self.set(section, key, value)
  File "/Users/swhitlock/.pyenv/versions/3.8.13/lib/python3.8/configparser.py", line 1201, in set
    super().set(section, option, value)
  File "/Users/swhitlock/.pyenv/versions/3.8.13/lib/python3.8/configparser.py", line 894, in set
    value = self._interpolation.before_set(self, section, option,
  File "/Users/swhitlock/.pyenv/versions/3.8.13/lib/python3.8/configparser.py", line 402, in before_set
    raise ValueError("invalid interpolation syntax in %r at "
ValueError: invalid interpolation syntax in '%s' at position 0

So it appears that ConfigParser cannot handle the dictionary it is given

def parse_options(
    args: Sequence[str],
) -> Tuple[...]:
    parser = argparse.ArgumentParser(formatter_class=NewlineHelpFormatter)
    ...
    for toml_file, raise_error in toml_files_errors:
        try:
            import tomli
        except Exception as exc:
            ...
        with open(toml_file, "rb") as f:
            data = tomli.load(f).get("tool", {})
        config.read_dict(data)

This is a problem, because the [tool.codespell] key does not need to be parsed for this to happen
If the syntax wasn't allowed in codespell that would be easier to mitigate

I propose that the tool.codespell key is parsed before it is passed to ConfigParser, if it must?

@DimitriPapadopoulos
Copy link
Collaborator

I believe this is a duplicate of #2544. Does it work better after applying #2545?

@jshwi
Copy link
Author

jshwi commented Dec 27, 2022

Hi @DimitriPapadopoulos,
Sorry for opening a dupe, I was using the latest version for my pre-commit rev key, and not seeing any open issues I didn't know this had been spotted yet
This now works, thank you!

  - repo: https://github.com/codespell-project/codespell
    rev: "b11cd233331056e7a30b42e065b2d3500627a692"
    hooks:
      - id: codespell
        exclude: ^(.*/messages\.po)$
        additional_dependencies:
          - tomli==2.0.1

@jshwi jshwi closed this as completed Dec 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants