Skip to content

feat: Add py.typed marker and fix type annotations - #180

Merged
davidgrayston-paddle merged 38 commits into
mainfrom
fix/py-typed
Nov 24, 2026
Merged

davidgrayston-paddle merged 38 commits into
mainfrom
fix/py-typed

Conversation

@davidgrayston-paddle

@davidgrayston-paddle davidgrayston-paddle commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Added

  • Added a py.typed marker, so mypy and pyright check your code against the SDK's type annotations. Package __init__ modules now re-export their names explicitly, so imports such as from paddle_billing import Client pass type checkers
  • Added BodyRequest, ContentRequest and DataRequest protocols to paddle_billing.Notifications.Requests, for typing your own webhook request objects

Changed

  • mypy now checks code that uses the SDK against its annotations, where before it treated the SDK as untyped, so upgrading may report new errors. For example, client.customers.response is Response | None, and fields the API can return as null are optional. Under mypy --strict, a bare CreateReport annotation needs type arguments, such as CreateReport[Any, Any]. Pyright already read the SDK's annotations
  • Verifier.verify() and NotificationEvent.from_request() take Request | BodyRequest | ContentRequest | DataRequest, so Django, Flask, httpx and requests request objects pass type checkers

Fixed

  • Verifier(maximum_variance=None) no longer raises a TypeError on verify(). None skips the time drift check, like 0
  • Verifier.verify() and NotificationEvent.from_request() skip a body, content or data attribute that is None and read the next one, instead of raising AttributeError
  • NotificationPayout.from_dict() no longer raises TypeError: 'module' object is not callable
  • Importing paddle_billing.Notifications.Events.PaymentMethodDeleted no longer raises an ImportError

These entries are in CHANGELOG.md under Unreleased.

The branch is rebased onto main, which brought in the API key modules. They get the same typing fixes as the rest of the SDK: explicit re-exports, member() enum declarations and ApiKeysClient.response typed as Response | None.

Why the re-exports are needed

Adding py.typed on its own breaks the SDK's documented imports for pyright users.

Type checkers apply stricter rules to packages that ship py.typed (PEP 484, typing spec). In a typed package, a name imported into an __init__.py with a plain from module import Name counts as private to that module, not as part of the package's public interface. Only from module import Name as Name, or listing the name in __all__, marks it as re-exported.

Every __init__.py in the SDK used the plain form. Without this change, pyright reports an error on imports that the README uses:

from paddle_billing import Client
# error: "Client" is not exported from module "paddle_billing"
#   Import from "paddle_billing.Client" instead (reportPrivateImportUsage)

mypy --strict reports the same thing (Module "paddle_billing" does not explicitly export attribute "Client"). Without the marker neither check runs, which is why this hasn't come up before.

The __init__ changes were generated mechanically: Name as Name for each import, and __all__ in the five modules that import a name under a different one (for example Includes as PriceIncludes), because a rename is only public through __all__. Those __all__ lists also name the subpackage modules that from ...Operations import * picked up before, such as List, Price, Create and Update, so star imports export the same names as before; a test pins them. Imports that the typing changes left unused but that worked before, such as Request from NotificationEvent and ReportType and ReportFilter from CreateReport, are kept so they still work, and re-exported so type checkers accept them too. Otherwise runtime behaviour is unchanged.

Why the webhook request parameter is a union of protocols

verify() and from_request() were typed with the Request protocol, which requires body, content and data together as settable attributes. No framework request has all three (Django has body, Flask has data, httpx and requests have content), and frameworks expose them as read-only properties. So once the package is typed, mypy and pyright would reject Verifier().verify(request, ...) for Django, Flask, httpx and requests, and for a custom class that has only a body.

The parameter is now a union of the existing Request protocol and three read-only protocols, BodyRequest, ContentRequest and DataRequest, each needing headers and one body attribute. That matches what the SDK reads at runtime, keeps existing Request implementations valid, and still rejects objects with no body attribute. headers is typed Any in the new protocols, because Django's, Flask's and requests' header types don't match a get() protocol in both mypy and pyright. Checked with mypy --strict (with django-stubs) and pyright against real Django, Flask, httpx and requests objects.

One small runtime change: a body attribute set to None is now skipped, and the SDK moves on to the next attribute, instead of raising AttributeError.

Why the other type fixes are in this PR

The marker publishes the SDK's annotations, so they need to be right when it ships. mypy paddle_billing reported 1,793 errors:

  • 1,693 came from how enum members are declared (Active: "Status" = "active"). The annotation is deliberate: it makes Status.Active an instance of Status for type checkers. Members are now declared with a small helper, Active: "Status" = member("active"), which returns its value typed as Any, so the annotation type-checks without disabling any check. Runtime values are identical for all 162 enums, and a member declared the old way fails mypy.
  • The other 100 were wrong or incomplete annotations, fixed here. Three of them were real runtime bugs in code no test ran (listed under Fixed), each now covered by a test that failed first. A new test also imports every SDK module, which is how the PaymentMethodDeleted import error is caught.

mypy paddle_billing is now clean and runs in the lint workflow, so new annotation errors fail CI. pyright reports no errors over paddle_billing. It does report warnings where those __all__ lists name subpackage modules, which pyright can't see as imported; they don't affect users.

Type-checking tests

tests/Unit/Typing runs mypy --strict from pytest over usage files written the way users write code: the README examples, and webhook verification with Django-, Flask- and httpx-shaped requests, a custom request and a Request implementation. It also checks that invalid usage is still rejected, including a request with no body attribute, so the test can't pass just because the types went missing. It fails if the re-exports or the webhook fix are reverted. tests/Unit/test_PackageExports.py fails if a package __init__.py imports a name without re-exporting it. mypy is added to the dev extras.

Reviewing

The changes are split into single-purpose commits, so it's easiest to review commit by commit. The __init__ re-exports and the member() rewrite are mechanical and large; the annotation fixes and bug fixes are small and hand-written.

Verification

The marker is shipped through package_data, so it's included in both the wheel and the sdist. With the package installed from this branch, a script using the SDK's documented imports passes both pyright and mypy --strict. examples/ has no new type errors.

Closes #171

@davidgrayston-paddle
davidgrayston-paddle marked this pull request as ready for review November 18, 2026 14:44
@davidgrayston-paddle
davidgrayston-paddle requested a review from a team as a code owner November 18, 2026 14:44
@davidgrayston-paddle
davidgrayston-paddle marked this pull request as draft November 18, 2026 15:10
@davidgrayston-paddle
davidgrayston-paddle marked this pull request as ready for review November 18, 2026 15:35
@davidgrayston-paddle davidgrayston-paddle self-assigned this Nov 18, 2026
@davidgrayston-paddle
davidgrayston-paddle marked this pull request as draft November 18, 2026 15:37
Request became a Union alias, so subclassing it raised TypeError. Keep
Request as a protocol with headers, extend it for each body attribute, and
type Verifier.verify and NotificationEvent.from_request with the new
WebhookRequest union of those.
Point mypy at this checkout and keep its cache in a temp directory.
The per-module directive turned mypy's assignment check off for every enum
module. member() returns its value typed as Any, so the annotation type-checks
with every checker, and members are still typed as the enum.
Any request object with a body, content or data attribute works at runtime,
so type verify() and from_request() as Any rather than a precise protocol.
Request goes back to its previous definition.
verify() and from_request() now take Request | BodyRequest | ContentRequest |
DataRequest: the existing Request protocol, or any object with headers and a
body, content or data attribute. A None body attribute is skipped instead of
raising AttributeError.

This branch was successfully deployed

1 active deployment
dev — e42064c6 Deployed Nov 24, 2026 by davidgrayston-paddle via Package stability #527
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing py.typed marker

2 participants