Skip to content

Commit

Permalink
Add ruff to db and minor houskeeping stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
floatingpurr committed Oct 7, 2024
1 parent 41b6bf2 commit 5dc7fdd
Show file tree
Hide file tree
Showing 13 changed files with 294 additions and 578 deletions.
3 changes: 0 additions & 3 deletions .flake8

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
python-version: ["3.9", "3.10", "3.11", "3.12"]
os: [ubuntu-latest, macOS-latest, windows-latest]

steps:
Expand Down
22 changes: 6 additions & 16 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,13 @@ repos:
- id: trailing-whitespace
- id: check-toml

# black - formatting
- repo: https://github.com/psf/black
rev: 22.8.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.9
hooks:
- id: black

# isort - sorting imports
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort

# flake8 - linting
- repo: https://github.com/pycqa/flake8
rev: 5.0.4
hooks:
- id: flake8
- id: ruff
args:
- --fix
- id: ruff-format

# mypy - lint-like type checking
- repo: https://github.com/pre-commit/mirrors-mypy
Expand Down
15 changes: 9 additions & 6 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
{
"python.formatting.provider": "black",
"editor.formatOnSave": true,
"python.linting.enabled": true,
"python.linting.flake8Enabled": true,
"python.linting.mypyEnabled": true,
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.codeActionsOnSave": {
"source.organizeImports": true
"source.organizeImports": "always"
},
},
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
"python.testing.pytestEnabled": true,
"ruff.lint.args": [
"--config=pyproject.toml"
],
"ruff.lint.run": "onSave"


}
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![codecov](https://codecov.io/gh/floatingpurr/sync_with_poetry/branch/main/graph/badge.svg?token=RNDNWATE25)](https://codecov.io/gh/floatingpurr/sync_with_poetry)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/floatingpurr/sync_with_poetry/main.svg)](https://results.pre-commit.ci/latest/github/floatingpurr/sync_with_poetry/main)
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)

