8000 Start shipping py.typed with the beta library by richardm-stripe · Pull Request #1074 · stripe/stripe-python · GitHub
[go: up one dir, main page]

Skip to content

Start shipping py.typed with the beta library #1074

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

Merged
merged 8 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ jobs:

- name: Typecheck with pyright
run: make pyright
# Pip won't install newer versions of pyright on python 3.6 (odd, because pyright is a node module)
# so we skip running pyright, and do double duty on python 3.7 using the --pythonversion flag.
if: matrix.python-version != '3.6'

- name: Typecheck with pyright
run: make pyright PYRIGHT_ARGS="--pythonversion 3.6"
if: matrix.python-version == '3.7'

- name: Test with pytest
run: make ci-test
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ pyright: venv
# we don't need to pass config-settings anyway because "editable_mode=compat" just
# means to perform as these old versions of pip already do.
pip install -e . --config-settings editable_mode=compat || pip install -e .
@${VENV_NAME}/bin/pyright
@${VENV_NAME}/bin/pyright --version
@${VENV_NAME}/bin/pyright $(PYRIGHT_ARGS)

fmt: venv
@${VENV_NAME}/bin/tox -e fmt
Expand Down
2 changes: 0 additions & 2 deletions constraints.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
# cryptography 40.0.0 deprecates support for Python 3.6 and PyPy3 < 7.3.10
cryptography<40
# TODO (remove later): pyright 1.1.323 introduces false positive errors
pyright<1.1.323
6 changes: 6 additions & 0 deletions flake8_stripe/flake8_stripe.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class TypingImportsChecker:
"TYPE_CHECKING",
"Type",
"TypedDict",
"NotRequired",
"Self",
"Unpack",
]

allowed_typing_imports = [
Expand All @@ -40,9 +42,13 @@ class TypingImportsChecker:
"cast",
"overload",
"Dict",
"Tuple",
"List",
"Generic",
"Mapping",
"Tuple",
"Iterator",
"Mapping",
]

def __init__(self, tree: ast.AST):
Expand Down
119 changes: 119 additions & 0 deletions flake8_stripe/flake8_stripe.py.orig
10000
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Hint: if you're developing this plugin, test changes with:
# venv/bin/tox -e lint -r
# so that tox re-installs the plugin from the local directory
import ast
from typing import Iterator, Tuple


class TypingImportsChecker:
name = __name__
version = "0.1.0"

# Rules:
# * typing_extensions v4.1.1 is the latest that supports Python 3.6
# so don't depend on anything from a more recent version than that.
#
# If we need something newer, maybe we can provide it for users on
# newer versions with a conditional import, but we'll cross that
# bridge when we come to it.

# If a symbol exists in both `typing` and `typing_extensions`, which
# should you use? Prefer `typing_extensions` if the symbol available there.
# in 4.1.1. In typing_extensions 4.7.0, `typing_extensions` started re-exporting
# EVERYTHING from `typing` but this is not the case in v4.1.1.
allowed_typing_extensions_imports = [
"Literal",
"NoReturn",
"Protocol",
"TYPE_CHECKING",
"Type",
"TypedDict",
"NotRequired",
"Self",
"Unpack",
]

allowed_typing_imports = [
"Any",
"ClassVar",
"Optional",
"TypeVar",
"Union",
"cast",
"overload",
"Dict",
"Tuple",
"List",
"Generic",
<<<<<<< HEAD
"Mapping",
||||||| parent of f2e8187 (Lint)
"Tuple",
=======
"Tuple",
"Iterator",
"Mapping",
>>>>>>> f2e8187 (Lint)
]

def __init__(self, tree: ast.AST):
self.tree = tree

intersection = set(self.allowed_typing_imports) & set(
self.allowed_typing_extensions_imports
)
if len(intersection) > 0:
raise AssertionError(
"TypingImportsChecker: allowed_typing_imports and allowed_typing_extensions_imports must not overlap. Both entries contained: %s"
% (intersection)
)

def run(self) -> Iterator[Tuple[int, int, str, type]]:
for node in ast.walk(self.tree):
if isinstance(node, ast.ImportFrom):
if node.module == "typing":
for name in node.names:
if name.name not in self.allowed_typing_imports:
msg = None
if (
name.name
in self.allowed_typing_extensions_imports
):
msg = (
"SPY100 Don't import %s from 'typing', instead import from 'typing_extensions'"
% (name.name)
)
else:
msg = (
"SPY101 Importing %s from 'typing' is prohibited. Do you need to add to the allowlist in flake8_stripe.py?"
% (name.name)
)
yield (
name.lineno,
name.col_offset,
msg,
type(self),
)
elif node.module == "typing_extensions":
for name in node.names:
if (
name.name
not in self.allowed_typing_extensions_imports
):
msg = None
if name.name in self.allowed_typing_imports:
msg = (
"SPY102 Don't import '%s' from 'typing_extensions', instead import from 'typing'"
% (name.name)
)
else:
msg = (
"SPY103 Importing '%s' from 'typing_extensions' is prohibited. Do you need to add to the allowlist in flake8_stripe.py?"
% (name.name)
)
yield (
name.lineno,
name.col_offset,
msg,
type(self),
)
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ exclude = '''
)
'''
[tool.pyright]
executionEnvironments=[
{"root" = "stripe"}
]
include=["stripe"]
exclude=["build", "**/__pycache__"]
reportMissingTypeArgument=true
reportUnnecessaryCast=true
reportUnnecessaryComparison=true
Expand Down
15 changes: 11 additions & 4 deletions stripe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@
# Configuration variables
from stripe.api_version import _ApiVersion

api_key = None
client_id = None
from stripe.app_info import AppInfo

api_key: Optional[str] = None
client_id: Optional[str] = None
api_base = "https://api.stripe.com"
connect_api_base = "https://connect.stripe.com"
upload_api_base = "https://files.stripe.com"
api_version = _ApiVersion.CURRENT
verify_ssl_certs = True
proxy = None
default_http_client = None
app_info = None
app_info: Optional[AppInfo] = None
enable_telemetry = True
max_network_retries = 0
ca_bundle_path = os.path.join(
Expand Down Expand Up @@ -59,7 +61,12 @@
# communicating with Stripe.
#
# Takes a name and optional version and plugin URL.
def set_app_info(name, partner_id=None, url=None, version=None):
def set_app_info(
name: str,
partner_id: Optional[str] = None,
url: Optional[str] = None,
version: Optional[str] = None,
):
global app_info
app_info = {
"name": name,
Expand Down
13 changes: 11 additions & 2 deletions stripe/api_resources/abstract/api_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@
from stripe import api_requestor, error, util
from stripe.stripe_object import StripeObject
from urllib.parse import quote_plus
from typing import Any, ClassVar, Dict, Generic, Optional, TypeVar, cast
from typing import (
Any,
ClassVar,
Dict,
Generic,
Optional,
TypeVar,
cast,
Mapping,
)

T = TypeVar("T", bound=StripeObject)

Expand Down Expand Up @@ -89,7 +98,7 @@ def _request_and_refresh(
stripe_version: Optional[str] = None,
stripe_account: Optional[str] = None,
headers: Optional[Dict[str, str]] = None,
params: Optional[Dict[str, Any]] = None,
params: Optional[Mapping[str, Any]] = None,
):
obj = StripeObject._request(
self,
Expand Down
Loading
0