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

Mypy typing? Getting that the methods are not available. Would like to have a clean mypy scan without ignores. #6

Open
DimitrisMilonopoulos opened this issue Jun 1, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@DimitrisMilonopoulos
Copy link

Title.

Thank you for the great library

image

@bhch bhch added the enhancement New feature or request label Jun 1, 2023
@bhch
Copy link
Owner

bhch commented Jun 1, 2023

Sorry, I can't add new features in this library as this is just a patch layer on top of the official stripe library.

I'll keep this issue open, but it won't be implemented until the official library gets typing support first.

See related issue: stripe/stripe-python#650

@aawilson
Copy link

aawilson commented Aug 2, 2023

You should at least be able to support the Awaitable inference, using either a TypeVar

from typing import Awaitable, TypeVar
T = TypeVar("T")
def make_wrapper(some_func: Callable[..., T]) -> Callable[..., Awaitable[T]]):
   async def wrapped(*args, **kwargs) -> T:
      etc
   return wrapped

or alternatively with ParamSpec (using 3.10 or typing_extensions), which will be more useful if Stripe ever types their library (but means nothing until then)

import sys
from typing import Awaitable, TypeVar

if sys.version_info < (3, 10):
    from typing_extensions import ParamSpec
else:
    from typing import ParamSpec

P = ParamSpec("P")
T = TypeVar("T")
def make_wrapper(some_func: Callable[P, T]) -> Callable[P, Awaitable[T]]):
   async def wrapped(*args: P.args, **kwargs: P.kwargs) -> T:
      etc
   return wrapped

Until/if Stripe types their library, this will all resolve to Awaitable[Unknown] but that should at least be enough to not have to hint ignore in usage with await

@aawilson
Copy link

aawilson commented Aug 2, 2023

In the meantime, for anyone stumbling on this who just wants a solution, it is safe to do, e.g.,

from typing import Awaitable, cast

result = await cast(Awaitable, stripe.Customer.retrieve(customer_id))

As long as you're not accidentally importing stripe directly, rather than from async-stripe, this will be the correct inference and will shut up MyPy without having to put # ignores everywhere. Until Stripe types their library, Awaitable[Unknown] is as specific as it gets anyway and you're not masking anything.

@aawilson
Copy link

aawilson commented Aug 2, 2023

(after thinking about this for most of an hour it occurs to me that the monkeypatches actually will still keep this style of inference from working, because they are modifying a library at runtime that is inferred differently from that when it is imported statically. async-stripe would have to be redesigned without monkeypatches, sadly)

@bhch
Copy link
Owner

bhch commented Aug 25, 2023

Small update on this issue: stripe-python just got some basic typing support (PR stripe/stripe-python#1020). If it's possible, I will add typing support as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants