Skip to content

Commit

Permalink
Switch linting to Ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
benjeffery committed May 24, 2024
1 parent 998d710 commit d9256d9
Show file tree
Hide file tree
Showing 66 changed files with 861 additions and 1,303 deletions.
34 changes: 6 additions & 28 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,10 @@ repos:
- id: clang-format
exclude: dev-tools|examples
verbose: true
- repo: https://github.com/asottile/reorder_python_imports
rev: v3.12.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.5
hooks:
- id: reorder-python-imports
args: [--application-directories=python,
--unclassifiable-application-module=_tskit]
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.2
hooks:
- id: pyupgrade
args: [--py3-plus, --py38-plus]
- repo: https://github.com/psf/black
rev: 24.4.2
hooks:
- id: black
language_version: python3
- repo: https://github.com/pycqa/flake8
rev: 7.0.0
hooks:
- id: flake8
args: [--config=python/.flake8]
additional_dependencies: ["flake8-bugbear==23.9.16", "flake8-builtins==2.1.0"]
- repo: https://github.com/asottile/blacken-docs
rev: 1.16.0
hooks:
- id: blacken-docs
args: [--skip-errors]
additional_dependencies: [black==22.3.0]
language_version: python3
- id: ruff
args: [ "--fix", "--config", "python/ruff.toml" ]
- id: ruff-format
args: [ "--config", "python/ruff.toml" ]
7 changes: 0 additions & 7 deletions python/.flake8

This file was deleted.

2 changes: 1 addition & 1 deletion python/benchmark/run-for-all-releases.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import json
import subprocess
from distutils.version import StrictVersion
from urllib.request import urlopen

import tqdm
from distutils.version import StrictVersion


def versions(package_name):
Expand Down
3 changes: 2 additions & 1 deletion python/benchmark/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

tskit_dir = Path(__file__).parent.parent
sys.path.append(str(tskit_dir))
import tskit # noqa: E402
import msprime # noqa: E402

import tskit # noqa: E402

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

Expand Down
25 changes: 8 additions & 17 deletions python/lwt_interface/dict_encoding_testlib.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MIT License
#
# Copyright (c) 2018-2022 Tskit Developers
# Copyright (c) 2018-2024 Tskit Developers
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand All @@ -26,6 +26,7 @@
compiled module exporting the LightweightTableCollection class.
See the test_example_c_module file for an example.
"""

import copy

import kastore
Expand Down Expand Up @@ -98,7 +99,7 @@ def full_ts():

# The ts above is used for the whole test session, but our tests need fresh tables to
# modify
@pytest.fixture
@pytest.fixture()
def tables(full_ts):
return full_ts.dump_tables()

Expand Down Expand Up @@ -183,9 +184,7 @@ def test_example(self, tables):
{
"codec": "struct",
"type": "object",
"properties": {
table: {"type": "string", "binaryFormat": "50p"}
},
"properties": {table: {"type": "string", "binaryFormat": "50p"}},
}
)

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

def verify_offset_pair(
self, tables, table_len, table_name, col_name, required=False
):
def verify_offset_pair(self, tables, table_len, table_name, col_name, required=False):
offset_col = col_name + "_offset"

if not required:
Expand Down Expand Up @@ -544,9 +541,7 @@ def test_individuals(self, tables):
self.verify_offset_pair(
tables, len(tables.individuals), "individuals", "location"
)
self.verify_offset_pair(
tables, len(tables.individuals), "individuals", "parents"
)
self.verify_offset_pair(tables, len(tables.individuals), "individuals", "parents")
self.verify_offset_pair(
tables, len(tables.individuals), "individuals", "metadata"
)
Expand Down Expand Up @@ -578,9 +573,7 @@ def test_migrations(self, tables):
self.verify_required_columns(
tables, "migrations", ["left", "right", "node", "source", "dest", "time"]
)
self.verify_offset_pair(
tables, len(tables.migrations), "migrations", "metadata"
)
self.verify_offset_pair(tables, len(tables.migrations), "migrations", "metadata")
self.verify_optional_column(tables, len(tables.nodes), "nodes", "individual")
self.verify_metadata_schema(tables, "migrations")

Expand Down Expand Up @@ -674,9 +667,7 @@ def get_refseq(d):
assert get_refseq(d).is_null()

# All empty strings is the same thing
d["reference_sequence"] = dict(
data="", url="", metadata_schema="", metadata=b""
)
d["reference_sequence"] = dict(data="", url="", metadata_schema="", metadata=b"")
assert get_refseq(d).is_null()

del refseq_dict["metadata_schema"] # handled above
Expand Down
6 changes: 2 additions & 4 deletions python/lwt_interface/setup.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import os.path
import platform

from setuptools import Extension
from setuptools import setup
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext


IS_WINDOWS = platform.system() == "Windows"


# Obscure magic required to allow numpy be used as a 'setup_requires'.
# Based on https://stackoverflow.com/questions/19919905
class local_build_ext(build_ext):
class local_build_ext(build_ext): # noqa: N801
def finalize_options(self):
build_ext.finalize_options(self)
import builtins
Expand Down
12 changes: 12 additions & 0 deletions python/ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
line-length = 90

[lint]
select = ["E", "F", "B", "W", "I", "N", "UP", "A", "RUF", "PT", "NPY"]
# N803,806,802 Allow capital varnames
# E741 Allow "l" as var name
# PT011 allow pytest raises without match
ignore = ["N803", "N806", "N802", "E741", "PT011", "PT009"]

[lint.isort]
section-order = ["future", "standard-library", "third-party", "first-party", "local-folder"]
known-first-party = ["tskit", "_tskit"]
6 changes: 2 additions & 4 deletions python/setup.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import os.path
import platform

from setuptools import Extension
from setuptools import setup
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext


IS_WINDOWS = platform.system() == "Windows"


# Obscure magic required to allow numpy be used as a 'setup_requires'.
# Based on https://stackoverflow.com/questions/19919905
class local_build_ext(build_ext):
class local_build_ext(build_ext): # noqa: N801
def finalize_options(self):
build_ext.finalize_options(self)
import builtins
Expand Down
7 changes: 3 additions & 4 deletions python/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MIT License
#
# Copyright (c) 2018-2023 Tskit Developers
# Copyright (c) 2018-2024 Tskit Developers
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand All @@ -22,6 +22,7 @@
import base64

import tskit

from . import tsutil
from .simplify import * # NOQA

Expand Down Expand Up @@ -195,9 +196,7 @@ def trees(self):
pt.left = left
pt.right = right
# Add in all the sites
pt.site_list = [
site for site in self._sites if left <= site.position < right
]
pt.site_list = [site for site in self._sites if left <= site.position < right]
yield pt
pt.index += 1
pt.index = -1
Expand Down
18 changes: 9 additions & 9 deletions python/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MIT License
#
# Copyright (c) 2018-2022 Tskit Developers
# Copyright (c) 2018-2024 Tskit Developers
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -38,9 +38,9 @@ def test_something(self, ts_fixture):
Note that fixtures have a "scope" for example `ts_fixture` below is only created once
per test session and re-used for subsequent tests.
"""

