Skip to content

Commit

Permalink
Bump mypy and pytest, fix simple mypy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
TedBrookings committed Nov 15, 2023
1 parent ca4a572 commit cf7552e
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 67 deletions.
10 changes: 5 additions & 5 deletions fgpyo/io/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ def test_assert_path_is_writeable_pass() -> None:
"""Should return the correct writeable path"""
with NamedTemp(suffix=".txt", mode="w", delete=True) as read_file:
path = Path(read_file.name)
assert fio.assert_path_is_writeable(path=path) is None
fio.assert_path_is_writeable(path=path)


@pytest.mark.parametrize(
"suffix, expected",
[
(".gz", io._io.TextIOWrapper),
(".fa", io._io.TextIOWrapper),
(".gz", io.TextIOWrapper),
(".fa", io.TextIOWrapper),
],
)
def test_reader(
Expand All @@ -103,8 +103,8 @@ def test_reader(
@pytest.mark.parametrize(
"suffix, expected",
[
(".gz", io._io.TextIOWrapper),
(".fa", io._io.TextIOWrapper),
(".gz", io.TextIOWrapper),
(".fa", io.TextIOWrapper),
],
)
def test_writer(
Expand Down
6 changes: 4 additions & 2 deletions fgpyo/util/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@
"""

import logging
import sys

try: # py>=38
if sys.version_info >= (3, 8):
from typing import Literal
except ImportError: # py<38
else:
from typing_extensions import Literal

import socket
from contextlib import AbstractContextManager
from logging import Logger
Expand Down
4 changes: 2 additions & 2 deletions fgpyo/util/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def values(self) -> Iterator[Any]:
"""An iterator over attribute values in the same order as the header."""
return iter(attr.astuple(self, recurse=False))

def formatted_values(self) -> Iterator[str]:
def formatted_values(self) -> List[str]:
"""An iterator over formatted attribute values in the same order as the header."""
return [self.format_value(value) for value in self.values()]

Expand Down Expand Up @@ -226,7 +226,7 @@ def parse(cls, fields: List[str]) -> Any:
return inspect.attr_from(cls=cls, kwargs=dict(zip(header, fields)), parsers=parsers)

@classmethod
def write(cls, path: Path, *values: MetricType) -> None:
def write(cls, path: Path, *values: "Metric") -> None:
"""Writes zero or more metrics to the given path.
The header will always be written.
Expand Down
25 changes: 9 additions & 16 deletions fgpyo/util/types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import collections
import inspect
import sys
import typing
from enum import Enum
from functools import partial
Expand All @@ -9,20 +10,19 @@
from typing import TypeVar
from typing import Union

try:
# `get_origin_type` is a method that gets the outer type (ex list in a List[str])
# `get_arg_types` is a method that gets the inner type (ex str in a List[str])
if sys.version_info >= (3, 8):
from typing import Literal
except ImportError:
from typing_extensions import Literal


# `get_origin_type` is a method that gets the outer type (ex list in a List[str])
if hasattr(typing, "get_origin"): # py>=38
get_origin_type = typing.get_origin
else: # py<38
get_arg_types = typing.get_args
else:
import typing_inspect
from typing_extensions import Literal

def get_origin_type(tp: Type) -> Type:
"""Returns the outer type of a Typing object (ex list in a List[T])"""
import typing_inspect

if type(tp) is type(Literal): # Py<=3.6.
return Literal
Expand All @@ -37,15 +37,8 @@ def get_origin_type(tp: Type) -> Type:
typing.Dict: dict,
}.get(origin, origin)


# `get_origin_type` is a method that gets the inner type (ex str in a List[str])
if hasattr(typing, "get_args"): # py>=38
get_arg_types = typing.get_args
else: # py<38

def get_arg_types(tp: Type) -> Type:
"""Gets the inner types of a Typing object (ex T in a List[T])"""
import typing_inspect

if type(tp) is type(Literal): # Py<=3.6.
return tp.__values__
Expand Down Expand Up @@ -122,7 +115,7 @@ def _make_union_parser_worker(
union: Type[UnionType],
parsers: Iterable[Callable[[str], UnionType]],
value: str,
) -> T:
) -> UnionType:
"""Worker function behind union parsing. Iterates through possible parsers for the union and
returns the value produced by the first parser that works. Otherwise raises an error if none
work"""
Expand Down
13 changes: 8 additions & 5 deletions fgpyo/vcf/tests/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Any
from typing import Dict
from typing import Iterable
from typing import List
from typing import Mapping
from typing import Tuple

Expand Down Expand Up @@ -33,7 +34,7 @@ def sequence_dict() -> Dict[str, Dict[str, Any]]:

def _get_random_contig(
random_generator: random.Random, sequence_dict: Dict[str, Dict[str, Any]]
) -> (str, int):
) -> Tuple[str, int]:
"""Randomly select a contig from the sequence dictionary and return its name and length."""
contig = random_generator.choice(list(sequence_dict.values()))
return contig["ID"], contig["length"]
Expand Down Expand Up @@ -102,7 +103,7 @@ def _get_random_variant_inputs(
@pytest.fixture(scope="function")
def zero_sample_record_inputs(
random_generator: random.Random, sequence_dict: Dict[str, Dict[str, Any]]
) -> Tuple[Mapping[str, Any]]:
) -> Tuple[Mapping[str, Any], ...]:
"""
Fixture with inputs to create test Variant records for zero-sample VCFs (no genotypes).
Make them MappingProxyType so that they are immutable.
Expand Down Expand Up @@ -174,7 +175,7 @@ def test_minimal_inputs() -> None:

def test_sort_order(random_generator: random.Random) -> None:
"""Test if the VariantBuilder sorts the Variant records in the correct order."""
sorted_inputs = [
sorted_inputs: List[Dict[str, Any]] = [
{"contig": "chr1", "pos": 100},
{"contig": "chr1", "pos": 500},
{"contig": "chr2", "pos": 1000},
Expand All @@ -183,7 +184,9 @@ def test_sort_order(random_generator: random.Random) -> None:
{"contig": "chr10", "pos": 20},
{"contig": "chr11", "pos": 5},
]
scrambled_inputs = random_generator.sample(sorted_inputs, k=len(sorted_inputs))
scrambled_inputs: List[Dict[str, Any]] = random_generator.sample(
sorted_inputs, k=len(sorted_inputs)
)
assert scrambled_inputs != sorted_inputs # there should be something to actually sort
variant_builder = VariantBuilder()
for record_input in scrambled_inputs:
Expand Down Expand Up @@ -223,7 +226,7 @@ def _get_is_compressed(input_file: Path) -> bool:
@pytest.mark.parametrize("compress", (True, False))
def test_zero_sample_vcf_round_trip(
temp_path: Path,
zero_sample_record_inputs,
zero_sample_record_inputs: Tuple[Mapping[str, Any], ...],
compress: bool,
) -> None:
"""
Expand Down
110 changes: 74 additions & 36 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ typing_inspect = { version = ">=0.3.1", python = "<3.8" } # inspecting types
sphinx = {version = "4.3.1", optional = true}
sphinx_rtd_theme = {version = "^1.3.0", optional = true}
pysam = ">=0.22.0"
pytest = ">=7.4.0"

[tool.poetry.extras]
docs = ["sphinx", "sphinx_rtd_theme"]

[tool.poetry.dev-dependencies]
setuptools = ">=68.0.0"
pytest = ">=5.4.2"
mypy = ">=0.770"
mypy = [
{ version = ">=0.770", python = "<3.8" },
{ version = ">=1.7.0", python = ">=3.8"}
]
flake8 = [
{ version = ">=3.8.1", python = "<3.12.0" },
{ version = ">=6.1.0", python = ">=3.12.0" },
Expand Down

0 comments on commit cf7552e

Please sign in to comment.