pypa / setuptools

Official project repository for the Setuptools build system
https://pypi.org/project/setuptools/
MIT License
2.34k stars 1.14k forks source link

[debt] `EncodingWarning` when using `pytest`'s `tmpdir.join(...).write(...)` #4326

Open abravalheri opened 3 weeks ago

abravalheri commented 3 weeks ago

In the last CI tests we can see the following warnings:

setuptools/tests/config/test_setupcfg.py: 54 warnings
setuptools/tests/test_bdist_egg.py: 2 warnings
setuptools/tests/test_dist.py: 1 warning
setuptools/tests/test_easy_install.py: 1 warning
setuptools/tests/test_setuptools.py: 2 warnings
setuptools/tests/test_packageindex.py: 1 warning
setuptools/tests/test_virtualenv.py: 1 warning
  /home/runner/work/setuptools/setuptools/.tox/py/lib/python3.11/site-packages/_pytest/_py/error.py:87: EncodingWarning: 'encoding' argument not specified
    return func(*args, **kwargs)

This seems to came from the fact that we are using tmpdir.join(...)write(...). This method does not allow to specify the encoding argument.

We could replace it with write_text(..., encoding="utf-8"). However the tmpdir fixture itself is deprecated, so another possibility would be moving to tmp_dir.

Part of https://github.com/pypa/setuptools/issues/3810.

abravalheri commented 3 weeks ago

Since tmpdir is a legacy API and there is already support for encoding in write_text(), I think it is unlikely this will change upstream.

Avasam commented 3 weeks ago

LocalPath.open and LocalPath.write_text both support setting an encoding. The only advantage of write over write_text, is that in non-b mode it'll automatically stringify the data. https://github.com/pytest-dev/pytest/blob/main/src/_pytest/_py/path.py#L903

So we have two paths forward:

  1. Update the fixture (should probably be done anyway)
  2. Change write for write_text and add encoding where it's still missing.

Either way, I've added a todo comment in https://github.com/pypa/setuptools/pull/4255

(note: small typos in issue title: "debit" --> "debt", and missing . before "write")