parameterized icon indicating copy to clipboard operation
parameterized copied to clipboard

Type hints

Open adamchainz opened this issue 3 years ago • 8 comments

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.

adamchainz avatar Jul 29 '22 13:07 adamchainz

@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.

kasittig avatar Sep 24 '22 04:09 kasittig

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!

adamchainz avatar Sep 26 '22 09:09 adamchainz

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

sshishov avatar Oct 18 '22 08:10 sshishov

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.

wolever avatar Mar 02 '23 06:03 wolever

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.

adamchainz avatar Mar 17 '23 19:03 adamchainz

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.

wolever avatar Mar 27 '23 01:03 wolever

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

stephew2 avatar Aug 04 '23 14:08 stephew2

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.

adamchainz avatar Aug 06 '23 16:08 adamchainz