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

codespell unhappy with pytest config in pyproject.toml #2544

Closed
aucampia opened this issue Oct 19, 2022 · 4 comments · Fixed by #2545
Closed

codespell unhappy with pytest config in pyproject.toml #2544

aucampia opened this issue Oct 19, 2022 · 4 comments · Fixed by #2545

Comments

@aucampia
Copy link

When running codespell with this pyproject.toml I get the following error:

$ python -m poetry run codespell --version
2.2.2
$ python -m poetry run codespell
Traceback (most recent call last):
  File "/home/iwana/sw/d/github.com/aucampia/cookiecutter-python/.venv/bin/codespell", line 8, in <module>
    sys.exit(_script_main())
  File "/home/iwana/sw/d/github.com/aucampia/cookiecutter-python/.venv/lib/python3.10/site-packages/codespell_lib/_codespell.py", line 767, in _script_main
    return main(*sys.argv[1:])
  File "/home/iwana/sw/d/github.com/aucampia/cookiecutter-python/.venv/lib/python3.10/site-packages/codespell_lib/_codespell.py", line 772, in main
    options, parser = parse_options(args)
  File "/home/iwana/sw/d/github.com/aucampia/cookiecutter-python/.venv/lib/python3.10/site-packages/codespell_lib/_codespell.py", line 426, in parse_options
    config.read_dict(data)
  File "/usr/lib64/python3.10/configparser.py", line 755, in read_dict
    self.set(section, key, value)
  File "/usr/lib64/python3.10/configparser.py", line 1205, in set
    super().set(section, option, value)
  File "/usr/lib64/python3.10/configparser.py", line 895, in set
    value = self._interpolation.before_set(self, section, option,
  File "/usr/lib64/python3.10/configparser.py", line 402, in before_set
    raise ValueError("invalid interpolation syntax in %r at "
ValueError: invalid interpolation syntax in "{'testpaths': ['tests'], 'addopts': ['--cov-config=pyproject.toml', '--capture=no', '--tb=native', '-ra'], 'log_format': '%(asctime)s %(process)d %(thread)d %(levelno)03d:%(levelname)-8s %(name)-12s %(module)s:%(lineno)s:%(funcName)s %(message)s', 'log_date_format': '%Y-%m-%dT%H:%M:%S', 'log_cli_format': '%(asctime)s %(process)d %(thread)d %(levelno)03d:%(levelname)-8s %(name)-12s %(module)s:%(lineno)s:%(funcName)s %(message)s', 'log_cli_date_format': '%Y-%m-%dT%H:%M:%S'}" at position 123
@DimitriPapadopoulos
Copy link
Collaborator

DimitriPapadopoulos commented Oct 19, 2022

This looks like an issue with the configparser module of Python:

$ python3
Python 3.10.6 (main, Aug 10 2022, 11:40:04) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import configparser
>>> 
>>> data = {
...     'foo': {
...         'x': '%Y'
...     }
... }
>>> 
>>> config = configparser.ConfigParser()
>>> config.read_dict(data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.10/configparser.py", line 755, in read_dict
    self.set(section, key, value)
  File "/usr/lib/python3.10/configparser.py", line 1205, in set
    super().set(section, option, value)
  File "/usr/lib/python3.10/configparser.py", line 895, in set
    value = self._interpolation.before_set(self, section, option,
  File "/usr/lib/python3.10/configparser.py", line 402, in before_set
    raise ValueError("invalid interpolation syntax in %r at "
ValueError: invalid interpolation syntax in '%Y' at position 0
>>> 

See Interpolation of values.

@DimitriPapadopoulos
Copy link
Collaborator

DimitriPapadopoulos commented Oct 19, 2022

We used to read INI files with configparser.ConfigParser (setup.cfg and .codespellrc), now we also read a TOML file (pyproject.toml). But then, is configparser.ConfigParser able to read TOML files too? Is % supposed to be processed differently in INI files and TOML files? I honestly don't know, I don't have much experience with configparser.ConfigParser.

@DimitriPapadopoulos
Copy link
Collaborator

DimitriPapadopoulos commented Oct 19, 2022

File pyproject.toml does not escape %. Is this expected, or not? Are configuration files supposed to escape %?

Note that configparser.ConfigParser has a named parameter interpolation=BasicInterpolation():

Interpolation behaviour may be customized by providing a custom handler through the interpolation argument. None can be used to turn off interpolation completely, ExtendedInterpolation() provides a more advanced variant inspired by zc.buildout. More on the subject in the dedicated documentation section.

Unfortunately, it looks like there is no unique config file format – and no universal config file reader. So one has to try different formats with different interpolation schemes to read a config file, until it works. Ouch.

@DimitriPapadopoulos
Copy link
Collaborator

Thinking about it again, perhaps we should pass interpolation=None because we do not expect interpolation in the [codespell] section.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants