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

TYP,ENH: Improve typing with the help of ParamSpec #20885

Merged
merged 2 commits into from Jan 24, 2022
Merged

Conversation

BvB93
Copy link
Member

@BvB93 BvB93 commented Jan 24, 2022

This PR improves the annotations of a few functions via use of the PEP 612 ParamSpec type, the latter allowing functions to exclusivelly accept *args and **kwargs that are accepted by a wrapped callback.

Mypy's support for PEP 612 is incomplete at this point though, and changes to np.piecewise and np.apply_along_axis are deferred until support for Concatenate is fully implemented (xref python/mypy#8645).

Examples

from typing import TYPE_CHECKING, Callable
from typing_extensions import ParamSpec

P = ParamSpec("P")

def test_func(func: Callable[P, object], *args: P.args, **kwargs: P.kwargs) -> None:
    func(*args, **kwargs)

if TYPE_CHECKING:
    reveal_type(test_func(len, [1]))  # Revealed type is "None"
    reveal_type(test_func(len, [1], "test123"))  # Too many arguments for "test_func"
    reveal_type(test_func(len, [1], test="test123"))  # Unexpected keyword argument "test" for "test_func"

@BvB93 BvB93 changed the title TYP,ENH: Improve typing with the help of ParamSpec TYP,ENH: Improve typing with the help of ParamSpec Jan 24, 2022
@charris charris merged commit 986a879 into numpy:main Jan 24, 2022
@charris
Copy link
Member

charris commented Jan 24, 2022

Thanks Bas.

@BvB93 BvB93 deleted the param_spec branch January 24, 2022 20:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants