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

Feature Request: type annotations #650

Closed
chdsbd opened this issue Apr 4, 2020 · 10 comments
Closed

Feature Request: type annotations #650

chdsbd opened this issue Apr 4, 2020 · 10 comments
Labels

Comments

@chdsbd
Copy link

chdsbd commented Apr 4, 2020

I think adding type annotations to this library would be helpful for mypy users.

@ob-stripe
Copy link
Contributor

Hi @chdsbd, thanks for the feature request and sorry for delayed reply.

While I agree it would be awesome to have type annotations in the library, we still need to support Python 2.7 for the time being despite it having reached EOL at the beginning of the year, as a large portion of our user base has yet to upgrade.

Additionally, in its current form the stripe-python library does not know the exact shape of API requests and responses -- it accepts arbitrary maps both ways. So even if we had type annotations, many of them would look like t.Dict[str, t.Any] which is better than nothing but not extremely useful.

In order to offer exact types like we do in our typed language libraries (stripe-java, stripe-dotnet, etc.) and in TypeScript, we would need to pin the library to a specific API version. It's something we're considering, but it would be a huge change from the current state so we don't plan on making this change soon.

Note that we're also considering shipping a separate, Python 3.6+ only version of stripe-python with full support for types and async methods. We haven't made a lot of progress towards this recently, but it's still on our roadmap.

sbdchd added a commit to chdsbd/kodiak that referenced this issue Aug 11, 2020
Now we explicitly ignore specific imports instead of a blanket ignore
on all missing types.

Upgraded databases and pytest to get their latest versions which include
types, for the rest we're ignoring them explicitly.
see #485

- arrow

  doesn't have types but we can replace it with std library calls

- requests_async

  deprecated in favor of httpx, so we'd need to upgrade to get types.
  The API of httpx is a little different as well.

- structlog

  doesn't have types currently

  hynek/structlog#165

- rure

  No stubs yet

  davidblewett/rure-python#23

- pytest_mock

  types recently added to the typeshed so we'll need to wait for the next
  release of mypy.

  pytest-dev/pytest-mock#152

- markdown_html_finder

  Wrapper around some Rust code. Needs stubs to be added.

  chdsbd/markdown-html-finder#5

- asyncio_redis

  We're our own fork of it, but adding types would be rather manual.
  Granted we aren't using that much of the API surface.

  https://github.com/chdsbd/asyncio-redis

- zstandard

  No stubs yet, so we're ignoring for the time being

  indygreg/python-zstandard#120

- inflection

  Types, but I think they're misconfigured, once they're fixed we can
  unignore.

  jpvanhal/inflection#49

- django

  Need to setup django-stubs for this to work since django doesn't have
  any static types.

  https://github.com/TypedDjango/django-stubs

- dj_database_url

  Doesn't have any static types yet

  jazzband/dj-database-url#135

- responses

  No types yet

  getsentry/responses#339

- stripe

  Plan for types, but they don't exist currently

  stripe/stripe-python#650
kodiakhq bot pushed a commit to chdsbd/kodiak that referenced this issue Aug 11, 2020
Now we explicitly ignore specific imports instead of a blanket ignore
on all missing types.

Upgraded databases and pytest to get their latest versions which include
types, for the rest we're ignoring them explicitly.
see #485

- arrow

  doesn't have types but we can replace it with std library calls

- requests_async

  deprecated in favor of httpx, so we'd need to upgrade to get types.
  The API of httpx is a little different as well.

- structlog

  doesn't have types currently

  hynek/structlog#165

- rure

  No stubs yet

  davidblewett/rure-python#23

- pytest_mock

  types recently added to the typeshed so we'll need to wait for the next
  release of mypy.

  pytest-dev/pytest-mock#152

- markdown_html_finder

  Wrapper around some Rust code. Needs stubs to be added.

  chdsbd/markdown-html-finder#5

- asyncio_redis

  We're using our own fork of it, but adding types would be rather manual.
  Granted we aren't using that much of the API surface.

  https://github.com/chdsbd/asyncio-redis

- zstandard

  No stubs yet, so we're ignoring for the time being

  indygreg/python-zstandard#120

- inflection

  Types, but I think they're misconfigured, once they're fixed we can
  unignore.

  jpvanhal/inflection#49

- django

  Need to setup django-stubs for this to work since django doesn't have
  any static types.

  https://github.com/TypedDjango/django-stubs

- dj_database_url

  Doesn't have any static types yet

  jazzband/dj-database-url#135

- responses

  No types yet

  getsentry/responses#339

- stripe

  Plan for types, but they don't exist currently

  stripe/stripe-python#650
@nihaals
Copy link

nihaals commented Aug 16, 2020

Having a separate stripe-stubs package could also be helpful so it wouldn't break old versions (although definitely all for dropping <3.6, could help add more modern features like async)

sbdchd added a commit to chdsbd/kodiak that referenced this issue Nov 12, 2020
Added basic type stubs for some of the smaller libraries the bot and
web_api use.

