psf / black

The uncompromising Python code formatter
https://black.readthedocs.io/en/stable/
MIT License
38.34k stars 2.42k forks source link

Inference of target versions from `requires-python` config is incorrect. #3581

Open manueljacob opened 1 year ago

manueljacob commented 1 year ago

3219 introduced inference of the target versions for black from the requires-python project metadata in pyproject.toml. However, it was not implemented correctly.

The rules for the comparison operators in requires-python is documented in PEP 440.

For example, >3.7,<3.10 currently results in the inference of ["py38", "py39"]. However, 3.7.1 is in >3.7,<3.10, so "py37" should be included as well.

>3.7.0,<3.10 results in the inference of ["py37", "py38", "py39"], which is correct. However, >3.7.0,<3.10 is equivalent to >3.7,<3.10, so they should give the same results.

I re-implemented the inference code. I will open a pull request as soon as I clarified and commented the code.

manueljacob commented 1 year ago

Currently, we treat empty requires-python metadata as if it was not present. But technically, it’s a version specifier with no clauses, so it contains all Python versions. On one hand, if we want to respect the configuration and be consistent, we should treat empty requires-python metadata as “all Python versions”. On the other hand, empty requires-python metadata is very non-explicit, so falling back to per-file auto-detection doesn’t seem too wrong, either. What do you think?

stinodego commented 1 year ago

You're right, >3.7 should be treated as >3.7.0, so it should include py37. That's an oversight on my part. Great that you're working on a fix 👍 should be a small adjustment.