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

PEP 614 support #1717

Merged
merged 10 commits into from Sep 19, 2020
Merged

PEP 614 support #1717

merged 10 commits into from Sep 19, 2020

Conversation

QuentinSoubeyran
Copy link
Contributor

@QuentinSoubeyran QuentinSoubeyran commented Sep 18, 2020

Adds support for PEP 614 to fix #1711. This is still WIP

Changes

  • updated typed-ast to 1.4.1 so that pipenv install --dev doesn't fail within a python 3.9 venv due to compile errors
  • changed the definition ofdecorator in blib2to3/Grammar.txt to reflect the relaxed decorator syntax
  • added a PY39 target
  • added a RELAXED_DECORATORS feature
  • added an eager detection of the above feature in get_feature_used(), with helper functions
  • moved PY36_VERSIONS from src/black/__init__.py to tests/test_black.py
  • added test for the detection of RELAXED_DECORATORS feature
  • added test for python 3.9 decorator formatting

Note

  • --safe doesn't work on python 3.9 code from black running on python <=3.8.5. This might already be the case with python 3.8 code formatted from black running on python 3.7, because typed_ast doesn't support python 3.8+ syntax. So older python version cannot parse the syntax with either the builtin ast or typed_ast

@QuentinSoubeyran
Copy link
Contributor Author

I'm looking for help on the space after @ bug, I have no idea where this is added in the code. My hypothesis is that all expression get a space before, I'd like to special-case that so it doesn't happens if the previous character is @ and we are in a decorator (so that the @ as __matmul__ operator is still properly spaced)

@zsol
Copy link
Collaborator

zsol commented Sep 18, 2020

For the whitespace issue, this will need to be handled in whitespace around here: https://github.com/psf/black/blob/master/src/black/__init__.py#L2259

Please add a couple of explicit test cases for the feature detection, as well as some more complex decorator examples.



def is_simple_decorator_expression(node: LN) -> bool:
"""Return True iff `node` is the 'dotted name' in the grammar @ dotted_name [arguments] NEWLINE"""
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit

Suggested change
"""Return True iff `node` is the 'dotted name' in the grammar @ dotted_name [arguments] NEWLINE"""
"""Return True iff `node` is the 'namedexpr_test' in the grammar @ namedexpr_test [arguments] NEWLINE"""

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tried to clarify that this function does test against a dotted_name grammar. Is this better ?

@zsol
Copy link
Collaborator

zsol commented Sep 18, 2020

Also feel free to put this PR in draft mode and ping me explicitly if you need more help until you're ready to publish it :)

@QuentinSoubeyran
Copy link
Contributor Author

I should have checked here earlier, I just found the whitespace() function by using pdb. Thanks anyway :)

@QuentinSoubeyran QuentinSoubeyran marked this pull request as draft September 18, 2020 20:56
@QuentinSoubeyran
Copy link
Contributor Author

Hi @zsol , I'm not sure how to handle the following: the --safe option doesn't work on python 3.9 code when black run on python 3.8. (tested on 3.8.2 and 3.8.5)
This is because parse_ast uses the builtin ast module, which cannot parse 3.9 code in python 3.8.5. Judging from the grammar on the ast doc, where decorator list are expr*, this might be fixed in python 3.8.6.

Still, python 3.9 code cannot be safely reformatted used python <=3.8.5. What is the best way to handle that ? Should I introduce a call to ast3.parse(..., feature_version=9) in parse_ast to fix that ?

@QuentinSoubeyran
Copy link
Contributor Author

Also, I tried running black-primer, but it fails for some projects (hypothesis, poetry, pyramid, pytest, sqlalchemy). Should I just skip them as hinted in the contribution guide, or should I do something about that ?

@zsol
Copy link
Collaborator

zsol commented Sep 19, 2020

The idea behind that parse_ast function is to use the builtin ast module on the latest supported python version. That will be 3.9 with this PR, so feel free to change the conditions so that

  • on 3.9 we use the builtin ast module
  • on 3.6 3.7 and 3.8 we use typed_ast

@zsol
Copy link
Collaborator

zsol commented Sep 19, 2020

I'm not sure about primer. What kinds of changes are there? CI looks green

@QuentinSoubeyran
Copy link
Contributor Author

QuentinSoubeyran commented Sep 19, 2020

I changed typed_ast as described. Curiously, this makes some unit test fail on python3.8, while everything works fine on python3.9. Perhaps typed_ast doesn't lke PEP570, PEP572 and stared returns (those are the unit test that fail due to SyntaxError).

Sorry I forgot the details about black-primer. I didn't look at all changes individually, but those I saw were arguments that weresplitted on multiline, or sometime just adding the trailing comma in already-splitted arguments.

@QuentinSoubeyran
Copy link
Contributor Author

Just found out that typed_ast does not support python 3.8 syntax and won't support it. So I guess I should revert the above commit ? To parse python 3.8, we need builtin the ast, so we need python >=3.8.

I think --safe would not work for python 3.8 code formatted from python 3.7 due to that, so black run on python3.8 not formatting python 3.9 might be acceptable

@QuentinSoubeyran
Copy link
Contributor Author

I went through all changes reported by black-primer, those are all one of:

  • arguments split on multiline due to long line
  • arguments split on multiline due to magic trailing comma
  • a docstring reformat to remove spaces after """
  • in sqlalchemy, reformat due to the call chain style

All in all, every formatting I saw is due to a well-defined rule explained in the code style, so I think this PR is ready for review

@QuentinSoubeyran QuentinSoubeyran marked this pull request as ready for review September 19, 2020 14:01
@QuentinSoubeyran QuentinSoubeyran changed the title [WIP] PEP 614 support PEP 614 support Sep 19, 2020
Copy link
Collaborator

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Looks good apart from one nit

# Those before the 'output' comment are valid under the old syntax
# Those after the 'ouput' comment require PEP614 relaxed syntax
# Do not remove the double # separator before the first test case, it allows
# to the comment before the test case
Copy link
Collaborator

Choose a reason for hiding this comment

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

remove "to"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I also forgot the verb. It should be fixed now. Thanks !

Copy link
Collaborator

@zsol zsol left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for taking the time to work on this

@QuentinSoubeyran
Copy link
Contributor Author

My pleasure :) Thanks to you two for your help. And thank everyone for Black, it is awesome !

@JelleZijlstra JelleZijlstra merged commit 6dddbd7 into psf:master Sep 19, 2020
noxan pushed a commit to noxan/black that referenced this pull request Jun 6, 2021
gnawhleinad added a commit to gnawhleinad/mega-linter that referenced this pull request Jun 9, 2021
https://pypi.org/project/black/ 21.5b2 was released on May 31, 2021.

The current version 20.8 was released ~August 2020. `py39` was added in
September 2020 with psf/black#1717.
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.

[python 3.9] black doesn't support PEP 614
3 participants