Libraries with missing types that remain:
- structlog, waiting on the release of its typed api
- requests_async, need to convert to httpx which has types
- asyncio_redis, maybe write stubs, or use a different client
- django, stubs exist, last time we tried it, there were a number of
  false positives
- stripe, no types currently, could probably write some minimal stubs
  rel: stripe/stripe-python#650
kodiakhq bot pushed a commit to chdsbd/kodiak that referenced this issue Nov 13, 2020
Added basic type stubs for some of the smaller libraries the bot and
web_api use.

We put the types in the `typings` folder which we configure in the Mypy, 
but is also the [default path that pyright looks at](https://github.com/microsoft/pyright/blob/b9e61e9f2ae6179284fed8e7b534e5dcb46c8f8b/docs/configuration.md)
so pylance should just work.

Libraries with missing types that remain:
- structlog, waiting on the release of its typed api
- requests_async, need to convert to httpx which has types
- asyncio_redis, maybe write stubs, or use a different client
- django, stubs exist, last time we tried it, there were a number of
  false positives
- stripe, no types currently, could probably write some minimal stubs
  rel: stripe/stripe-python#650


rel: indygreg/python-zstandard#120
rel: davidblewett/rure-python#23
rel: jazzband/dj-database-url#135
@dalanmiller
Copy link

@nihaals have you seen the *-stubs pattern used elsewhere?

@nihaals
Copy link

nihaals commented May 15, 2021

have you seen the *-stubs pattern used elsewhere?

I can think of a few:

Typeshed uses types-*. I rarely need to install separate stub packages so I'm not sure what the most popular convention is. awesome-python-typing includes a list of some "awesome" stub packages which might be a useful resource.

I believe typeshed aims to be something similar to TypeScript's DefinitelyTyped, which, for TS, is definitely the most popular and really the best place to put types if they're not included in the library, so typeshed might be best. A bonus of this is some type checkers already include/use typeshed, so users won't even need to install the stubs package themselves.

@andersk
Copy link
Contributor

andersk commented Aug 3, 2021

While I agree it would be awesome to have type annotations in the library, we still need to support Python 2.7 for the time being despite it having reached EOL at the beginning of the year, as a large portion of our user base has yet to upgrade.

There is a Python 2–compatible annotation syntax.

https://www.python.org/dev/peps/pep-0484/#suggested-syntax-for-python-2-7-and-straddling-code
https://mypy.readthedocs.io/en/stable/python2.html

@tony
Copy link
Contributor

tony commented Oct 3, 2022

types-stripe: https://pypi.org/project/types-stripe/ (https://github.com/python/typeshed/tree/master/stubs/stripe)

There seems to have been recent activity in this front, python/typeshed#8696, #871

@ekeric13
Copy link

Sorry, I am new to python annotations. How would I use the types in typeshed to annotate my code?
I get Import "types_stripe" could not be resolved when I install it and import it

@bosmobosmo
Copy link

Sorry, I am new to python annotations. How would I use the types in typeshed to annotate my code? I get Import "types_stripe" could not be resolved when I install it and import it

You just need to import stripe. Typeshed is only used by your type checker and doesn't need to be imported.

@Avasam
Copy link

Avasam commented Oct 14, 2023

Separate stubs are an option. You don't even need to create a separate stripe-stubs package (which could be shipped as part of the stripe distribution), you can just include your type-stubs files (.pyi) along the runtime ones (.py).

Stub files have the advantage of being (mostly) version agnostic (there's some edge cases), but can fall out of sync with the original code. Case in point: typeshed is still on stripe 3.5 because no maintainer or contributor care to spend the time on these stubs.

Inline type annotations on the other hand, are restricted by the lowest supported python version. Luckily, most features have been backported thanks to typing_extensions or by using the typing module. Type-checking-only code can also be hidden behind a typing.TYPE_CHECKING check. The validity of your syntax, annotations and imports can also statically be checked thanks to static type-checkers like mypy, pyright, pytype, pyre, etc. (typeshed validates for mypy, pyright and pytype)

Type comments are a thing for Python 2 code, but they can get really unwieldy. Thankfully support for Python 2 has been dropped since version 6.0.0.

Edit: Just saw all the typing-related PRs, so it seems this is well on its way! https://github.com/stripe/stripe-python/pulls?q=is%3Apr+sort%3Aupdated-desc+is%3Aclosed+type & #1074

@richardm-stripe
Copy link
Contributor

We have been gradually adding type annotations and finally activated them, shipping py.typed last week in https://github.com/stripe/stripe-python/releases/tag/v7.1.0.

Your feedback is welcome, please upgrade your stripe-python and give the types a spin!

The approach we've taken was to try and annotate the interface for stripe-python as it already exists, which mean using relatively new type features like **params: Unpack[ParamsTypedDict] instead of introducing new parameter classes. This means that the best experience comes from type checkers like pyright that seem to be a little bit further along in their support for Unpack than mypy is, so be aware of that.

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

No branches or pull requests

10 participants