Skip to content

Commit 6a7ad1c

Browse files
Merge pull request #936 from RonnyPfannschmidt/try-ruff
introduce ruff as a linter
2 parents 056584b + 57bd901 commit 6a7ad1c

22 files changed

+119
-73
lines changed

.pre-commit-config.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ repos:
2727
hooks:
2828
- id: pyupgrade
2929
args: [--py38-plus]
30+
- repo: https://github.com/astral-sh/ruff-pre-commit
31+
rev: v0.0.291
32+
hooks:
33+
- id: ruff
34+
args: [--fix, --exit-non-zero-on-fix]
3035
- repo: https://github.com/tox-dev/pyproject-fmt
3136
rev: "1.1.0"
3237
hooks:

MANIFEST.in

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
exclude *.nix
22
exclude .pre-commit-config.yaml
3+
exclude changelog.d/*
34
exclude .git_archival.txt
5+
exclude .readthedocs.yaml
46
include *.py
57
include testing/*.py
68
include tox.ini
@@ -10,5 +12,14 @@ include *.toml
1012
include mypy.ini
1113
include testing/Dockerfile.*
1214
include src/setuptools_scm/.git_archival.txt
15+
include README.md
16+
include CHANGELOG.md
17+
18+
1319
recursive-include testing *.bash
1420
prune nextgen
21+
22+
recursive-include docs *.md
23+
include docs/examples/version_scheme_code/*.py
24+
include docs/examples/version_scheme_code/*.toml
25+
include mkdocs.yml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
### Changed
3+
4+
- introduce ruff as a linter

hatch.toml

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
[envs.test]
2+
extras = ["test", "dev"]
3+
4+
[envs.test.scripts]
5+
all = "pytest {args}"
6+
7+
[[env.test.matrix]]
8+
python = ["3.8", "3.9", "3.10", "3.11"]
9+
10+
111
[envs.docs]
212
python = "3.11"
313
extras = ["docs"]

pyproject.toml

+21-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ dependencies = [
4242
"packaging>=20",
4343
"setuptools",
4444
'tomli>=1; python_version < "3.11"',
45-
'typing-extensions; python_version < "3.11"',
45+
"typing-extensions",
4646
]
4747
[project.optional-dependencies]
4848
docs = [
@@ -108,5 +108,25 @@ version = { attr = "_own_version_helper.version"}
108108

109109
[tool.setuptools_scm]
110110

111+
[tool.ruff]
112+
select = ["E", "F", "B", "U", "YTT", "C", "DTZ", "PYI", "PT"]
113+
ignore = ["B028"]
114+
115+
[tool.pytest.ini_options]
116+
testpaths = ["testing"]
117+
filterwarnings = [
118+
"error",
119+
"ignore:.*tool\\.setuptools_scm.*",
120+
"ignore:.*git archive did not support describe output.*:UserWarning",
121+
]
122+
log_level = "debug"
123+
log_cli_level = "info"
124+
# disable unraisable until investigated
125+
addopts = ["-p", "no:unraisableexception"]
126+
markers = [
127+
"issue(id): reference to github issue",
128+
"skip_commit: allows to skip committing in the helpers",
129+
]
130+
111131
[tool.scriv]
112132
format = "md"

src/setuptools_scm/_config.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ def _check_absolute_root(root: _t.PathT, relative_to: _t.PathT | None) -> str:
6565
and not os.path.commonpath([root, relative_to]) == root
6666
):
6767
warnings.warn(
68-
"absolute root path '%s' overrides relative_to '%s'"
69-
% (root, relative_to)
68+
f"absolute root path '{root}' overrides relative_to '{relative_to}'"
7069
)
7170
if os.path.isdir(relative_to):
7271
warnings.warn(

src/setuptools_scm/_file_finders/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def scm_find_files(
4444
# dirpath with symlinks resolved
4545
realdirpath = os.path.normcase(os.path.realpath(dirpath))
4646

47-
def _link_not_in_scm(n: str) -> bool:
47+
def _link_not_in_scm(n: str, realdirpath: str = realdirpath) -> bool:
4848
fn = os.path.join(realdirpath, os.path.normcase(n))
4949
return os.path.islink(fn) and fn not in scm_files
5050

src/setuptools_scm/_file_finders/hg.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def _hg_ls_files_and_dirs(toplevel: str) -> tuple[set[str], set[str]]:
3434
hg_dirs = {toplevel}
3535
res = _run(["hg", "files"], cwd=toplevel)
3636
if res.returncode:
37-
(), ()
37+
return set(), set()
3838
for name in res.stdout.splitlines():
3939
name = os.path.normcase(name).replace("/", os.path.sep)
4040
fullname = os.path.join(toplevel, name)

src/setuptools_scm/_version_cls.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ def _validate_version_cls(
8484
try:
8585
return cast(Type[_VersionT], import_name(version_cls))
8686
except: # noqa
87-
raise ValueError(f"Unable to import version_cls='{version_cls}'")
87+
raise ValueError(
88+
f"Unable to import version_cls='{version_cls}'"
89+
) from None
8890
else:
8991
return version_cls

src/setuptools_scm/git.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import warnings
99
from datetime import date
1010
from datetime import datetime
11+
from datetime import timezone
1112
from os.path import samefile
1213
from pathlib import Path
1314
from typing import Callable
@@ -268,7 +269,7 @@ def _git_parse_inner(
268269
tag=tag, distance=distance, dirty=dirty, node=node, config=config
269270
)
270271
branch = wd.get_branch()
271-
node_date = wd.get_head_date() or date.today()
272+
node_date = wd.get_head_date() or datetime.now(timezone.utc).date()
272273
return dataclasses.replace(version, branch=branch, node_date=node_date)
273274

274275

src/setuptools_scm/version.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def guess_next_simple_semver(
240240
try:
241241
parts = [int(i) for i in str(version.tag).split(".")[:retain]]
242242
except ValueError:
243-
raise ValueError(f"{version} can't be parsed as numeric version")
243+
raise ValueError(f"{version} can't be parsed as numeric version") from None
244244
while len(parts) < retain:
245245
parts.append(0)
246246
if increment:
@@ -355,17 +355,20 @@ def guess_next_date_ver(
355355
if match is None:
356356
tag_date = today
357357
else:
358-
tag_date = datetime.strptime(match.group("date"), date_fmt).date()
358+
tag_date = (
359+
datetime.strptime(match.group("date"), date_fmt)
360+
.replace(tzinfo=timezone.utc)
361+
.date()
362+
)
359363
if tag_date == head_date:
360364
patch = "0" if match is None else (match.group("patch") or "0")
361365
patch = int(patch) + 1
362366
else:
363367
if tag_date > head_date and match is not None:
364368
# warn on future times
365369
warnings.warn(
366-
"your previous tag ({}) is ahead your node date ({})".format(
367-
tag_date, head_date
368-
)
370+
f"your previous tag ({tag_date})"
371+
f" is ahead your node date ({head_date})"
369372
)
370373
patch = 0
371374
next_version = "{node_date:{date_fmt}}.{patch}".format(

testing/conftest.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from typing import Iterator
99

1010
import pytest
11+
from typing_extensions import Self
1112

1213
from .wd_wrapper import WorkDir
1314
from setuptools_scm._run_cmd import run
@@ -46,7 +47,7 @@ class DebugMode(contextlib.AbstractContextManager): # type: ignore[type-arg]
4647
def __init__(self) -> None:
4748
self.__stack = contextlib.ExitStack()
4849

49-
def __enter__(self) -> DebugMode:
50+
def __enter__(self) -> Self:
5051
self.enable()
5152
return self
5253

@@ -71,14 +72,14 @@ def debug_mode() -> Iterator[DebugMode]:
7172
yield debug_mode
7273

7374

74-
@pytest.fixture
75+
@pytest.fixture()
7576
def wd(tmp_path: Path) -> WorkDir:
7677
target_wd = tmp_path.resolve() / "wd"
7778
target_wd.mkdir()
7879
return WorkDir(target_wd)
7980

8081

81-
@pytest.fixture
82+
@pytest.fixture()
8283
def repositories_hg_git(tmp_path: Path) -> tuple[WorkDir, WorkDir]:
8384
tmp_path = tmp_path.resolve()
8485
path_git = tmp_path / "repo_git"

testing/test_basic_api.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,18 @@ def test_custom_version_cls() -> None:
226226
"""Test that `normalize` and `version_cls` work as expected"""
227227

228228
class MyVersion:
229-
def __init__(self, tag_str: str):
229+
def __init__(self, tag_str: str) -> None:
230230
self.version = tag_str
231231

232232
def __repr__(self) -> str:
233233
return f"hello,{self.version}"
234234

235235
# you can not use normalize=False and version_cls at the same time
236-
with pytest.raises(ValueError):
236+
with pytest.raises(
237+
ValueError,
238+
match="Providing a custom `version_cls`"
239+
" is not permitted when `normalize=False`",
240+
):
237241
setuptools_scm.get_version(normalize=False, version_cls=MyVersion)
238242

239243
# TODO unfortunately with PRETEND_KEY the preformatted flag becomes True

testing/test_config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
@pytest.mark.parametrize(
13-
"tag, expected_version",
13+
("tag", "expected_version"),
1414
[
1515
("apache-arrow-0.9.0", "0.9.0"),
1616
("arrow-0.9.0", "0.9.0"),

testing/test_file_finder.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import os
44
import sys
5-
from typing import Generator
65
from typing import Iterable
76

87
import pytest
@@ -14,8 +13,8 @@
1413
@pytest.fixture(params=["git", "hg"])
1514
def inwd(
1615
request: pytest.FixtureRequest, wd: WorkDir, monkeypatch: pytest.MonkeyPatch
17-
) -> Generator[WorkDir, None, None]:
18-
param: str = getattr(request, "param") # todo: fix
16+
) -> WorkDir:
17+
param: str = request.param # type: ignore
1918
if param == "git":
2019
try:
2120
wd("git init")
@@ -42,7 +41,7 @@ def inwd(
4241
if request.node.get_closest_marker("skip_commit") is None:
4342
wd.add_and_commit()
4443
monkeypatch.chdir(wd.cwd)
45-
yield wd
44+
return wd
4645

4746

4847
def _sep(paths: Iterable[str]) -> set[str]:
@@ -198,7 +197,7 @@ def test_symlink_not_in_scm_while_target_is(inwd: WorkDir) -> None:
198197

199198

200199
@pytest.mark.issue(587)
201-
@pytest.mark.skip_commit
200+
@pytest.mark.skip_commit()
202201
def test_not_commited(inwd: WorkDir) -> None:
203202
assert find_files() == []
204203

@@ -212,7 +211,7 @@ def test_unexpanded_git_archival(wd: WorkDir, monkeypatch: pytest.MonkeyPatch) -
212211
assert find_files() == []
213212

214213

215-
@pytest.mark.parametrize("archive_file", (".git_archival.txt", ".hg_archival.txt"))
214+
@pytest.mark.parametrize("archive_file", [".git_archival.txt", ".hg_archival.txt"])
216215
def test_archive(
217216
wd: WorkDir, monkeypatch: pytest.MonkeyPatch, archive_file: str
218217
) -> None:

testing/test_functions.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121

2222
@pytest.mark.parametrize(
23-
"tag, expected",
23+
("tag", "expected"),
2424
[
2525
("1.1", "1.2"),
2626
("1.2.dev", "1.2"),
@@ -48,7 +48,7 @@ def test_next_tag(tag: str, expected: str) -> None:
4848

4949

5050
@pytest.mark.parametrize(
51-
"version,version_scheme, local_scheme,expected",
51+
("version", "version_scheme", "local_scheme", "expected"),
5252
[
5353
("exact", "guess-next-dev", "node-and-date", "1.1"),
5454
("dirty", "guess-next-dev", "node-and-date", "1.2.dev0+d20090213"),
@@ -172,7 +172,7 @@ def test_has_command_logs_stderr(caplog: pytest.LogCaptureFixture) -> None:
172172

173173

174174
@pytest.mark.parametrize(
175-
"tag, expected_version",
175+
("tag", "expected_version"),
176176
[
177177
("1.1", "1.1"),
178178
("release-1.1", "1.1"),

testing/test_git.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def wd(wd: WorkDir, monkeypatch: pytest.MonkeyPatch, debug_mode: DebugMode) -> W
5050

5151

5252
@pytest.mark.parametrize(
53-
"given, tag, number, node, dirty",
53+
("given", "tag", "number", "node", "dirty"),
5454
[
5555
("3.3.1-rc26-0-g9df187b", "3.3.1-rc26", 0, "g9df187b", False),
5656
("17.33.0-rc-17-g38c3047c0", "17.33.0-rc", 17, "g38c3047c0", False),
@@ -158,7 +158,8 @@ def test_version_from_git(wd: WorkDir) -> None:
158158
assert wd.get_version() == "0.1.dev0+d20090213"
159159

160160
parsed = git.parse(str(wd.cwd), Configuration(), git.DEFAULT_DESCRIBE)
161-
assert parsed is not None and parsed.branch in ("master", "main")
161+
assert parsed is not None
162+
assert parsed.branch in ("master", "main")
162163

163164
wd.commit_testfile()
164165
assert wd.get_version().startswith("0.1.dev1+g")
@@ -285,7 +286,8 @@ def test_git_dirty_notag(
285286
tag = datetime.now(timezone.utc).date().strftime(".d%Y%m%d")
286287
else:
287288
tag = ".d20090213"
288-
assert version.startswith("0.1.dev1+g") and version.endswith(tag)
289+
assert version.startswith("0.1.dev1+g")
290+
assert version.endswith(tag)
289291

290292

291293
@pytest.mark.issue(193)
@@ -300,7 +302,7 @@ def test_git_worktree_support(wd: WorkDir, tmp_path: Path) -> None:
300302
assert str(worktree) in res.stdout
301303

302304

303-
@pytest.fixture
305+
@pytest.fixture()
304306
def shallow_wd(wd: WorkDir, tmp_path: Path) -> Path:
305307
wd.commit_testfile()
306308
wd.commit_testfile()
@@ -461,7 +463,7 @@ def test_gitdir(monkeypatch: pytest.MonkeyPatch, wd: WorkDir) -> None:
461463

462464
def test_git_getdate(wd: WorkDir) -> None:
463465
# TODO: case coverage for git wd parse
464-
today = date.today()
466+
today = datetime.now(timezone.utc).date()
465467

466468
def parse_date() -> date:
467469
parsed = git.parse(os.fspath(wd.cwd), Configuration())
@@ -492,7 +494,7 @@ def test_git_getdate_badgit(
492494
assert git_wd.get_head_date() is None
493495

494496

495-
@pytest.fixture
497+
@pytest.fixture()
496498
def signed_commit_wd(monkeypatch: pytest.MonkeyPatch, wd: WorkDir) -> WorkDir:
497499
if not has_command("gpg", args=["--version"], warn=False):
498500
pytest.skip("gpg executable not found")
@@ -519,14 +521,14 @@ def signed_commit_wd(monkeypatch: pytest.MonkeyPatch, wd: WorkDir) -> WorkDir:
519521

520522
@pytest.mark.issue("https://github.com/pypa/setuptools_scm/issues/548")
521523
def test_git_getdate_signed_commit(signed_commit_wd: WorkDir) -> None:
522-
today = date.today()
524+
today = datetime.now(timezone.utc).date()
523525
signed_commit_wd.commit_testfile(signed=True)
524526
git_wd = git.GitWorkdir(signed_commit_wd.cwd)
525527
assert git_wd.get_head_date() == today
526528

527529

528530
@pytest.mark.parametrize(
529-
"expected, from_data",
531+
("expected", "from_data"),
530532
[
531533
(
532534
"1.0",

testing/test_integration.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
c = Configuration()
1919

2020

21-
@pytest.fixture
21+
@pytest.fixture()
2222
def wd(wd: WorkDir) -> WorkDir:
2323
wd("git init")
2424
wd("git config user.email [email protected]")

0 commit comments

Comments
 (0)