import msprime
import pytest
from pytest import fixture

from . import tsutil

Expand Down Expand Up @@ -81,45 +81,45 @@ def pytest_collection_modifyitems(config, items):
item.add_marker(skip_slow)


@fixture
@pytest.fixture()
def overwrite_viz(request):
return request.config.getoption("--overwrite-expected-visualizations")


@fixture
@pytest.fixture()
def draw_plotbox(request):
return request.config.getoption("--draw-svg-debug-box")


@fixture(scope="session")
@pytest.fixture(scope="session")
def simple_degree1_ts_fixture():
return msprime.simulate(10, random_seed=42)


@fixture(scope="session")
@pytest.fixture(scope="session")
def simple_degree2_ts_fixture():
ts = msprime.simulate(10, recombination_rate=0.2, random_seed=42)
assert ts.num_trees == 2
return ts


@fixture(scope="session")
@pytest.fixture(scope="session")
def ts_fixture():
"""
A tree sequence with data in all fields
"""
return tsutil.all_fields_ts()


@fixture(scope="session")
@pytest.fixture(scope="session")
def ts_fixture_for_simplify():
"""
A tree sequence with data in all fields execpt edge metadata and migrations
"""
return tsutil.all_fields_ts(edge_metadata=False, migrations=False)


@fixture(scope="session")
@pytest.fixture(scope="session")
def replicate_ts_fixture():
"""
A list of tree sequences
Expand Down
7 changes: 3 additions & 4 deletions python/tests/ibd.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MIT License
#
# Copyright (c) 2020-2022 Tskit Developers
# Copyright (c) 2020-2024 Tskit Developers
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand All @@ -22,6 +22,7 @@
"""
Python implementation of the IBD-finding algorithms.
"""

import argparse
import collections

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

def __str__(self):
s = "({}-{}->{}:next={})".format(
self.left, self.right, self.node, repr(self.next)
)
s = f"({self.left}-{self.right}->{self.node}:next={self.next!r})"
return s

def __repr__(self):
Expand Down
18 changes: 8 additions & 10 deletions python/tests/simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"""
Python implementation of the simplify algorithm.
"""

import sys

import numpy as np
Expand Down Expand Up @@ -82,9 +83,7 @@ def __init__(self, left=None, right=None, node=None, next_segment=None):
self.next = next_segment

def __str__(self):
s = "({}-{}->{}:next={})".format(
self.left, self.right, self.node, repr(self.next)
)
s = f"({self.left}-{self.right}->{self.node}:next={self.next!r})"
return s

def __repr__(self):
Expand Down Expand Up @@ -305,7 +304,8 @@ def merge_labeled_ancestors(self, S, input_id):
if is_sample:
# Free up the existing ancestry mapping.
x = self.A_tail[input_id]
assert x.left == 0 and x.right == self.sequence_length
assert x.left == 0
assert x.right == self.sequence_length
self.A_tail[input_id] = None
self.A_head[input_id] = None

Expand Down Expand Up @@ -333,8 +333,7 @@ def merge_labeled_ancestors(self, S, input_id):
# Fill in any gaps in the ancestry for the sample
self.add_ancestry(input_id, prev_right, left, output_id)
if self.keep_unary or (
self.keep_unary_in_individuals
and self.ts.node(input_id).individual >= 0
self.keep_unary_in_individuals and self.ts.node(input_id).individual >= 0
):
ancestry_node = output_id
self.add_ancestry(input_id, left, right, ancestry_node)
Expand Down Expand Up @@ -637,9 +636,7 @@ def process_parent_edges(self, edges):
x = self.A_head[edge.child]
while x is not None:
if x.right > edge.left and edge.right > x.left:
y = Segment(
max(x.left, edge.left), min(x.right, edge.right), x.node
)
y = Segment(max(x.left, edge.left), min(x.right, edge.right), x.node)
S.append(y)
x = x.next
self.merge_labeled_ancestors(S, parent)
Expand All @@ -654,7 +651,8 @@ def merge_labeled_ancestors(self, S, input_id):
if is_sample:
# Free up the existing ancestry mapping.
x = self.A_tail[input_id]
assert x.left == 0 and x.right == self.sequence_length
assert x.left == 0
assert x.right == self.sequence_length
self.A_tail[input_id] = None
self.A_head[input_id] = None

Expand Down
Loading

0 comments on commit d9256d9

Please sign in to comment.