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 23, 2024
1 parent 998d710 commit 872880c
Show file tree
Hide file tree
Showing 66 changed files with 890 additions and 1,396 deletions.
29 changes: 6 additions & 23 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,15 @@ repos:
- id: clang-format
exclude: dev-tools|examples
verbose: true
- repo: https://github.com/asottile/reorder_python_imports
rev: v3.12.0
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
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.2
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
2 changes: 1 addition & 1 deletion python/benchmark/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

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
26 changes: 8 additions & 18 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,13 +26,13 @@
compiled module exporting the LightweightTableCollection class.
See the test_example_c_module file for an example.
"""

import copy

import kastore
import msprime
import numpy as np
import pytest

import tskit
import tskit.util as util

Expand Down Expand Up @@ -98,7 +98,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 +183,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 +457,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 +540,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 +572,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 +666,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
8 changes: 8 additions & 0 deletions python/ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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"]
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
8 changes: 3 additions & 5 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,11 +22,11 @@
"""
Python implementation of the IBD-finding algorithms.
"""

import argparse
import collections

import numpy as np

import tskit


Expand All @@ -46,9 +46,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
Loading

0 comments on commit 872880c

Please sign in to comment.