Skip to content

Commit

Permalink
Formatting fixes from ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwhite committed Nov 4, 2024
1 parent 1babb1f commit 528eea0
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 37 deletions.
4 changes: 1 addition & 3 deletions tests/test_regions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

import pytest

from vcztools.regions import parse_region_string
Expand All @@ -15,6 +13,6 @@
],
)
def test_parse_region_string(
targets: str, expected: tuple[str, Optional[int], Optional[int]]
targets: str, expected: tuple[str, int | None, int | None]
):
assert parse_region_string(targets) == expected
6 changes: 3 additions & 3 deletions tests/test_vcf_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def test_write_vcf__filtering(tmp_path, include, exclude, expected_chrom_pos):
assert len(variants) == len(expected_chrom_pos)
assert v.samples == ["NA00001", "NA00002", "NA00003"]

for variant, chrom_pos in zip(variants, expected_chrom_pos):
for variant, chrom_pos in zip(variants, expected_chrom_pos, strict=False):
chrom, pos = chrom_pos
assert variant.CHROM == chrom
assert variant.POS == pos
Expand Down Expand Up @@ -141,7 +141,7 @@ def test_write_vcf__regions(tmp_path, regions, targets,

assert v.samples == ["NA00001", "NA00002", "NA00003"]

for variant, chrom_pos in zip(variants, expected_chrom_pos):
for variant, chrom_pos in zip(variants, expected_chrom_pos, strict=False):
chrom, pos = chrom_pos
assert variant.CHROM == chrom
assert variant.POS == pos
Expand Down Expand Up @@ -230,7 +230,7 @@ def test_write_vcf__regions_samples_filtering(
assert len(variants) == len(expected_chrom_pos)
assert v.samples == ["NA00001"]

for variant, chrom_pos in zip(variants, expected_chrom_pos):
for variant, chrom_pos in zip(variants, expected_chrom_pos, strict=False):
chrom, pos = chrom_pos
assert variant.CHROM == chrom
assert variant.POS == pos
Expand Down
2 changes: 1 addition & 1 deletion vcztools/filter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import functools
import operator
from typing import Callable
from collections.abc import Callable

import numpy as np
import pyparsing as pp
Expand Down
32 changes: 17 additions & 15 deletions vcztools/query.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import functools
import itertools
import math
from typing import Callable, Optional, Union
from collections.abc import Callable

import numpy as np
import pyparsing as pp
Expand Down Expand Up @@ -62,10 +62,10 @@ def __call__(self, *args, **kwargs):
class QueryFormatGenerator:
def __init__(
self,
query_format: Union[str, pp.ParseResults],
query_format: str | pp.ParseResults,
*,
include: Optional[str] = None,
exclude: Optional[str] = None,
include: str | None = None,
exclude: str | None = None,
):
if isinstance(query_format, str):
parser = QueryFormatParser()
Expand Down Expand Up @@ -99,7 +99,7 @@ def generate(root):
end = start + v_chunk_size

for gt_row, phase in zip(
gt_zarray[start:end], phase_zarray[start:end]
gt_zarray[start:end], phase_zarray[start:end], strict=False
):

def stringify(gt_and_phase: tuple):
Expand All @@ -113,7 +113,7 @@ def stringify(gt_and_phase: tuple):
return separator.join(gt)

gt_row = gt_row.tolist()
yield map(stringify, zip(gt_row, phase))
yield map(stringify, zip(gt_row, phase, strict=False))
else:
# TODO: Support datasets without the phasing data
raise NotImplementedError
Expand Down Expand Up @@ -219,8 +219,8 @@ def _compose_sample_loop_generator(

def generate(root):
iterables = (generator(root) for generator in generators)
zipped = zip(*iterables)
zipped_zipped = (zip(*element) for element in zipped)
zipped = zip(*iterables, strict=False)
zipped_zipped = (zip(*element, strict=False) for element in zipped)
flattened_zipped_zipped = (
(
subsubelement
Expand All @@ -234,7 +234,7 @@ def generate(root):
return generate

def _compose_element_generator(
self, element: Union[str, pp.ParseResults], *, sample_loop=False
self, element: str | pp.ParseResults, *, sample_loop=False
) -> Callable:
if isinstance(element, pp.ParseResults):
if element.get_name() == "subfield":
Expand All @@ -261,7 +261,7 @@ def generate(root):
return generate

def _compose_filter_generator(
self, *, include: Optional[str] = None, exclude: Optional[str] = None
self, *, include: str | None = None, exclude: str | None = None
) -> Callable:
assert not (include and exclude)

Expand Down Expand Up @@ -294,8 +294,8 @@ def _compose_generator(
self,
parse_results: pp.ParseResults,
*,
include: Optional[str] = None,
exclude: Optional[str] = None,
include: str | None = None,
exclude: str | None = None,
) -> Callable:
generators = (
self._compose_element_generator(element) for element in parse_results
Expand All @@ -308,7 +308,9 @@ def generate(root) -> str:
iterables = (generator(root) for generator in generators)
filter_iterable = filter_generator(root)

for results, filter_indicator in zip(zip(*iterables), filter_iterable):
for results, filter_indicator in zip(
zip(*iterables, strict=False), filter_iterable, strict=False
):
if filter_indicator:
results = map(str, results)
yield "".join(results)
Expand All @@ -321,8 +323,8 @@ def write_query(
output=None,
*,
query_format: str,
include: Optional[str] = None,
exclude: Optional[str] = None,
include: str | None = None,
exclude: str | None = None,
):
if include and exclude:
raise ValueError(
Expand Down
20 changes: 10 additions & 10 deletions vcztools/regions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import re
from typing import Any, Optional
from typing import Any

import numcodecs
import numpy as np
Expand Down Expand Up @@ -52,7 +52,7 @@ def create_index(vcz) -> None:
)


def parse_region_string(region: str) -> tuple[str, Optional[int], Optional[int]]:
def parse_region_string(region: str) -> tuple[str, int | None, int | None]:
"""Return the contig, start position and end position from a region string."""
if re.search(r":\d+-\d*$", region):
contig, start_end = region.rsplit(":", 1)
Expand All @@ -67,7 +67,7 @@ def parse_region_string(region: str) -> tuple[str, Optional[int], Optional[int]]


def regions_to_pyranges(
regions: list[tuple[str, Optional[int], Optional[int]]], all_contigs: list[str]
regions: list[tuple[str, int | None, int | None]], all_contigs: list[str]
) -> PyRanges:
"""Convert region tuples to a PyRanges object."""

Expand All @@ -90,7 +90,7 @@ def regions_to_pyranges(
return PyRanges(chromosomes=chromosomes, starts=starts, ends=ends)


def parse_regions(regions: Optional[str], all_contigs: list[str]) -> Optional[PyRanges]:
def parse_regions(regions: str | None, all_contigs: list[str]) -> PyRanges | None:
"""Return a PyRanges object from a comma-separated set of region strings."""
if regions is None:
return None
Expand All @@ -100,8 +100,8 @@ def parse_regions(regions: Optional[str], all_contigs: list[str]) -> Optional[Py


def parse_targets(
targets: Optional[str], all_contigs: list[str]
) -> tuple[Optional[PyRanges], bool]:
targets: str | None, all_contigs: list[str]
) -> tuple[PyRanges | None, bool]:
"""Return a PyRanges object from a comma-separated set of region strings,
optionally preceeded by a ^ character to indicate complement."""
if targets is None:
Expand All @@ -113,8 +113,8 @@ def parse_targets(


def regions_to_chunk_indexes(
regions: Optional[PyRanges],
targets: Optional[PyRanges],
regions: PyRanges | None,
targets: PyRanges | None,
complement: bool,
regions_index: Any,
):
Expand Down Expand Up @@ -158,8 +158,8 @@ def regions_to_chunk_indexes(


def regions_to_selection(
regions: Optional[PyRanges],
targets: Optional[PyRanges],
regions: PyRanges | None,
targets: PyRanges | None,
complement: bool,
variant_contig: Any,
variant_position: Any,
Expand Down
2 changes: 1 addition & 1 deletion vcztools/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def stats(vcz, output):
).astype(np.int64)

for contig, contig_length, nr in zip(
contigs, contig_lengths, num_records_per_contig
contigs, contig_lengths, num_records_per_contig, strict=False
):
if nr > 0:
print(f"{contig}\t{contig_length}\t{nr}", file=output)
2 changes: 1 addition & 1 deletion vcztools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def open_file_like(file):
"""A context manager for opening a file path or string (and closing on exit),
or passing a file-like object through."""
with ExitStack() as stack:
if isinstance(file, (str, Path)):
if isinstance(file, str | Path):
file = stack.enter_context(open(file, mode="w"))
yield file

Expand Down
5 changes: 2 additions & 3 deletions vcztools/vcf_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import re
import sys
from datetime import datetime
from typing import Optional

import numpy as np
import zarr
Expand Down Expand Up @@ -89,8 +88,8 @@ def write_vcf(
no_update=None,
samples=None,
drop_genotypes: bool = False,
include: Optional[str] = None,
exclude: Optional[str] = None,
include: str | None = None,
exclude: str | None = None,
) -> None:
"""Convert a dataset to a VCF file.
Expand Down

0 comments on commit 528eea0

Please sign in to comment.