Skip to content

🏷️ Adding Type Annotations #5

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

Closed
YousefEZ opened this issue Apr 28, 2023 · 5 comments
Closed

🏷️ Adding Type Annotations #5

YousefEZ opened this issue Apr 28, 2023 · 5 comments

Comments

@YousefEZ
Copy link

Request:

  • adding type Annotations to the function signatures,
  • add the py.typed marker so that other packages can type check against the type annotations
@mentalisttraceur
Copy link
Owner

Cheers!

My current plan is to release type hints for compose in a secondary stub-only package.

I'm not sure how soon I'll get to it, and there are some tricky things to figure out. (I wrote a little bit more on that in the comments of this StackOverflow question.)

@mentalisttraceur
Copy link
Owner

mentalisttraceur commented Aug 17, 2023

So here's the situation when I last looked into it:

Problem: there's no way to do just one type-hint for variadic compose. We have to do type hint overloads for each arity, like this:

from typing import Callable, TypeVar, ParamSpec, overload

P = ParamSpec('P')
R1 = TypeVar('R1')
R2 = TypeVar('R2')
R3 = TypeVar('R3')
R4 = TypeVar('R4')
@overload
def compose(f2: Callable[[R1], R2], f1: Callable[P, R1]) -> Callable[P, R2]:
    ...
@overload
def compose(f3: Callable[[R2], R3], f2: Callable[[R1], R2], f1: Callable[P, R1]) -> Callable[P, R3]:
    ...
@overload
def compose(f4: Callable[[R3], R4], f3: Callable[[R2], R3], f2: Callable[[R1], R2], f1: Callable[P, R1]) -> Callable[P, R4]:
    ...
# and so on, up to some large number

When I last tried that, it seemed that MyPy had issues with typing.overload in the same file as the code; I haven't yet gotten around to testing it in a separate typing stub file, but if that works then that might be the best we can do.

(I'm hoping that the above type-hint overloads work well enough for most cases despite compose/acompose/sacompose being classes with callable instances - a more precise solution would be to type-hint __init__ and __call__ in a way that links the types between them, assuming the existing type hint stuff can even handle that.)

@mentalisttraceur
Copy link
Owner

mentalisttraceur commented Aug 17, 2023

Finally got around to sitting down and trying the typing stubs package approach. It works!

Confirmed with MyPy, Pyre, and Pyright.

@mentalisttraceur
Copy link
Owner

Implemented in compose-stubs, about as well as it can be.

I'll promote the stubs to stable v1 release (and push out a compose release with the description updated to mention the stubs package) soon unless someone catches a serious problem that I missed.

@mentalisttraceur
Copy link
Owner

mentalisttraceur commented Aug 18, 2023

Okay, compose-stubs is now officially released as v1.0.0, and compose v1.5.0 now officially points to it in the package description and repo README. 🎉

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

2 participants