Skip to content

Commit bd43a25

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

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

+890
-1401
lines changed

.pre-commit-config.yaml

+6-28
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.2
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

-7
This file was deleted.

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

+1-1
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
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
18+
import tskit # noqa: E402
1919

2020
with open("config.yaml") as f:
2121
config = yaml.load(f, Loader=yaml.FullLoader)

python/lwt_interface/dict_encoding_testlib.py

+8-18
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,13 +26,13 @@
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
3233
import msprime
3334
import numpy as np
3435
import pytest
35-
3636
import tskit
3737
import tskit.util as util
3838

@@ -98,7 +98,7 @@ def full_ts():
9898

9999
# The ts above is used for the whole test session, but our tests need fresh tables to
100100
# modify
101-
@pytest.fixture
101+
@pytest.fixture()
102102
def tables(full_ts):
103103
return full_ts.dump_tables()
104104

@@ -183,9 +183,7 @@ def test_example(self, tables):
183183
{
184184
"codec": "struct",
185185
"type": "object",
186-
"properties": {
187-
table: {"type": "string", "binaryFormat": "50p"}
188-
},
186+
"properties": {table: {"type": "string", "binaryFormat": "50p"}},
189187
}
190188
)
191189

@@ -459,9 +457,7 @@ def verify_optional_column(self, tables, table_len, table_name, col_name):
459457
out[table_name][col_name], np.zeros(table_len, dtype=np.int32) - 1
460458
)
461459

462-
def verify_offset_pair(
463-
self, tables, table_len, table_name, col_name, required=False
464-
):
460+
def verify_offset_pair(self, tables, table_len, table_name, col_name, required=False):
465461
offset_col = col_name + "_offset"
466462

467463
if not required:
@@ -544,9 +540,7 @@ def test_individuals(self, tables):
544540
self.verify_offset_pair(
545541
tables, len(tables.individuals), "individuals", "location"
546542
)
547-
self.verify_offset_pair(
548-
tables, len(tables.individuals), "individuals", "parents"
549-
)
543+
self.verify_offset_pair(tables, len(tables.individuals), "individuals", "parents")
550544
self.verify_offset_pair(
551545
tables, len(tables.individuals), "individuals", "metadata"
552546
)
@@ -578,9 +572,7 @@ def test_migrations(self, tables):
578572
self.verify_required_columns(
579573
tables, "migrations", ["left", "right", "node", "source", "dest", "time"]
580574
)
581-
self.verify_offset_pair(
582-
tables, len(tables.migrations), "migrations", "metadata"
583-
)
575+
self.verify_offset_pair(tables, len(tables.migrations), "migrations", "metadata")
584576
self.verify_optional_column(tables, len(tables.nodes), "nodes", "individual")
585577
self.verify_metadata_schema(tables, "migrations")
586578

@@ -674,9 +666,7 @@ def get_refseq(d):
674666
assert get_refseq(d).is_null()
675667

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

682672
del refseq_dict["metadata_schema"] # handled above

python/lwt_interface/setup.py

+2-4
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

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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"]

python/setup.py

+2-4
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

+3-4
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

+9-9
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

python/tests/ibd.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# MIT License
22
#
3-
# Copyright (c) 2020-2022 Tskit Developers
3+
# Copyright (c) 2020-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,11 +22,11 @@
2222
"""
2323
Python implementation of the IBD-finding algorithms.
2424
"""
25+
2526
import argparse
2627
import collections
2728

2829
import numpy as np
29-
3030
import tskit
3131

3232

@@ -46,9 +46,7 @@ def __init__(self, left=None, right=None, node=None, next_seg=None):
4646
self.next = next_seg
4747

4848
def __str__(self):
49-
s = "({}-{}->{}:next={})".format(
50-
self.left, self.right, self.node, repr(self.next)
51-
)
49+
s = f"({self.left}-{self.right}->{self.node}:next={self.next!r})"
5250
return s
5351

5452
def __repr__(self):

0 commit comments

Comments
 (0)