Skip to content

Commit

Permalink
Require black >= 19.3b0 (code cleanup)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjc committed Aug 8, 2019
1 parent f3b8a33 commit 3bfa2df
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 35 deletions.
7 changes: 4 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,17 @@ v0.1.1 *pending* - Option to use a (global) black configuration file,
`Tomasz Grining <https://github.com/098799>`_.
- Logs configuration files, use ``-v`` or ``--verbose``.
- Fixed flake8 "builtins" parameter warning.
v0.1.0 2019-06-03 - Uses main ``black`` settings from ``pyproject.toml``,
- Now requires black 19.3b0 or later.
v0.1.0 2019-06-03 - Uses main black settings from ``pyproject.toml``,
contribution from `Alex <https://github.com/ADKosm>`_.
- WARNING: Now ignores ``flake8`` max-line-length setting.
- WARNING: Now ignores flake8 ``max-line-length`` setting.
v0.0.4 2019-03-15 - Supports black 19.3b0 which changed a function call.
v0.0.3 2019-02-21 - Bug fix when ``W292 no newline at end of file`` applies,
contribution from
`Sapphire Becker <https://github.com/sapphire-janrain>`_.
v0.0.2 2019-02-15 - Document syntax error behaviour (no BLK error reported).
v0.0.1 2019-01-10 - Initial public release.
- Passes ``flake8`` max-line-length setting to ``black``.
- Passes flake8 ``max-line-length`` setting to black.
======= ============ ===========================================================


Expand Down
40 changes: 9 additions & 31 deletions flake8_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ def __init__(self, tree, filename="(none)"):
self.tree = tree
self.filename = filename
self.line_length = black.DEFAULT_LINE_LENGTH # Expect to be 88
# Following for legacy versions of black only,
# see property self._file_mode for new black versions:
self.file_mode = 0 # was: black.FileMode.AUTO_DETECT

@property
@lru_cache()
Expand Down Expand Up @@ -101,21 +98,12 @@ def _file_mode(self):
skip_string_normalization = black_config.get(
"skip_string_normalization", False
)
if skip_string_normalization:
# Used with older versions of black:
self.file_mode |= 4 # was black.FileMode.NO_STRING_NORMALIZATION
try:
# Recent versions of black have a FileMode object
# which includes the line length setting
return black.FileMode(
target_versions=target_versions,
line_length=self.line_length,
string_normalization=not skip_string_normalization,
)
except TypeError as e:
# Legacy mode for old versions of black
assert "got an unexpected keyword argument" in str(e), e
return None
# Requires black 19.3b0 or later:
return black.FileMode(
target_versions=target_versions,
line_length=self.line_length,
string_normalization=not skip_string_normalization,
)

@classmethod
def add_options(cls, parser):
Expand Down Expand Up @@ -159,19 +147,9 @@ def run(self):
elif source:
# Call black...
try:
if self._file_mode is None:
# Legacy version of black, 18.9b0 or older
new_code = black.format_file_contents(
source,
line_length=self.line_length,
fast=False,
mode=black.FileMode(self.file_mode),
)
else:
# For black 19.3b0 or later
new_code = black.format_file_contents(
source, mode=self._file_mode, fast=False
)
new_code = black.format_file_contents(
source, mode=self._file_mode, fast=False
)
except black.NothingChanged:
return
except black.InvalidInput:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ def get_version(fname="flake8_black.py"):
],
keywords="PEP8",
py_modules=["flake8_black"],
install_requires=["flake8 >= 3.0.0", "black"],
install_requires=["flake8 >= 3.0.0", "black >= 19.3b0"],
entry_points={"flake8.extension": ["BLK = flake8_black:BlackStyleChecker"]},
)

0 comments on commit 3bfa2df

Please sign in to comment.