wolever / parameterized

Parameterized testing with any Python test framework
Other
833 stars 105 forks source link

Type hints #143

Open adamchainz opened 2 years ago

adamchainz commented 2 years ago

I've been rolling out Mypy on a client project. When activating the disallow_untyped_decorators flag, I found I needed type hints for parametrized. I added the below to the project's stubs folder (on mypy_path:

from __future__ import annotations

from collections.abc import Callable, Iterable, Sequence
from typing import Any, ParamSpec, TypeVar
from unittest import TestCase

P = ParamSpec("P")
T = TypeVar("T")

class param:
    def __init__(self, *args: Any, **kwargs: Any) -> None: ...

InputType = (
    Iterable[str] | Iterable[Sequence[Any]] | Iterable[dict[str, Any]] | Iterable[param]
)

class parameterized:
    def __init__(
        self,
        input: InputType,
        doc_func: Callable[[Callable[..., Any], int, param], str] | None = None,
        skip_on_empty: bool = False,
    ): ...
    def __call__(self, test_func: Callable[P, T]) -> Callable[P, T]: ...
    @classmethod
    def expand(
        cls,
        input: InputType,
        name_func: Callable[[Callable[..., Any], int, param], str] | None = None,
        skip_on_empty: bool = False,
        **legacy: Any,
    ) -> Callable[[Callable[P, T]], Callable[P, T]]: ...

_TestCaseClass = TypeVar("_TestCaseClass", bound=type[TestCase])

def parameterized_class(input: Any) -> Callable[[_TestCaseClass], _TestCaseClass]: ...

These hints cover the main usage of parameterized. If you're interested, I wouldn't mind contributing full type hint coverage to the package.

kasittig commented 1 year ago

@adamchainz - an alternate place to contribute these stubs is the typeshed project! I found your issue while enabling the mypy --strict flag on my own project.

adamchainz commented 1 year ago

Yeah that might be the solution given no response here. If you want to take my hints and open a typeshed PR please feel free!

sshishov commented 1 year ago

We are also interested in this. If it is working, can it be merged please?

wolever commented 1 year ago

Hey folks! Sorry for the delay here. I'd love to have type hint support, and would be more than happy to help get a PR working (although I confess I'm just coming back to Python after a couple of years in TypeScript land, and not yet fully up to speed on best practices).

If you could open a PR that gives an example of how to test that the project is covered by types, I'd be happy to help get them put in place.

adamchainz commented 1 year ago

I just had a look at doing this but found the python 2/3 compatibility code a bit "in the way". It will be hard to get Mypy to properly check it. I think all the compat code should be removed first.

wolever commented 1 year ago

Good news, @adamchainz! Python 2 support is dropped in 0.9! I haven't removed all the compat code yet, but if you can point me in the right direction for how to test with mypy, I'm happy to help iterate on it.

I'm going to cut a 0.9 release today, and adding typing support seems like a feature worthy of a 1.0 release.

stephew2 commented 1 year ago

Any updates on v1.0 and/or typing mypy support for this? Would love to have it! Thanks.

adamchainz commented 1 year ago

I found adding type hints too hard so I wrote my own more minimal parametrised unittest library: https://pypi.org/project/unittest-parametrize/ . It copies pytest’s API as much as possible and comes with full types.