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

Use PEP 621's [project.requires-python] field to automatically set --target-version #3124

Closed
GamePad64 opened this issue Jun 17, 2022 · 14 comments · Fixed by #3219
Closed

Use PEP 621's [project.requires-python] field to automatically set --target-version #3124

GamePad64 opened this issue Jun 17, 2022 · 14 comments · Fixed by #3219
Labels
C: configuration CLI and configuration C: target version Related to --target-version, e.g. autodetection S: accepted The changes in this design / enhancement issue have been accepted and can be implemented T: enhancement New feature or request

Comments

@GamePad64
Copy link

PEP 621 provides project.requires-python field in pyproject.toml. I expect, that most of the tools will support this field eventually.
Each code-related linter and formatter has a setting for supported python version.

Using this field would be good for tool interoperability in the future.

Related: #751 (comment)

@GamePad64 GamePad64 added the T: enhancement New feature or request label Jun 17, 2022
@ichard26 ichard26 added C: configuration CLI and configuration C: target version Related to --target-version, e.g. autodetection labels Jun 28, 2022
@ichard26
Copy link
Collaborator

I'm in favour.

@cooperlees
Copy link
Collaborator

When it makes sense, I'm in favor of moving everything we can to pyproject.toml long as it does not break mypyc etc. etc.

@ichard26 ichard26 changed the title Use PEP 621 for python version Use PEP 621's [project.requires-python] field to automatically set --target-version Jun 28, 2022
@stinodego
Copy link
Contributor

I looked into this. It should be easy enough to augment the parse_pyproject_toml function to also read the project.requires-python field.

However, parsing that field is another matter. We would probably have to rely on a third party library if we really want to do this properly. And I'm not sure it's worth introducing a dependency in order to be able to automatically infer the target version.

Input is appreciated!

@kdeldycke
Copy link

FWIW, I hacked something similiar in my GitHub workflow to feed pyupgrade with the appropriate --py3-plus / --py36-plus / (...) / --py311-plus option flags.

@SamirPS
Copy link

SamirPS commented Jul 31, 2022

@kdeldycke If a new version of python is out you will need to modify the file no ? Can we erase the check <= 11

@stinodego
Copy link
Contributor

I indeed looked at poetry-core for the version parsing. But if you wanted to use their version parser (it's quite extensive), you'd have to add it as a dependency, which feels quite bad.

@ssbarnea
Copy link
Contributor

ssbarnea commented Aug 2, 2022

I hope to see this feature being added and maybe in a way that it would make it easy to reuse in other tools as updating min-python in so many tools is a PITA and almost a guarantee that you will forget to update one of them.

@ichard26 ichard26 added the S: accepted The changes in this design / enhancement issue have been accepted and can be implemented label Aug 11, 2022
@ichard26
Copy link
Collaborator

We could also probably use packaging.specifiers.SpecifierSet to parse [project].python-requires. Not sure whether to hard depend on packaging or add it as an extra since it's not core Black functionality.

@stinodego
Copy link
Contributor

We could also probably use packaging.specifiers.SpecifierSet to parse [project].python-requires. Not sure whether to hard depend on packaging or add it as an extra since it's not core Black functionality.

packaging was a great suggestion - it does exactly what we need. I have a working implementation now; will open a PR when I have added tests.

I'll first add it as a hard dependency - we can adjust in code review if required.

@ichard26
Copy link
Collaborator

That's great, thank you for looking into this! I look forward to the PR :)

@hugovk
Copy link
Contributor

hugovk commented Aug 13, 2022

The Requires-Python metadata can be in pyproject.toml, setup.cfg or setup.py.

Would Black only support reading from pyproject.toml?

I usually put it in setup.cfg, but am fine if Black only wants to read pyproject.toml, because it's already doing so for other config. (And it may also drive further pyproject.toml adoption.)

@JelleZijlstra
Copy link
Collaborator

I wouldn't want Black to read other config files than pyproject.toml; that's asking for a lot of additional complexity.

kdeldycke added a commit to kdeldycke/workflows that referenced this issue Feb 2, 2023
@kdeldycke
Copy link

@kdeldycke If a new version of python is out you will need to modify the file no ? Can we erase the check <= 11

Hey @SamirPS, just noticed your comment. Since then I indeed removed the hard-coded check on Python <= 3.11. My code to produce Black's --target-version parameters is now future-proof.

Since #3219 has been released upstream in v23.1.0, we still need my custom code for Poetry-based projects. Black only looks for the PEP-621's requires-python marker in the pyproject.toml file. Not Poetry's [tool.poetry.dependencies] > python = "^3.7" specifiers.

@kdeldycke
Copy link

I hope to see this feature being added and maybe in a way that it would make it easy to reuse in other tools as updating min-python in so many tools is a PITA and almost a guarantee that you will forget to update one of them.

Agreed @ssbarnea. I had to write some code to produce consistent Python target parameters for Black, Mypy and Pyupgrade.

For more details, see my metadata.py script in my reuseable workflows that I try to use everywhere in my Python projects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: configuration CLI and configuration C: target version Related to --target-version, e.g. autodetection S: accepted The changes in this design / enhancement issue have been accepted and can be implemented T: enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants