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

Colon inside a range gets a colon added to it #1413

Closed
WhyNotHugo opened this issue May 15, 2020 · 7 comments
Closed

Colon inside a range gets a colon added to it #1413

WhyNotHugo opened this issue May 15, 2020 · 7 comments
Labels
T: bug Something isn't working

Comments

@WhyNotHugo
Copy link
Contributor

The colon before inside a range gets a space added to it:

input:

def a(code: str, pos: int):
    return code[pos: pos + 10]

output

def a(code: str, pos: int):
    return code[pos : pos + 10]
    #              ^ Note this space

This seems to be the only scenario in which black adds a space before a colon, so I'm assuming it's a bug. Flake8 immediately complains since that's, well, wrong.

This doesn't seem to happen if + 10 is not there.

Environment:

$ black --version
black, version 19.10b0
$ python --version
Python 3.8.2
$ uname -a
Linux aphrodite 5.6.12-arch1-1 #1 SMP PREEMPT Sun, 10 May 2020 10:43:42 +0000 x86_64 GNU/Linux

Does this bug also happen on master?:
Yes

@WhyNotHugo WhyNotHugo added the T: bug Something isn't working label May 15, 2020
@hugovk
Copy link
Contributor

hugovk commented May 15, 2020

This is documented in the README:

https://github.com/psf/black/blob/master/README.md#slices

@WhyNotHugo
Copy link
Contributor Author

But it would seem that this is not enforced consistently, eg:

def a(code: str, pos: int):
    x = code[pos : pos + 10]  # Spaces here
    y = code[pos:pos]  # No spaces here

@zsol
Copy link
Collaborator

zsol commented May 15, 2020

We should probably expand that paragraph a bit in the docs. PEP8 recommends no spaces around : for "simple" expressions, and extra space for "complex expressions". Black treats anything more than variable names as "complex".

@WhyNotHugo
Copy link
Contributor Author

Ah, this makes sense. I guess I'm just too used to flake8's syntax.

I think it would be good to mention, in "installation and usage", a basic configuration for flake8 that aligns with black. Would you guys accept a PR that describes this?

@WhyNotHugo
Copy link
Contributor Author

(I realise the individual settings are mentioned in different places, but I'm talking about a single-block copy-pasteable thing :) )

@zsol
Copy link
Collaborator

zsol commented May 15, 2020

FWIW here's the relevant code that implements this behavior:

black/src/black/__init__.py

Lines 1411 to 1413 in 45c98cf

leaf.prefix += whitespace(
leaf, complex_subscript=self.is_complex_subscript(leaf)
)

I would turn off flake8 warnings related to subscripts (in fact, related to all code formatting). #1371 added an example flake8 config. If you think that needs tuning, feel free to open a PR :)

BTW this is a duplicate of #889, #940, #1343, and possibly more. Thanks @hugovk for replying to all of them :)

@zsol zsol closed this as completed May 15, 2020
@WhyNotHugo
Copy link
Contributor Author

Oh, it has! Guess I have to read the updated README! Thanks, sorry for the noise!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants