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 mypy to CI job and type hints for one class. #404

Open
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

eliotwrobson
Copy link

Changes to test type hints and integrating a typechecker into the CI job.
This was to test feasibility of adding type annotations to the package (per
the issue). This was surprisingly easy to do, I basically just copied the types
listed in the docstring.

Relevant issues/PRs:

#330

Copy link

welcome bot commented Mar 11, 2024

💖 Thank you for opening your first pull request in this repository! 💖

A few things to keep in mind:

No matter what, we are really grateful that you put in the effort to do this!

@eliotwrobson eliotwrobson marked this pull request as ready for review January 23, 2025 18:21
@eliotwrobson
Copy link
Author

Hi @santisoler, is there anything I could do to help move this along? This change would be helpful to me when working with this library, and it seems like there has been interest from others as well.

Copy link
Member

@santisoler santisoler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @eliotwrobson. Yes, I totally agree. Type hints is something people would like to see in Pooch (me included!).

I left a few comments here so we can move forward with this PR.

Importing from typing

I noticed that in this PR you import typing as t, and then use each one of the types in signatures with t.<TYPE>, like t.Optional, t.List`, etc.

Why not importing the required types directly? Like from typing import .... Most of the codebases that use type hints use the modules like this:

from typing import Optional

def f(x: Optional[float] = None):
    ...

I think having that extra t. creates some clutter in the signatures, specially when we need to use several types. What do you think?

Do we need typing_extensions?

I noticed that you added typing_extensions as a dependency. I'm curious if this strictly necessary. I left a comment below where we can continue the conversation.

Minor things to do

Before I forget, we need to add the new style requirements to environment.yml as well. Thanks for adding them to requirements-style.txt and to add a new target to the Makefile!


Thanks again for the effort you put in this! I really appreciate that we can move forward with type hints in Pooch!

types-tqdm
types-paramiko
mypy
pytest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why we need pytests for checking styles?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mypy is run over the test case files as well, so this needs pytest installed to recognize the import.

pooch/core.py Outdated
Comment on lines 32 to 52
FilePath = t.Union[str, os.PathLike]
Actions = te.Literal["download", "fetch", "update"]


class DownloaderT(te.Protocol):
"""
A class used to define the type definition for the downloader function.
"""

# pylint: disable=too-few-public-methods
def __call__( # noqa: E704
self,
fname: str,
action: t.Optional[FilePath],
pooch: "Pooch",
*,
check_only: t.Optional[bool] = None,
) -> t.Any: ...


ProcessorT = t.Callable[[str, Actions, "Pooch"], t.Any]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd like to separate the public classes from the type variables in different submodules. What about defining a pooch.typing submodule where these can live?

On a side note, would it be feasible to name them without the T? Or will it create more confusion?

Copy link
Author

@eliotwrobson eliotwrobson Jan 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would work. The T is my convention but I can remove that as well.

EDIT: It seems like separating this would cause a circular import because there's a method that takes in a callable that calls Pooch. This also seems to cause runtime errors for some reason.

@eliotwrobson
Copy link
Author

eliotwrobson commented Jan 25, 2025

@santisoler I've responded to your comments. I've added mypy to the environment.yml, removed typing_extensions, and changed the typing import. Also, let me know if you want any help with #453, since I'm currently using ruff to autoformat everything locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants