Skip to content

Commit cfd0171

Browse files
committed
Switch linting to Ruff
1 parent 998d710 commit cfd0171

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+843
-1332
lines changed

.pre-commit-config.yaml

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,10 @@ repos:
1717
- id: clang-format
1818
exclude: dev-tools|examples
1919
verbose: true
20-
- repo: https://github.com/asottile/reorder_python_imports
21-
rev: v3.12.0
20+
- repo: https://github.com/astral-sh/ruff-pre-commit
21+
rev: v0.4.5
2222
hooks:
23-
- id: reorder-python-imports
24-
args: [--application-directories=python,
25-
--unclassifiable-application-module=_tskit]
26-
- repo: https://github.com/asottile/pyupgrade
27-
rev: v3.15.2
28-
hooks:
29-
- id: pyupgrade
30-
args: [--py3-plus, --py38-plus]
31-
- repo: https://github.com/psf/black
32-
rev: 24.4.2
33-
hooks:
34-
- id: black
35-
language_version: python3
36-
- repo: https://github.com/pycqa/flake8
37-
rev: 7.0.0
38-
hooks:
39-
- id: flake8
40-
args: [--config=python/.flake8]
41-
additional_dependencies: ["flake8-bugbear==23.9.16", "flake8-builtins==2.1.0"]
42-
- repo: https://github.com/asottile/blacken-docs
43-
rev: 1.16.0
44-
hooks:
45-
- id: blacken-docs
46-
args: [--skip-errors]
47-
additional_dependencies: [black==22.3.0]
48-
language_version: python3
23+
- id: ruff
24+
args: [ "--fix", "--config", "python/ruff.toml" ]
25+
- id: ruff-format
26+
args: [ "--config", "python/ruff.toml" ]

python/.flake8

Lines changed: 0 additions & 7 deletions
This file was deleted.

python/benchmark/run-for-all-releases.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import json
22
import subprocess
3+
from distutils.version import StrictVersion
34
from urllib.request import urlopen
45

56
import tqdm
6-
from distutils.version import StrictVersion
77

88

99
def versions(package_name):

python/benchmark/run.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414

1515
tskit_dir = Path(__file__).parent.parent
1616
sys.path.append(str(tskit_dir))
17-
import tskit # noqa: E402
1817
import msprime # noqa: E402
1918

19+
import tskit # noqa: E402
20+
2021
with open("config.yaml") as f:
2122
config = yaml.load(f, Loader=yaml.FullLoader)
2223

