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

Add --target-version option to allow users to choose targeted Python versions #618

Merged
merged 24 commits into from Feb 7, 2019

Conversation

JelleZijlstra
Copy link
Collaborator

@JelleZijlstra JelleZijlstra commented Nov 23, 2018

This generalizes the --py36 flag to allow the user to specify exactly which Python versions should be supported by the code Black produces. For example, you can now say black -t cpy27 -t pypy35 setup.py to produce code that is compatible with Python 2.7 and PyPy 3.5.

Advantages of this approach include:

I haven't yet fully tested all the ways this could be used, but wanted to put it up to see if others (@ambv, @zsol) are OK with this approach.

@coveralls
Copy link

coveralls commented Nov 23, 2018

Pull Request Test Coverage Report for Build 896

  • 288 of 294 (97.96%) changed or added relevant lines in 3 files are covered.
  • 5 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+4.3%) to 95.911%

Changes Missing Coverage Covered Lines Changed/Added Lines %
black.py 93 99 93.94%
Files with Coverage Reduction New Missed Lines %
tests/test_black.py 5 97.72%
Totals Coverage Status
Change from base Build 879: 4.3%
Covered Lines: 3002
Relevant Lines: 3130

💛 - Coveralls

line_length: int = DEFAULT_LINE_LENGTH
numeric_underscore_normalization: bool = True
string_normalization: bool = True
is_pyi: bool = False
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you thought about folding is_pyi into target_versions?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm not sure it's worth it though, since pyi mode is not quite like a Python version: you can't target say "pyi and CPython 3.6", it's always just pyi and nothing else.

"""Detect the version to target based on the nodes used."""
features = get_features_used(node)
return {
version for version in TargetVersion if features <= VERSION_TO_FEATURES[version]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful ❤️

blackd.py Outdated
except (KeyError, ValueError):
return web.Response(
status=400,
text=f"Invalid value for {PYTHON_VARIANT_HEADER}",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's have a more specific error message here. End users will likely encounter this if they misconfigure their clients

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do

blackd.py Outdated
)
for version in value.split(","):
tag = "cpy"
if version.startswith("cpy"):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like the explicit parsing here. Is it possible to try to match on all possible enum values here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's not great. I tried to work with the following constraints:

  • Continue accepting everything the previous code accepted (for backward compatibility). That's why there's the default if the user just put "3" or "2" as the version.
  • Don't be too liberal in what I accept, because then future backward compatibility will bind us to continue accepting it.
  • Avoid exposing our internal enum values too much. Users shouldn't have to care that we have a separate PyPy 3.5 version, but not one for other PyPy versions.
    I'll see what I can change but I'm not sure there's much more I can do keeping these constraints.

@JelleZijlstra
Copy link
Collaborator Author

There's some issue with asyncio in 3.8 that I don't fully understand now—I wonder whether it's even related to this diff, or whether it's just 3.8 instability.

@asvetlov
Copy link

asvetlov commented Feb 6, 2019

The observed issue is an aiohttp bug.
I have a plan to fix it in the next aiohttp 3.5.x release

@JelleZijlstra
Copy link
Collaborator Author

Thanks for confirming! I was thinking in the same direction.

The reason it came up in this PR is that currently we actually skip all blackd tests because aiohttp-cors, which was recently introduced as a dependency, is missing from setup.py. I added the missing dep in this PR (not sure why). I backed it out and will make the change in a separate PR.

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 this pull request may close these issues.

None yet

4 participants