black and isort incompatibility within stub files

Created on 7 May 2019  路  10Comments  路  Source: psf/black

from tokenize import TokenInfo
from typing import Iterator, List

def replacetokens(tokens: List[TokenInfo], fullname: str) -> Iterator[TokenInfo]: ...
def apply_job_security(code: str) -> str: ...

In this case, black wants to put one line, while isort enforces two.

bug

Most helpful comment

I'll close this then as it stands.

All 10 comments

Workaround for now:

from tokenize import TokenInfo
from typing import Iterator, List

# needed to bypass https://github.com/python/black/issues/837

def replacetokens(tokens: List[TokenInfo], fullname: str) -> Iterator[TokenInfo]: ...
def apply_job_security(code: str) -> str: ...

This is probably fixable with a special case in EmptyLineTracker._maybe_empty_lines that adds two newlines between imports and def lines even in stub files, but honestly I'd rather implement import sorting in Black itself.

This is fully configurable in isort, however, isn't isort correct here according to pep8?

In fact, isn't black being self inconsistent, for example black produces this:

from tokenize import TokenInfo
from typing import Iterator, List


def function():
    pass

Adding the extra line if I remove it.
On what basis is black deciding to only use one line above?

This is specifically in stub files (.pyi), which use a different, more compact coding style.

@JelleZijlstra this makes sense, is this the standard for pyi files (the lack of the new line), is there an equivalent to pep8 for these files?

The closest equivalent is the typeshed coding style: https://github.com/python/typeshed/blob/master/CONTRIBUTING.md#stub-file-coding-style.

@JelleZijlstra thank you! With the additional information, I don't think this is an issue within Black. I'll have a resolution in isort shortly.

~Timothy

Thank you!

With version 4.3.19 isort has implemented black's behaviour for stub files

I'll close this then as it stands.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lig picture lig  路  84Comments

mhham picture mhham  路  22Comments

rouge8 picture rouge8  路  20Comments

kindjacket picture kindjacket  路  21Comments

devxpy picture devxpy  路  70Comments