Skip to content

Commit

Permalink
Update ruff and typings (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 committed Nov 20, 2023
1 parent 52d2fcb commit ffa5328
Show file tree
Hide file tree
Showing 23 changed files with 84 additions and 120 deletions.
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ repos:
- id: mdformat

- repo: https://github.com/pre-commit/mirrors-prettier
rev: "v3.0.3"
rev: "v3.1.0"
hooks:
- id: prettier
types_or: [yaml, html, json]
Expand All @@ -49,7 +49,7 @@ repos:
args: ["-L", "sur,nd"]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.6.1"
rev: "v1.7.0"
hooks:
- id: mypy
files: hatch_jupyter_builder
Expand All @@ -65,7 +65,7 @@ repos:
- id: rst-inline-touching-normal

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.5
rev: v0.1.6
hooks:
- id: ruff
types_or: [python, jupyter]
Expand All @@ -74,7 +74,7 @@ repos:
types_or: [python, jupyter]

- repo: https://github.com/scientific-python/cookie
rev: "2023.10.27"
rev: "2023.11.17"
hooks:
- id: sp-repo-review
additional_dependencies: ["repo-review[cli]"]
12 changes: 6 additions & 6 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
]

try:
import enchant # type:ignore # noqa
import enchant

extensions += ["sphinxcontrib.spelling"]
except ImportError:
Expand Down Expand Up @@ -75,12 +75,12 @@
# html_static_path = ["_static"]


import os.path as osp
import shutil
from pathlib import Path

HERE = osp.abspath(osp.dirname(__file__))
HERE = Path(__file__).parent.resolve()


def setup(app):
dest = osp.join(HERE, "source", "reference", "changelog.md")
shutil.copy(osp.join(HERE, "..", "CHANGELOG.md"), dest)
def setup(_):
dest = HERE / "source" / "reference" / "changelog.md"
shutil.copy(HERE / ".." / "CHANGELOG.md", dest)
2 changes: 1 addition & 1 deletion hatch_jupyter_builder/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .utils import is_stale, npm_builder # noqa F401
from .utils import is_stale, npm_builder

__version__ = "0.8.3"
14 changes: 8 additions & 6 deletions hatch_jupyter_builder/compare_migrated/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,29 @@
import sys
import tarfile
import zipfile
from pathlib import Path


def build_file(dirname: str, dist_name: str) -> None:
"""Build a dist file in a directory."""
orig_dir = os.getcwd()
orig_dir = Path.cwd()
os.chdir(dirname)
if os.path.exists("dist"):
if Path("dist").exists():
shutil.rmtree("dist")
subprocess.check_call([sys.executable, "-m", "build", f"--{dist_name}"])
os.chdir(orig_dir)


def get_tar_names(dirname: str) -> set[str]:
"""Get the tarball names in a directory."""
dist_file = glob.glob(f"{dirname}/dist/*.tar.gz")[0]
dist_file = glob.glob(f"{dirname}/dist/*.tar.gz")[0] # noqa: PTH207
tarf = tarfile.open(dist_file, "r:gz")
return set(tarf.getnames())


def get_zip_names(dirname: str) -> set[str]:
"""Get the zip (wheel) file names in a directory."""
wheel_file = glob.glob(f"{dirname}/dist/*.whl")[0]
wheel_file = glob.glob(f"{dirname}/dist/*.whl")[0] # noqa: PTH207
with zipfile.ZipFile(wheel_file, "r") as f:
return set(f.namelist())

Expand All @@ -40,10 +41,11 @@ def filter_file(path: str) -> bool:
"""Filter a file path for interesting files."""
if "egg-info" in path:
return True
_, ext = os.path.splitext(path)
path_obj = Path(path)
ext = path_obj.suffix
if not ext:
return True
if os.path.basename(path) in [path, "setup.py", "setup.cfg", "MANIFEST.in"]:
if path_obj.name in [path, "setup.py", "setup.cfg", "MANIFEST.in"]:
return True
return False

Expand Down
13 changes: 7 additions & 6 deletions hatch_jupyter_builder/migrate/_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@
# Run the jupyter-packaging migration script - must be done after
# hatch migration to avoid conflicts.
logger.info("Running jupyter-packaging migration")
here = os.path.abspath(os.path.dirname(__file__))
here = Path(__file__).parent.resolve()
prev_pythonpath = os.environ.get("PYTHONPATH", "")
if prev_pythonpath:
os.environ["PYTHONPATH"] = f"{here}{os.pathsep}{prev_pythonpath}"
else:
os.environ["PYTHONPATH"] = here
os.environ["PYTHONPATH"] = str(here)
subprocess.run([sys.executable, "setup.py", "--version"], capture_output=True, check=False)
os.environ["PYTHONPATH"] = prev_pythonpath

Expand All @@ -98,7 +98,7 @@

if matches:
Path(".flake8").write_text("\n".join(flake8) + "\n", "utf-8")
subprocess.run(["git", "add", ".flake"], check=False) # noqa
subprocess.run(["git", "add", ".flake"], check=False)


# Migrate and remove unused config.
Expand Down Expand Up @@ -233,8 +233,9 @@

# Remove old files
for fname in ["MANIFEST.in", "setup.cfg"]:
if os.path.exists(fname):
os.remove(fname)
fpath = Path(fname)
if fpath.exists():
fpath.unlink()

# Write out the new config.
logger.info("\n\nWriting pyproject.toml")
Expand All @@ -244,6 +245,6 @@
logger.info("\n\nWarning!! Not everything could be migrated automatically.")
logger.info("Please address the following concerns:")
for warning in warnings:
logger.info(f" - {warning}")
logger.info(" - %s", warning)

logger.info("\n\nMigration complete!")
6 changes: 3 additions & 3 deletions hatch_jupyter_builder/migrate/jupyter_packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def _write_config(path: Any, data: Any) -> None:

def _normalize_path(path: str) -> str:
path = str(path)
cwd = os.getcwd()
cwd = os.getcwd() # noqa: PTH109
if path.startswith(cwd):
return os.path.relpath(path, cwd)
return path
Expand Down Expand Up @@ -119,8 +119,8 @@ def create_cmdclass(
shared_data = {}
if data_files_spec is not None:
for path, dname, pattern in data_files_spec:
if os.path.isabs(dname):
dname = os.path.relpath(dname, os.getcwd()) # noqa
if Path(dname).is_absolute():
dname = os.path.relpath(dname, os.getcwd()) # noqa: PTH109, PLW2901
if pattern == "**":
shared_data[dname] = path
else:
Expand Down
10 changes: 5 additions & 5 deletions hatch_jupyter_builder/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ class JupyterBuildHook(BuildHookInterface[JupyterBuildConfig]):
PLUGIN_NAME = "jupyter-builder"
_skipped = False

def initialize(self, version: str, build_data: dict[str, t.Any]) -> None:
def initialize(self, version: str, _: dict[str, t.Any]) -> None:
"""Initialize the plugin."""
self._skipped = False
log = _get_log()
log.info("Running jupyter-builder")
if self.target_name not in ["wheel", "sdist"]:
log.info(f"ignoring target name {self.target_name}")
log.info("ignoring target name %s", self.target_name)
self._skipped = True
return

Expand Down Expand Up @@ -83,13 +83,13 @@ def initialize(self, version: str, build_data: dict[str, t.Any]) -> None:
if not should_skip_build and config.build_function:
build_func = get_build_func(config.build_function)
build_kwargs = normalize_kwargs(build_kwargs)
log.info(f"Building with {config.build_function}")
log.info(f"With kwargs: {build_kwargs}")
log.info("Building with %s", config.build_function)
log.info("With kwargs: %s", build_kwargs)
try:
build_func(self.target_name, version, **build_kwargs)
except Exception as e:
if version == "editable" and config.optional_editable_build.lower() == "true":
warnings.warn(f"Encountered build error:\n{e}") # noqa B028
warnings.warn(f"Encountered build error:\n{e}", stacklevel=2)
else:
raise e
else:
Expand Down
18 changes: 9 additions & 9 deletions hatch_jupyter_builder/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def list2cmdline(cmd_list: Any) -> str:


def _get_log() -> logging.Logger:
global _logger # noqa
global _logger # noqa: PLW0603
if _logger:
return _logger # type:ignore[unreachable]
_logger = logging.getLogger(__name__)
Expand All @@ -34,7 +34,7 @@ def _get_log() -> logging.Logger:


def npm_builder(
target_name: str,
target_name: str, # noqa: ARG001
version: str,
path: str = ".",
build_dir: str | None = None,
Expand Down Expand Up @@ -217,7 +217,7 @@ def normalize_kwargs(kwargs: Mapping[str, Any]) -> dict[str, Any]:
result = {}
for key, value in kwargs.items():
if isinstance(value, bool):
value = str(value) # noqa
value = str(value) # noqa: PLW2901
result[key.replace("-", "_")] = value
return result

Expand All @@ -227,7 +227,7 @@ def run(cmd: str | list[Any], **kwargs: Any) -> int:
kwargs.setdefault("shell", os.name == "nt")
cmd = normalize_cmd(cmd)
log = _get_log()
log.info(f"> {list2cmdline(cmd)}")
log.info("> %s", list2cmdline(cmd))
return subprocess.check_call(cmd, **kwargs)


Expand All @@ -244,7 +244,7 @@ def should_skip(skip_if_exists: Any) -> bool:
"""Detect whether all the paths in skip_if_exists exist"""
if not isinstance(skip_if_exists, list) or not len(skip_if_exists):
return False
return all(os.path.exists(p) for p in skip_if_exists)
return all(Path(p).exists() for p in skip_if_exists)


def install_pre_commit_hook() -> None:
Expand All @@ -257,18 +257,18 @@ def install_pre_commit_hook() -> None:
exec "$INSTALL_PYTHON" -m pre_commit "${{ARGS[@]}}"
"""
log = _get_log()
if not os.path.exists(".git"):
if not Path(".git").exists():
log.warning("Refusing to install pre-commit hook since this is not a git repository")
return

path = Path(".git/hooks/pre-commit")
if not path.exists():
log.info("Writing pre-commit hook")
with open(path, "w") as fid:
with path.open("w") as fid:
fid.write(data)
else:
log.warning("Refusing to overwrite pre-commit hook")

mode = os.stat(path).st_mode
mode = path.stat().st_mode
mode |= (mode & 0o444) >> 2 # copy R bits to X
os.chmod(path, mode)
path.chmod(mode)
77 changes: 28 additions & 49 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,67 +106,47 @@ line-length = 100

[tool.ruff.lint]
select = [
"A", "B", "C", "DTZ", "E", "EM", "F", "FBT", "I", "ICN", "N",
"PLC", "PLE", "PLR", "PLW", "Q", "RUF", "S", "SIM", "T", "TID", "UP",
"W", "YTT",
"B", # flake8-bugbear
"I", # isort
"ARG", # flake8-unused-arguments
"C4", # flake8-comprehensions
"EM", # flake8-errmsg
"ICN", # flake8-import-conventions
"G", # flake8-logging-format
"PGH", # pygrep-hooks
"PIE", # flake8-pie
"PL", # pylint
"PT", # flake8-pytest-style
"PTH", # flake8-use-pathlib
"RET", # flake8-return
"RUF", # Ruff-specific
"SIM", # flake8-simplify
"T20", # flake8-print
"UP", # pyupgrade
"YTT", # flake8-2020
"EXE", # flake8-executable
"NPY", # NumPy specific rules
"PD", # pandas-vet
"PYI", # flake8-pyi
]
ignore = [
# Allow non-abstract empty methods in abstract base classes
"B027",
# Ignore McCabe complexity
"C901",
# Allow boolean positional values in function calls, like `dict.get(... True)`
"FBT003",
# Use of `assert` detected
"S101",
# Line too long
"E501",
# Relative imports are banned
"TID252",
# Boolean ... in function definition
"FBT001",
"FBT002",
# Module level import not at top of file
"E402",
# A001/A002/A003 .. is shadowing a python builtin
"A001",
"A002",
"A003",
# Possible hardcoded password
"S105",
"S106",
# Variable `xxx` in function should be lowercase
"N806",
# Exception name `KernelSessionRecordConflict` should be named with an Error suffix
"N818",
# SIM105 Use `contextlib.suppress(...)`
"SIM105",
# PLR0913 Too many arguments to function call
"PLR0913",
# PLR0912 Too many branches
"PLR0912",
# S603 `subprocess` call: check for execution of untrusted input
"S603"
"PLR", # Design related pylint codes
"B027", # Allow non-abstract empty methods in abstract base classes
"SIM105", # SIM105 Use `contextlib.suppress(...)`
]
unfixable = [
# Don't touch print statements
"T201",
# Don't touch unused imports
"F401",
# Don't touch noqa lines
"RUF100",
]

[tool.ruff.lint.per-file-ignores]
# B011 Do not call assert False since python -O removes these calls
# F841 local variable 'foo' is assigned to but never used
# C408 Unnecessary `dict` call
# E402 Module level import not at top of file
# T201 `print` found
# EM101 Exception must not use a string literal
# PLR2004 Magic value used in comparison
# B018 Found useless expression
"tests/*" = ["B011", "B018", "F841", "C408", "E402", "T201", "EM101", "EM102", "EM103", "PLR2004"]
# PT011 `pytest.raises(ValueError)` is too broad
"tests/*" = ["B011", "B018", "C4", "T201", "PTH", "EM", "PT011", "ARG"]

[tool.pytest.ini_options]
minversion = "6.0"
Expand Down Expand Up @@ -196,7 +176,6 @@ source = ["hatch_jupyter_builder"]
[tool.mypy]
python_version = "3.8"
strict = true
show_error_codes = true
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
warn_unreachable = true
files = ["hatch_jupyter_builder"]
Expand All @@ -212,4 +191,4 @@ fail-under=100
exclude = ["docs", "tests"]

[tool.repo-review]
ignore = ["PY007", "PP308", "GH102"]
ignore = ["GH102"]

0 comments on commit ffa5328

Please sign in to comment.