Skip to content

🏷️ Adding Type Annotations #5

Closed

Description

@YousefEZ

Request:

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

Activity

  1. mentalisttraceur commented on Apr 29, 2023

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

  2. mentalisttraceur commented on Aug 17, 2023

    @mentalisttraceur
    Owner

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

  3. mentalisttraceur commented on Aug 17, 2023

    @mentalisttraceur
    Owner

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

    Confirmed with MyPy, Pyre, and Pyright.

  4. mentalisttraceur commented on Aug 18, 2023

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

  5. mentalisttraceur commented on Aug 18, 2023

    @mentalisttraceur
    Owner

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions