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

Type hints #143

Open
adamchainz opened this issue Jul 29, 2022 · 8 comments
Open

Type hints #143

adamchainz opened this issue Jul 29, 2022 · 8 comments

Comments

@adamchainz
Copy link
Contributor

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
Copy link

@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
Copy link
Contributor Author

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
Copy link

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

@wolever
Copy link
Owner

wolever commented Mar 2, 2023

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
Copy link
Contributor Author

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
Copy link
Owner

wolever commented Mar 27, 2023

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 wolever pinned this issue Mar 27, 2023
@stephew2
Copy link

stephew2 commented Aug 4, 2023

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

@adamchainz
Copy link
Contributor Author

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.

PeterJCLaw added a commit to PeterJCLaw/mockafka-py that referenced this issue Jul 6, 2024
PeterJCLaw added a commit to PeterJCLaw/mockafka-py that referenced this issue Jul 6, 2024
PeterJCLaw added a commit to PeterJCLaw/mockafka-py that referenced this issue Jul 6, 2024
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

No branches or pull requests

5 participants