A .pre-commit hook for keeping in sync the repos `rev` in
`.pre-commit-config.yaml` with the packages version locked into `poetry.lock`.
Expand All @@ -19,10 +19,10 @@ framework.
When it comes to Python dependency management,
[Poetry](https://python-poetry.org/) is one of the modern solutions to handle
project dependencies. [Sometimes](https://stackoverflow.com/q/70127649/4820341),
you might want to install dev dependencies locally (e.g., `black`, `flake8`,
`isort`, `mypy`, ...) to make your IDE (e.g., VS Code) play nicely with dev
packages. This approach usually turns on a live feedback as you code (e.g.,
suggestions, linting, formatting, errors highlighting). Poetry does not
you might want to install dev dependencies locally (e.g., `ruff`, `black`,
`flake8`, `isort`, `mypy`, ...) to make your IDE (e.g., VS Code) play nicely
with dev packages. This approach usually turns on a live feedback as you code
(e.g., suggestions, linting, formatting, errors highlighting). Poetry does not
differentiates anymore between dev and production packages inside `poetry.lock`.
Now, this info is managed by dependency groups in `pyproject.toml` (see
[#26](https://github.com/floatingpurr/sync_with_poetry/issues/26) for more).
Expand Down
741 changes: 248 additions & 493 deletions poetry.lock

Large diffs are not rendered by default.

38 changes: 10 additions & 28 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ name = "sync_with_poetry"
version = "1.0.0"

[tool.poetry.dependencies]
python = "^3.6.1"
python = ">=3.9"

PyYAML = "^6.0"
PyYAML = "^6.0.1"
tomlkit = "^0.7.2"

[tool.poetry.group.dev.dependencies]
black = {version = "^22.8.0", python = "^3.6.2"}
coverage = [
{extras = [
"toml",
Expand All @@ -20,11 +19,11 @@ coverage = [
"toml",
], version = ">=6.5", python = ">=3.7"},
]
flake8 = "^5.0.4"
mypy = "^0.971"
pre-commit = {version = "^2.15.0", python = "^3.6.1"}
pre-commit = "^3.8.0"
pytest = "^6.2"
pytest-cov = "^4.0.0"
ruff = "^0.6.8"
types-PyYAML = "^6.0.1"

[tool.poetry.scripts]
Expand All @@ -34,25 +33,6 @@ swp = "sync_with_poetry.swp:main"
addopts = "-svv"
testpaths = ["tests"]

[tool.black]
exclude = '''
/(
\.eggs
| \.git
| \.hg
| \.mypy_cache
| \.pytest_cache
| \.venv
| _build
| buck-out
| build
| dist
| tests/.*/setup.py
)/
'''
include = '\.pyi?$'
line-length = 88

[tool.mypy]
disallow_incomplete_defs = true
disallow_untyped_defs = true
Expand All @@ -61,10 +41,12 @@ files = ["**/*.py"]
namespace_packages = true
show_error_codes = true

[tool.isort]
profile = "black"
src_paths = ["sync_with_poetry", "tests"]

[build-system]
build-backend = "poetry.core.masonry.api"
requires = ["poetry-core"]

[tool.ruff]
line-length = 100

[tool.ruff.format]
docstring-code-format = true
4 changes: 4 additions & 0 deletions sync_with_poetry/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@
"repo": "https://github.com/asottile/pyupgrade",
"rev": "v${rev}",
},
"ruff": {
"repo": "https://github.com/astral-sh/ruff-pre-commit",
"rev": "v${rev}",
},
}
8 changes: 2 additions & 6 deletions sync_with_poetry/swp.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ def __init__(
if dependency_mapping:
name = package["name"]
repo = dependency_mapping["repo"]
rev = Template(dependency_mapping["rev"]).substitute(
rev=package["version"]
)
rev = Template(dependency_mapping["rev"]).substitute(rev=package["version"])
self._poetry_lock[repo] = {"name": name, "rev": rev}

def get_by_repo(self, repo: str) -> Optional[Dict[str, str]]:
Expand Down Expand Up @@ -167,9 +165,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
mapping = json.load(f)
retv = 0
for filename in args.filenames:
retv |= sync_repos(
filename, args.skip, args.config, mapping, frozen=args.frozen
)
retv |= sync_repos(filename, args.skip, args.config, mapping, frozen=args.frozen)
return retv


Expand Down
8 changes: 2 additions & 6 deletions tests/test_frozen.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@


def config_content(rev_line: str) -> str:
return (
"repos:\n" " - repo: test\n" + rev_line + " hooks:\n" " - id: test\n"
)
return "repos:\n" " - repo: test\n" + rev_line + " hooks:\n" " - id: test\n"


LOCK_CONTENT = (
Expand All @@ -72,9 +70,7 @@ def config_content(rev_line: str) -> str:
}


def run_and_check(
tmpdir: LocalPath, rev_line: str, expected: str, frozen: bool
) -> None:
def run_and_check(tmpdir: LocalPath, rev_line: str, expected: str, frozen: bool) -> None:
lock_file = tmpdir.join("poetry.lock")
lock_file.write(LOCK_CONTENT)
config_file = tmpdir.join(".pre-commit-yaml")
Expand Down
4 changes: 2 additions & 2 deletions tests/test_lock_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_poetry_items_creation() -> None:
content = tomlkit.loads(LOCK_CONTENT)
assert isinstance(content["package"], tomlkit.items.AoT)
p = PoetryItems(content["package"])
assert type(p._poetry_lock) == dict
assert type(p._poetry_lock) is dict


def test_poetry_items_metadata() -> None:
Expand All @@ -18,6 +18,6 @@ def test_poetry_items_metadata() -> None:
assert isinstance(content["package"], tomlkit.items.AoT)
p = PoetryItems(content["package"])
item = p.get_by_repo("https://github.com/pre-commit/mirrors-mypy")
assert type(item) == dict
assert type(item) is dict
assert item["name"] == "mypy"
assert item["rev"] == "v0.910"
2 changes: 1 addition & 1 deletion tests/test_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
def test_built_in_dependency_mapping() -> None:
"""Test the structure of the DEPENDENCY_MAPPING"""
for item in DEPENDENCY_MAPPING:
assert type(item) == str
assert type(item) is str
# chek url
assert re.match(URL_REGEX, DEPENDENCY_MAPPING[item]["repo"])
assert "${rev}" in DEPENDENCY_MAPPING[item]["rev"]
15 changes: 4 additions & 11 deletions tests/test_sync_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,24 @@
from py._path.local import LocalPath

from sync_with_poetry import swp
from tests.helpers import (
CONFIG_CONTENT,
CUSTOM_DEPENDENCY_MAPPING,
LOCK_CONTENT,
get_repo_version,
)
from tests.helpers import CONFIG_CONTENT, CUSTOM_DEPENDENCY_MAPPING, LOCK_CONTENT, get_repo_version

LEN_CONFIG_CONTENT = CONFIG_CONTENT.count("\n")


# fmt: off
@pytest.mark.parametrize(
"test_input,expected",
[
# sync all dependencies
(
{"skip": []},
# fmt: off
{
"https://github.com/pre-commit/mirrors-mypy": "v0.910", # bumped
"https://github.com/pycqa/flake8": "4.0.1", # bumped
"https://github.com/psf/black": "21.11b1", # bumped
"https://github.com/pycqa/isort": "5.10.1", # not managed by Poetry
},
# fmt: on
),
# sync dependencies, skipping `flake8` and black
(
Expand All @@ -37,19 +31,18 @@
"https://github.com/psf/black": "20.8b1", # not bumped skipped
"https://github.com/pycqa/isort": "5.10.1", # not managed by Poetry
},
# fmt: on
),
# sync all dependencies from custom mapping
(
{"skip": [], "db": CUSTOM_DEPENDENCY_MAPPING},
# fmt: off
{
"https://example.org/fakepackages/foobarbaz": "1.0.1", # bumped (main)
},
# fmt: on
),
],
)
# fmt: on
def test_sync_repos(tmpdir: LocalPath, test_input: dict, expected: dict) -> None:
"""Test repo synchronization against different inputs and configurations"""
lock_file = tmpdir.join("poetry.lock")
Expand Down

0 comments on commit 5dc7fdd

Please sign in to comment.