Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remaining type hints for extras #81

Merged
merged 2 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/pystow/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
Optional,
Sequence,
Union,
cast,
overload,
)

Expand Down Expand Up @@ -875,7 +876,6 @@ def ensure_csv(
:param download_kwargs: Keyword arguments to pass through to :func:`pystow.utils.download`.
:param read_csv_kwargs: Keyword arguments to pass through to :func:`pandas.read_csv`.
:return: A pandas DataFrame
:rtype: pandas.DataFrame
"""
import pandas as pd

Expand Down Expand Up @@ -1438,7 +1438,6 @@ def ensure_zip_df(
:param download_kwargs: Keyword arguments to pass through to :func:`pystow.utils.download`.
:param read_csv_kwargs: Keyword arguments to pass through to :func:`pandas.read_csv`.
:return: A pandas DataFrame
:rtype: pandas.DataFrame
"""
path = self.ensure(
*subkeys, url=url, name=name, force=force, download_kwargs=download_kwargs
Expand Down Expand Up @@ -1478,7 +1477,6 @@ def ensure_zip_np(
Additional keyword arguments that are passed through to :func:`read_zip_np`
and transitively to :func:`numpy.load`.
:returns: An array-like object
:rtype: numpy.typing.ArrayLike
"""
path = self.ensure(
*subkeys, url=url, name=name, force=force, download_kwargs=download_kwargs
Expand Down Expand Up @@ -1514,7 +1512,6 @@ def ensure_rdf(
Keyword arguments to pass through to :func:`pystow.utils.read_rdf` and transitively to
:func:`rdflib.Graph.parse`.
:return: An RDF graph
:rtype: rdflib.Graph
"""
path = self.ensure(
*subkeys, url=url, name=name, force=force, download_kwargs=download_kwargs
Expand All @@ -1525,7 +1522,7 @@ def ensure_rdf(
cache_path = path.with_suffix(path.suffix + ".pickle.gz")
if cache_path.exists() and not force:
with gzip.open(cache_path, "rb") as file:
return pickle.load(file)
return cast(rdflib.Graph, pickle.load(file))

rv = read_rdf(path=path, **(parse_kwargs or {}))
with gzip.open(cache_path, "wb") as file:
Expand Down
8 changes: 4 additions & 4 deletions src/pystow/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
NamedTuple,
Optional,
Union,
cast,
)
from urllib.parse import urlparse
from urllib.request import urlretrieve
Expand Down Expand Up @@ -736,7 +737,7 @@ def read_zip_np(path: Union[str, Path], inner_path: str, **kwargs: Any) -> "nump

with zipfile.ZipFile(file=path) as zip_file:
with zip_file.open(inner_path) as file:
return np.load(file, **kwargs)
return cast(np.typing.ArrayLike, np.load(file, **kwargs))


def read_zipfile_rdf(path: Union[str, Path], inner_path: str, **kwargs: Any) -> "rdflib.Graph":
Expand All @@ -752,7 +753,7 @@ def read_zipfile_rdf(path: Union[str, Path], inner_path: str, **kwargs: Any) ->
graph = rdflib.Graph()
with zipfile.ZipFile(file=path) as zip_file:
with zip_file.open(inner_path) as file:
graph.load(file, **kwargs)
graph.parse(file, **kwargs)
return graph


Expand Down Expand Up @@ -809,7 +810,6 @@ def read_tarfile_xml(
:param inner_path: The path inside the tar archive to the xml file
:param kwargs: Additional kwargs to pass to :func:`lxml.etree.parse`
:return: An XML element tree
:rtype: lxml.etree.ElementTree
"""
from lxml import etree

Expand All @@ -833,7 +833,7 @@ def read_rdf(path: Union[str, Path], **kwargs: Any) -> "rdflib.Graph":
with (
gzip.open(path, "rb") if isinstance(path, Path) and path.suffix == ".gz" else open(path)
) as file:
graph.parse(file, **kwargs)
graph.parse(file, **kwargs) # type:ignore
return graph


Expand Down
5 changes: 4 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ description = Run linters.
deps =
mypy
types-requests
skip_install = true
extras =
pandas
rdf
xml
commands =
mypy --install-types --non-interactive --ignore-missing-imports --strict src/pystow/
description = Run the mypy tool to check static typing on the project.
Expand Down
Loading