stripe/stripe-python

Can't import PaymentLink

Kjenium opened this issue · 6 comments

Describe the bug

Pylance on vs code just doesn't recognize PaymentLink in stripe

To Reproduce

import stripe on a code
try and perform stripe.PaymentLink.create

Expected behavior

The ide recognizing the methods

Code snippets

No response

OS

Windows 11

Language version

Python 3.11

Library version

5.4.0

API version

I'm not even using any API

Additional context

I tried reinstalling vs code, deleting every vs folder, reinstalling python, it just doesn't get recognized that way

Hi @Kjenium , this SDK doesn't ship with types just yet (this is an open feature request being tracked in #650, and something we are working on!). The types you are seeing likely come from the typeshed stripe type stubs, which Stripe doesn't officially own or maintain. The stubs are missing an annotation for the PaymentLink resource. This won't affect the behavior of the stripe package itself, but you won't get the Intellisense / type checking features offered by Pylance for that resource.

If you'd like, you can contribute to typeshed to fill in the missing stubs since you noticed the issue. Otherwise, I'm happy to create a PR there.

But shouldn’t types just be deduced by variable typing?

We don't distribute the package with type information and don't have type and variable type annotations yet, if that's what you're referring to.

Feel free to provide a code example / screenshots of what you're expecting to see - I assumed you were talking about how there is no autocompletion for PaymentLink and its methods when you try stripe.PaymentLink with Pylance as the language server:
Screenshot 2023-07-14 at 9 18 17 AM

Again, that is an issue with the typeshed stubs, which is not officially maintained by Stripe. I opened a PR to fix this in Typeshed and verified locally that this would address the issue with stripe.PaymentLink... / stripe.PaymentLink.create not being autocompleted.

This now detects stripe.PaymentLink locally for me:
Screenshot 2023-07-13 at 4 39 34 PM

And stripe.PaymentLink.create:
Screenshot 2023-07-13 at 4 45 33 PM

The fact is that I don't get what's the additional information that pylance needed. I would like to contribute myself to typestubs but I'm not getting in this case which typing hint did it need

Because basically doing some test I've noticed that the thing that allows PaymentLink to be imported is from stripe.api_resources.payment_link import PaymentLink as PaymentLink

in init.pyi.

I'm just not getting why a simple import like that changes how pylance behaves. Can't it get that information straight from the code? That's what I mean with the previous comment

In stripe/__init__.pyi, we have from stripe.api_resources import *, and in stripe/api_resources the typeshed stubs previously did not include a stripe/api_resources/payment_link.pyi stub file (so it was never imported in stripe/api_resources/__init__.pyi). If you take a look at the PR I opened to fix this in typeshed, you can see the changes that were necessary to add the PaymentLink type.

As to why adding stripe.api_resources.payment_link import PaymentLink as PaymentLink changes how the type checker behaves, I believe it forces the import loader to load a reference to payment_link on stripe.api_resources, even though it didn't exist before. You may find this page from the Pyright documentation to be helpful.

I'm going to close out this issue since the fix has been merged in typeshed, and it should be resolved the next time Pylance's typeshed fallback is updated.