Skip to content

Commit

Permalink
chore: use ruff as linter and formater
Browse files Browse the repository at this point in the history
Signed-off-by: Frost Ming <[email protected]>
  • Loading branch information
frostming committed Jan 25, 2024
1 parent e99545b commit a9a244d
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 30 deletions.
2 changes: 0 additions & 2 deletions .flake8

This file was deleted.

21 changes: 21 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-added-large-files

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.1.9'
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix, --show-fixes]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
hooks:
- id: mypy
20 changes: 18 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ pr = "onepm:pr"
pun = "onepm:pun"
pa = "onepm:pa"

[tool.pdm]
version = {source = "scm"}
[tool.pdm.version]
source = "scm"

[tool.pdm.dev-dependencies]
test = [
Expand All @@ -42,3 +42,19 @@ test = [
[build-system]
requires = ["pdm-backend"]
build-backend = "pdm.backend"

[tool.ruff]
target-version = "py310"

[tool.ruff.lint]
extend-select = [
"I", # isort
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"RUF", # ruff
"W", # pycodestyle
"YTT", # flake8-2020
]

[tool.ruff.isort]
known-first-party = ["onepm"]
13 changes: 8 additions & 5 deletions src/onepm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import os
import sys
from typing import Callable, Dict, List, NoReturn, Optional, Type
from typing import Callable, NoReturn

from onepm.base import PackageManager
from onepm.pdm import PDM
Expand All @@ -14,8 +16,9 @@
import tomli as tomllib


PACKAGE_MANAGERS: Dict[str, Type[PackageManager]] = {
p.name: p for p in [Pip, Pipenv, Poetry, PDM]
PACKAGE_MANAGERS: dict[str, type[PackageManager]] = {
p.name: p # type: ignore[type-abstract]
for p in [Pip, Pipenv, Poetry, PDM]
}


Expand All @@ -40,8 +43,8 @@ def determine_package_manager() -> str:
return "pip"


def make_shortcut(method_name: str) -> Callable[[Optional[List[str]]], NoReturn]:
def main(args: Optional[List[str]] = None) -> NoReturn:
def make_shortcut(method_name: str) -> Callable[[list[str] | None], NoReturn]:
def main(args: list[str] | None = None) -> NoReturn: # type: ignore[misc]
if args is None:
args = sys.argv[1:]
package_manager = PACKAGE_MANAGERS[determine_package_manager()]()
Expand Down
10 changes: 6 additions & 4 deletions src/onepm/base.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from __future__ import annotations

import abc
import os
import shutil
import subprocess
import sys
from typing import Iterable, List, NoReturn
from typing import Iterable, NoReturn


class PackageManager(metaclass=abc.ABCMeta):
name: str

@staticmethod
def has_unknown_args(args: Iterable[str], expecting_values: List[str]) -> bool:
def has_unknown_args(args: Iterable[str], expecting_values: list[str]) -> bool:
args_iter = iter(args)

for arg in args_iter:
Expand Down Expand Up @@ -42,14 +44,14 @@ def execute(self, *args: str) -> NoReturn:
self._execute_command(command_args)

@staticmethod
def _execute_command(args: List[str]) -> NoReturn:
def _execute_command(args: list[str]) -> NoReturn:
if sys.platform == "win32":
sys.exit(subprocess.run(args).returncode)
else:
os.execvp(args[0], args)

@abc.abstractmethod
def get_command(self) -> List[str]:
def get_command(self) -> list[str]:
pass

@abc.abstractmethod
Expand Down
6 changes: 4 additions & 2 deletions src/onepm/pdm.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from typing import List, NoReturn
from __future__ import annotations

from typing import NoReturn

from onepm.base import PackageManager


class PDM(PackageManager):
name = "pdm"

def get_command(self) -> List[str]:
def get_command(self) -> list[str]:
return [self.find_executable(self.name)]

def install(self, *args: str) -> NoReturn:
Expand Down
14 changes: 8 additions & 6 deletions src/onepm/pip.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import os
import sys
from typing import List, NoReturn, Optional
from typing import NoReturn

from onepm.base import PackageManager

Expand All @@ -22,23 +24,23 @@ def _ensure_virtualenv(self) -> str:
except ImportError:
raise Exception(
"To use pip, you must activate a virtualenv or create one at `.venv`."
)
) from None
venv.create(this_venv, with_pip=True)
return this_venv

def _find_requirements_txt(self) -> Optional[str]:
def _find_requirements_txt(self) -> str | None:
for filename in ["requirements.txt", "requirements.in"]:
if os.path.exists(filename):
return filename
return None

def _find_setup_py(self) -> Optional[str]:
def _find_setup_py(self) -> str | None:
for filename in ["setup.py", "pyproject.toml"]:
if os.path.exists(filename):
return filename
return None

def get_command(self) -> List[str]:
def get_command(self) -> list[str]:
venv = self._ensure_virtualenv()
if sys.platform == "win32":
bin_dir = "Scripts"
Expand All @@ -62,7 +64,7 @@ def install(self, *args: str) -> NoReturn:
"please specify packages to install."
)
else:
expanded_args = ["install"] + list(args)
expanded_args = ["install", *args]
self.execute(*expanded_args)

def update(self, *args: str) -> NoReturn:
Expand Down
6 changes: 4 additions & 2 deletions src/onepm/pipenv.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from typing import List, NoReturn
from __future__ import annotations

from typing import NoReturn

from onepm.base import PackageManager


class Pipenv(PackageManager):
name = "pipenv"

def get_command(self) -> List[str]:
def get_command(self) -> list[str]:
return [self.find_executable(self.name)]

def install(self, *args: str) -> NoReturn:
Expand Down
6 changes: 4 additions & 2 deletions src/onepm/poetry.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from typing import List, NoReturn
from __future__ import annotations

from typing import NoReturn

from onepm.base import PackageManager


class Poetry(PackageManager):
name = "poetry"

def get_command(self) -> List[str]:
def get_command(self) -> list[str]:
return [self.find_executable(self.name)]

def install(self, *args: str) -> NoReturn:
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from unittest import mock

import pytest


Expand Down
5 changes: 3 additions & 2 deletions tests/test_pdm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from onepm import pi, pa, pu, pun, pr
import pytest

from onepm import pa, pi, pr, pu, pun

pytestmark = pytest.mark.usefixtures("pdm")


Expand All @@ -17,7 +18,7 @@
)
def test_pdm_pi_dispatch(project, execute_args, args, expected_command):
pi(args)
assert execute_args[1:] == [expected_command] + args
assert execute_args[1:] == [expected_command, *args]


def test_pdm_pr(project, execute_args):
Expand Down
1 change: 1 addition & 0 deletions tests/test_pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys

import pytest

from onepm import pa, pi, pr, pu, pun
from onepm.pip import Pip

Expand Down
4 changes: 2 additions & 2 deletions tests/test_pipenv.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import pytest
from onepm import pi, pa, pu, pun, pr

from onepm import pa, pi, pr, pu, pun

pytestmark = pytest.mark.usefixtures("pipenv")


@pytest.mark.parametrize("args", [[], ["--dev"], ["--keep-outdated", "requests"]])
def test_pipenv_pi(project, execute_args, args):
pi(args)
assert execute_args[1:] == ["install"] + args
assert execute_args[1:] == ["install", *args]


def test_pipenv_pr(project, execute_args):
Expand Down
3 changes: 2 additions & 1 deletion tests/test_poetry.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest

from onepm import pa, pi, pr, pu, pun

pytestmark = pytest.mark.usefixtures("poetry")
Expand All @@ -16,7 +17,7 @@
)
def test_poetry_pi_dispatch(project, execute_args, args, expected_command):
pi(args)
assert execute_args[1:] == [expected_command] + args
assert execute_args[1:] == [expected_command, *args]


def test_poetry_pu(project, execute_args):
Expand Down
1 change: 1 addition & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest

from onepm import determine_package_manager


Expand Down

0 comments on commit a9a244d

Please sign in to comment.