python/lwt_interface/dict_encoding_testlib.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# MIT License
22
#
3-
# Copyright (c) 2018-2022 Tskit Developers
3+
# Copyright (c) 2018-2024 Tskit Developers
44
#
55
# Permission is hereby granted, free of charge, to any person obtaining a copy
66
# of this software and associated documentation files (the "Software"), to deal
@@ -26,6 +26,7 @@
2626
compiled module exporting the LightweightTableCollection class.
2727
See the test_example_c_module file for an example.
2828
"""
29+
2930
import copy
3031

3132
import kastore
@@ -98,7 +99,7 @@ def full_ts():
9899

99100
# The ts above is used for the whole test session, but our tests need fresh tables to
100101
# modify
101-
@pytest.fixture
102+
@pytest.fixture()
102103
def tables(full_ts):
103104
return full_ts.dump_tables()
104105

@@ -183,9 +184,7 @@ def test_example(self, tables):
183184
{
184185
"codec": "struct",
185186
"type": "object",
186-
"properties": {
187-
table: {"type": "string", "binaryFormat": "50p"}
188-
},
187+
"properties": {table: {"type": "string", "binaryFormat": "50p"}},
189188
}
190189
)
191190

@@ -459,9 +458,7 @@ def verify_optional_column(self, tables, table_len, table_name, col_name):
459458
out[table_name][col_name], np.zeros(table_len, dtype=np.int32) - 1
460459
)
461460

462-
def verify_offset_pair(
463-
self, tables, table_len, table_name, col_name, required=False
464-
):
461+
def verify_offset_pair(self, tables, table_len, table_name, col_name, required=False):
465462
offset_col = col_name + "_offset"
466463

467464
if not required:
@@ -544,9 +541,7 @@ def test_individuals(self, tables):
544541
self.verify_offset_pair(
545542
tables, len(tables.individuals), "individuals", "location"
546543
)
547-
self.verify_offset_pair(
548-
tables, len(tables.individuals), "individuals", "parents"
549-
)
544+
self.verify_offset_pair(tables, len(tables.individuals), "individuals", "parents")
550545
self.verify_offset_pair(
551546
tables, len(tables.individuals), "individuals", "metadata"
552547
)
@@ -578,9 +573,7 @@ def test_migrations(self, tables):
578573
self.verify_required_columns(
579574
tables, "migrations", ["left", "right", "node", "source", "dest", "time"]
580575
)
581-
self.verify_offset_pair(
582-
tables, len(tables.migrations), "migrations", "metadata"
583-
)
576+
self.verify_offset_pair(tables, len(tables.migrations), "migrations", "metadata")
584577
self.verify_optional_column(tables, len(tables.nodes), "nodes", "individual")
585578
self.verify_metadata_schema(tables, "migrations")
586579

@@ -674,9 +667,7 @@ def get_refseq(d):
674667
assert get_refseq(d).is_null()
675668

676669
# All empty strings is the same thing
677-
d["reference_sequence"] = dict(
678-
data="", url="", metadata_schema="", metadata=b""
679-
)
670+
d["reference_sequence"] = dict(data="", url="", metadata_schema="", metadata=b"")
680671
assert get_refseq(d).is_null()
681672

682673
del refseq_dict["metadata_schema"] # handled above

python/lwt_interface/setup.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import os.path
22
import platform
33

4-
from setuptools import Extension
5-
from setuptools import setup
4+
from setuptools import Extension, setup
65
from setuptools.command.build_ext import build_ext
76

8-
97
IS_WINDOWS = platform.system() == "Windows"
108

119

1210
# Obscure magic required to allow numpy be used as a 'setup_requires'.
1311
# Based on https://stackoverflow.com/questions/19919905
14-
class local_build_ext(build_ext):
12+
class local_build_ext(build_ext): # noqa: N801
1513
def finalize_options(self):
1614
build_ext.finalize_options(self)
1715
import builtins

python/ruff.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
line-length = 90
2+
3+
[lint]
4+
select = ["E", "F", "B", "W", "I", "N", "UP", "A", "RUF", "PT", "NPY"]
5+
# N803,806,802 Allow capital varnames
6+
# E741 Allow "l" as var name
7+
# PT011 allow pytest raises without match
8+
ignore = ["N803", "N806", "N802", "E741", "PT011", "PT009"]
9+
10+
[lint.isort]
11+
section-order = ["future", "standard-library", "third-party", "first-party", "local-folder"]
12+
known-first-party = ["tskit", "_tskit"]

python/setup.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import os.path
22
import platform
33

4-
from setuptools import Extension
5-
from setuptools import setup
4+
from setuptools import Extension, setup
65
from setuptools.command.build_ext import build_ext
76

8-
97
IS_WINDOWS = platform.system() == "Windows"
108

119

1210
# Obscure magic required to allow numpy be used as a 'setup_requires'.
1311
# Based on https://stackoverflow.com/questions/19919905
14-
class local_build_ext(build_ext):
12+
class local_build_ext(build_ext): # noqa: N801
1513
def finalize_options(self):
1614
build_ext.finalize_options(self)
1715
import builtins

python/tests/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# MIT License
22
#
3-
# Copyright (c) 2018-2023 Tskit Developers
3+
# Copyright (c) 2018-2024 Tskit Developers
44
#
55
# Permission is hereby granted, free of charge, to any person obtaining a copy
66
# of this software and associated documentation files (the "Software"), to deal
@@ -22,6 +22,7 @@
2222
import base64
2323

2424
import tskit
25+
2526
from . import tsutil
2627
from .simplify import * # NOQA
2728

@@ -195,9 +196,7 @@ def trees(self):
195196
pt.left = left
196197
pt.right = right
197198
# Add in all the sites
198-
pt.site_list = [
199-
site for site in self._sites if left <= site.position < right
200-
]
199+
pt.site_list = [site for site in self._sites if left <= site.position < right]
201200
yield pt
202201
pt.index += 1
203202
pt.index = -1

python/tests/conftest.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# MIT License
22
#
3-
# Copyright (c) 2018-2022 Tskit Developers
3+
# Copyright (c) 2018-2024 Tskit Developers
44
#
55
# Permission is hereby granted, free of charge, to any person obtaining a copy
66
# of this software and associated documentation files (the "Software"), to deal
@@ -38,9 +38,9 @@ def test_something(self, ts_fixture):
3838
Note that fixtures have a "scope" for example `ts_fixture` below is only created once
3939
per test session and re-used for subsequent tests.
4040
"""
41+
4142
import msprime
4243
import pytest
43-
from pytest import fixture
4444

4545
from . import tsutil
4646

@@ -81,45 +81,45 @@ def pytest_collection_modifyitems(config, items):
8181
item.add_marker(skip_slow)
8282

8383

84-
@fixture
84+
@pytest.fixture()
8585
def overwrite_viz(request):
8686
return request.config.getoption("--overwrite-expected-visualizations")
8787

8888

89-
@fixture
89+
@pytest.fixture()
9090
def draw_plotbox(request):
9191
return request.config.getoption("--draw-svg-debug-box")
9292

9393

94-
@fixture(scope="session")
94+
@pytest.fixture(scope="session")
9595
def simple_degree1_ts_fixture():
9696
return msprime.simulate(10, random_seed=42)
9797

9898

99-
@fixture(scope="session")
99+
@pytest.fixture(scope="session")
100100
def simple_degree2_ts_fixture():
101101
ts = msprime.simulate(10, recombination_rate=0.2, random_seed=42)
102102
assert ts.num_trees == 2
103103
return ts
104104

105105

106-
@fixture(scope="session")
106+
@pytest.fixture(scope="session")
107107
def ts_fixture():
108108
"""
109109
A tree sequence with data in all fields
110110
"""
111111
return tsutil.all_fields_ts()
112112

113113

114-
@fixture(scope="session")
114+
@pytest.fixture(scope="session")
115115
def ts_fixture_for_simplify():
116116
"""
117117
A tree sequence with data in all fields execpt edge metadata and migrations
118118
"""
119119
return tsutil.all_fields_ts(edge_metadata=False, migrations=False)
120120

121121

122-
@fixture(scope="session")
122+
@pytest.fixture(scope="session")
123123
def replicate_ts_fixture():
124124
"""
125125
A list of tree sequences

0 commit comments

Comments
 (0)