feat(python): add __repr__ and to_dict to all SDK types#38
Open
johnpmitsch wants to merge 1 commit into
Open
Conversation
Python classes previously printed as `<builtins.Endpoint object at 0x...>` and had no built-in way to convert to a plain dict for JSON serialization or comparison. Every PyO3-exposed type now implements: - `__repr__()` delegating to Rust `Debug` for readable REPL/print output - `to_dict()` returning a native Python dict via `pythonize` from the type's `Serialize` impl, so `json.dumps(obj.to_dict())` round-trips Implementation: - New `python_repr_dict!` macro (crates/core/src/python_macros.rs) emits both methods with one call per type; applied to ~160 pyclass types - Workspace pyo3 gains the `multiple-pymethods` feature so the macro can attach a second `#[pymethods]` block alongside existing ones - Per-language wrapper types (streams destinations, webhook templates, PyStream, PyListStreamsResponse) get hand-rolled equivalents because they hold non-Serialize fields Redaction policy preserved: `SdkFullConfig.api_key`, `EndpointToken.token`, and `EndpointJwt.public_key` show `"[redacted]"` in both repr() and to_dict(). `SdkFullConfig` gets a manual `Debug` impl that mirrors the existing pattern on internal `SdkConfig`, closing a latent leak where the derived `Debug` would print the raw API key. Docs: CLAUDE.md gains a "Python __repr__ and to_dict" subsection documenting the macro and the redaction workflow; python/README.md mentions both methods in the Language conventions section. Closes DX-5563
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
__repr__andto_dict()to every PyO3-exposed type in the Python SDK so they print readably (Endpoint { id: \"abc\", ... }instead of<builtins.Endpoint object at 0x...>) and convert cleanly to a native Pythondictfor JSON serialization, comparison, and logging.New
python_repr_dict!macro incrates/core/src/python_macros.rsemits both methods. Each pyclass module gets apython_repr_implsblock listing one call per type (~160 types).Workspace
pyo3gainsmultiple-pymethodsso the macro can attach its own#[pymethods]impl beside any existing one.__repr__delegates to RustDebug;to_dictusespythonizeover the type'sSerializeimpl.Sensitive fields stay redacted in both
repr()andto_dict():SdkFullConfig.api_key(closes a latent leak — the derivedDebugpreviously printed the raw key)EndpointToken.tokenEndpointJwt.public_keyPer-type hand-rolled
#[pymethods]blocks mirror the manualDebugredaction by overwriting the sensitive key with\"[redacted]\"afterpythonize.Per-language wrapper types (
StreamWebhookDestination/StreamS3Destination/StreamAzureDestination/StreamPostgresDestination/StreamKafkaDestination, the 8 webhook template wrappers,PyStream,PyListStreamsResponse) all expose the same surface.Docs: CLAUDE.md gains a Python
__repr__andto_dictsubsection that documents the macro and the redaction workflow for credential-bearing types;python/README.mdmentions both methods in Language conventions.Closes DX-5563
Test plan
cargo check— all crates compile (python, node, ruby features)just lint—cargo clippy --workspace --lib --tests -- -D warningscleancargo test -p quicknode-sdk --lib— 204 tests pass, including a newsdk_full_config_debug_redacts_api_keyregression testjust python-build— wheel + stubs regenerate;def __repr__anddef to_dicteach appear 182 times inpython/quicknode_sdk/_core/__init__.pyijust node-buildandjust ruby-build— unaffected (macro is gated behindfeature = \"python\")repr(SdkFullConfig(api_key=\"x\"))contains\"[redacted]\", not\"x\"SdkFullConfig(...).to_dict()[\"api_key\"] == \"[redacted]\"json.dumpsStreamWebhookDestination) show inner attributes in repr and to_dictpython/examples/admin.pyend-to-end against a real API key to verify the newrepr/to_dictdemo block prints as expected