Skip to content

Commit

Permalink
fix: abc & a few style issues (#617)
Browse files Browse the repository at this point in the history
* fix: ABC was applied to the wrong class

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* chore: update style and fix a few issues

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* fix: ABCMeta instead of ABC to avoid weakref on 3.6 issue

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii authored Oct 3, 2022
1 parent c80c64b commit 42d661d
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 51 deletions.
17 changes: 9 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ci:
repos:

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
rev: v4.3.0
hooks:
- id: check-added-large-files
- id: check-case-conflict
Expand All @@ -20,7 +20,7 @@ repos:
- id: trailing-whitespace

- repo: https://github.com/psf/black
rev: "22.3.0"
rev: "22.8.0"
hooks:
- id: black

Expand All @@ -30,32 +30,33 @@ repos:
- id: isort

- repo: https://github.com/asottile/pyupgrade
rev: "v2.31.1"
rev: "v2.38.2"
hooks:
- id: pyupgrade
args: ["--py36-plus"]

- repo: https://github.com/asottile/setup-cfg-fmt
rev: "v1.20.0"
rev: "v2.0.0"
hooks:
- id: setup-cfg-fmt
args: [--include-version-classifiers, --max-py-version=3.11]

- repo: https://github.com/hadialqattan/pycln
rev: v1.2.5
rev: v2.1.1
hooks:
- id: pycln
args: [--all]
stages: [manual]

- repo: https://github.com/pycqa/flake8
rev: "4.0.1"
rev: "5.0.4"
hooks:
- id: flake8
exclude: docs/conf.py
additional_dependencies: [flake8-bugbear, flake8-print, flake8-2020]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v0.942"
rev: "v0.981"
hooks:
- id: mypy
files: plumbum
Expand All @@ -70,7 +71,7 @@ repos:
stages: [manual]

- repo: https://github.com/codespell-project/codespell
rev: v2.1.0
rev: v2.2.1
hooks:
- id: codespell

Expand Down
4 changes: 2 additions & 2 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ def docs(session):

if session.posargs:
if "serve" in session.posargs:
print("Launching docs at http://localhost:8000/ - use Ctrl-C to quit")
session.log("Launching docs at http://localhost:8000/ - use Ctrl-C to quit")
session.run("python", "-m", "http.server", "8000", "-d", "_build/html")
else:
print("Unsupported argument to docs")
session.log("Unsupported argument to docs")


@nox.session
Expand Down
21 changes: 11 additions & 10 deletions plumbum/cli/image.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys

from plumbum import colors

from .. import cli
Expand Down Expand Up @@ -56,9 +58,10 @@ def show_pil(self, im):
for y in range(size[1]):
for x in range(size[0] - 1):
pix = new_im.getpixel((x, y))
print(colors.bg.rgb(*pix), " ", sep="", end="") # '\u2588'
print(colors.reset, " ", sep="")
print(colors.reset)
sys.stdout.write(colors.bg.rgb(*pix) + " ") # '\u2588'
sys.stdout.write(colors.reset + " \n")
sys.stdout.write(colors.reset + "\n")
sys.stdout.flush()

def show_pil_double(self, im):
"Show double resolution on some fonts"
Expand All @@ -71,14 +74,12 @@ def show_pil_double(self, im):
for x in range(size[0] - 1):
pix = new_im.getpixel((x, y * 2))
pixl = new_im.getpixel((x, y * 2 + 1))
print(
colors.bg.rgb(*pixl) & colors.fg.rgb(*pix),
"\u2580",
sep="",
end="",
sys.stdout.write(
(colors.bg.rgb(*pixl) & colors.fg.rgb(*pix)) + "\u2580"
)
print(colors.reset, " ", sep="")
print(colors.reset)
sys.stdout.write(colors.reset + " \n")
sys.stdout.write(colors.reset + "\n")
sys.stdout.flush()


class ShowImageApp(cli.Application):
Expand Down
13 changes: 7 additions & 6 deletions plumbum/cli/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ def done(self):
self.value = self.length
self.display()
if self.clear and not self.has_output:
print("\r", len(str(self)) * " ", "\r", end="", sep="")
sys.stdout.write("\r" + len(str(self)) * " " + "\r")
else:
print()
sys.stdout.write("\n")
sys.stdout.flush()

def __str__(self):
width = get_terminal_size(default=(0, 0))[0]
Expand Down Expand Up @@ -175,11 +176,11 @@ def __str__(self):
def display(self):
disptxt = str(self)
if self.width == 0 or self.has_output:
print(disptxt)
sys.stdout.write(disptxt + "\n")
else:
print("\r", end="")
print(disptxt, end="")
sys.stdout.flush()
sys.stdout.write("\r")
sys.stdout.write(disptxt)
sys.stdout.flush()


class ProgressIPy(ProgressBase): # pragma: no cover
Expand Down
2 changes: 1 addition & 1 deletion plumbum/colorlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def load_ipython_extension(ipython): # pragma: no cover
try:
from ._ipython_ext import OutputMagics # pylint:disable=import-outside-toplevel
except ImportError:
print("IPython required for the IPython extension to be loaded.")
print("IPython required for the IPython extension to be loaded.") # noqa: T201
raise

ipython.push({"colors": htmlcolors})
Expand Down
6 changes: 3 additions & 3 deletions plumbum/colorlib/styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import platform
import re
import sys
from abc import ABC, abstractmethod
from abc import ABCMeta, abstractmethod
from copy import copy
from typing import IO, Dict, Optional, Union

Expand Down Expand Up @@ -74,7 +74,7 @@ class ResetNotSupported(Exception):
for this Style."""


class Color(ABC):
class Color:
"""\
Loaded with ``(r, g, b, fg)`` or ``(color, fg=fg)``. The second signature is a short cut
and will try full and hex loading.
Expand Down Expand Up @@ -331,7 +331,7 @@ def limit_representation(self, val):
return self if self.representation <= val else self.to_representation(val)


class Style:
class Style(metaclass=ABCMeta):
"""This class allows the color changes to be called directly
to write them to stdout, ``[]`` calls to wrap colors (or the ``.wrap`` method)
and can be called in a with statement.
Expand Down
4 changes: 2 additions & 2 deletions plumbum/commands/processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def _shutdown_bg_threads():
global _shutting_down # pylint: disable=global-statement
_shutting_down = True
# Make sure this still exists (don't throw error in atexit!)
# TODO: not sure why this would be "falsy", though
# TODO: not sure why this would be "falsey", though
if _timeout_queue: # type: ignore[truthy-bool]
_timeout_queue.put((SystemExit, 0))
# grace period
Expand Down Expand Up @@ -367,7 +367,7 @@ def iter_lines(
assert mode in (BY_POSITION, BY_TYPE)

encoding = getattr(proc, "custom_encoding", None) or "utf-8"
decode = lambda s: s.decode(encoding, errors="replace").rstrip()
decode = lambda s: s.decode(encoding, errors="replace").rstrip() # noqa: E731

_register_proc_timeout(proc, timeout)

Expand Down
4 changes: 3 additions & 1 deletion plumbum/fs/atomic.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
from win32con import LOCKFILE_EXCLUSIVE_LOCK, LOCKFILE_FAIL_IMMEDIATELY
from win32file import OVERLAPPED, LockFileEx, UnlockFile
except ImportError:
print("On Windows, Plumbum requires Python for Windows Extensions (pywin32)")
print( # noqa: T201
"On Windows, Plumbum requires Python for Windows Extensions (pywin32)"
)
raise

@contextmanager
Expand Down
3 changes: 1 addition & 2 deletions plumbum/path/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,8 @@ def resolve(self, strict=False): # pylint:disable=unused-argument
@property
def parents(self):
"""Pathlib like sequence of ancestors"""
join = lambda x, y: self._form(x) / y
as_list = (
reduce(join, self.parts[:i], self.parts[0])
reduce(lambda x, y: self._form(x) / y, self.parts[:i], self.parts[0])
for i in range(len(self.parts) - 1, 0, -1)
)
return tuple(as_list)
Expand Down
11 changes: 7 additions & 4 deletions plumbum/path/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,13 @@ def with_suffix(self, suffix, depth=1):
return LocalPath(self.dirname) / (name + suffix)

def glob(self, pattern):
fn = lambda pat: [
LocalPath(m) for m in glob.glob(os.path.join(glob.escape(str(self)), pat))
]
return self._glob(pattern, fn)
return self._glob(
pattern,
lambda pat: [
LocalPath(m)
for m in glob.glob(os.path.join(glob.escape(str(self)), pat))
],
)

def delete(self):
if not self.exists():
Expand Down
10 changes: 6 additions & 4 deletions plumbum/path/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,12 @@ def with_suffix(self, suffix, depth=1):
return self.__class__(self.remote, self.dirname) / (name + suffix)

def glob(self, pattern):
fn = lambda pat: [
RemotePath(self.remote, m) for m in self.remote._path_glob(self, pat)
]
return self._glob(pattern, fn)
return self._glob(
pattern,
lambda pat: [
RemotePath(self.remote, m) for m in self.remote._path_glob(self, pat)
],
)

def delete(self):
if not self.exists():
Expand Down
2 changes: 1 addition & 1 deletion plumbum/typed_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class CSV(_BaseVar):
def __init__(
self, name, default=NO_DEFAULT, type=str, separator=","
): # pylint:disable=redefined-builtin
super(TypedEnv.CSV, self).__init__(name, default=default)
super().__init__(name, default=default)
self.type = type
self.separator = separator

Expand Down
10 changes: 8 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ classifiers =
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Programming Language :: Python :: 3.11
Topic :: Software Development :: Build Tools
Topic :: System :: Systems Administration
keywords =
Expand Down Expand Up @@ -88,8 +89,13 @@ exclude_lines =

[flake8]
max-complexity = 50
extend-ignore = E203, E501, E722, B950, E731
select = C,E,F,W,B,B9
extend-ignore = E203, E501, B950, T202
extend-select = B9
per-file-ignores =
tests/*: T
examples/*: T
experiments/*: T
plumbum/cli/application.py: T

[codespell]
ignore-words-list = ans,switchs,hart,ot,twoo,fo
Expand Down
10 changes: 5 additions & 5 deletions tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class TestValidator:
def test_named(self):
class Try:
@cli.positional(x=abs, y=str)
def main(selfy, x, y):
def main(selfy, x, y): # noqa: B902
pass

assert Try.main.positional == [abs, str]
Expand All @@ -14,7 +14,7 @@ def main(selfy, x, y):
def test_position(self):
class Try:
@cli.positional(abs, str)
def main(selfy, x, y):
def main(selfy, x, y): # noqa: B902
pass

assert Try.main.positional == [abs, str]
Expand All @@ -23,7 +23,7 @@ def main(selfy, x, y):
def test_mix(self):
class Try:
@cli.positional(abs, str, d=bool)
def main(selfy, x, y, z, d):
def main(selfy, x, y, z, d): # noqa: B902
pass

assert Try.main.positional == [abs, str, None, bool]
Expand All @@ -32,7 +32,7 @@ def main(selfy, x, y, z, d):
def test_var(self):
class Try:
@cli.positional(abs, str, int)
def main(selfy, x, y, *g):
def main(selfy, x, y, *g): # noqa: B902
pass

assert Try.main.positional == [abs, str]
Expand All @@ -41,7 +41,7 @@ def main(selfy, x, y, *g):
def test_defaults(self):
class Try:
@cli.positional(abs, str)
def main(selfy, x, y="hello"):
def main(selfy, x, y="hello"): # noqa: B902
pass

assert Try.main.positional == [abs, str]
Expand Down

0 comments on commit 42d661d

Please sign in to comment.