René's URL Explorer Experiment


Title: performance issue / optimization: response parsing is unnecessarily slow · Issue #201 · square/square-python-sdk · GitHub

Open Graph Title: performance issue / optimization: response parsing is unnecessarily slow · Issue #201 · square/square-python-sdk

X Title: performance issue / optimization: response parsing is unnecessarily slow · Issue #201 · square/square-python-sdk

Description: Summary Response parsing is unnecessarily slow and CPU bound, due to repeated calls to get_type_hints. client.catalog.search(limit=1000, include_related_objects=True) returns in under 3s, then the SDK spends ~7s of CPU building the respo...

Open Graph Description: Summary Response parsing is unnecessarily slow and CPU bound, due to repeated calls to get_type_hints. client.catalog.search(limit=1000, include_related_objects=True) returns in under 3s, then the ...

X Description: Summary Response parsing is unnecessarily slow and CPU bound, due to repeated calls to get_type_hints. client.catalog.search(limit=1000, include_related_objects=True) returns in under 3s, then the ...

Opengraph URL: https://github.com/square/square-python-sdk/issues/201

X: @github

direct link

Domain: github.com

IPv4 address: 140.82.114.4


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"performance issue / optimization: response parsing is unnecessarily slow","articleBody":"## Summary\n\nResponse parsing is unnecessarily slow and CPU bound, due to repeated calls to `get_type_hints`.\n\n`client.catalog.search(limit=1000, include_related_objects=True)` returns in under 3s, then the SDK spends ~7s of CPU building the response models. For a per-SKU catalog of a few thousand objects, parsing dominates the call.\n\nProfiling 200 objects shows 13,000 calls to `typing_extensions.get_type_hints`, about 65 per catalog object, and nearly all of the wall time under them. The chain is:\n\n1. `CatalogObject.item_data` is `Optional[CatalogItem]`, an undiscriminated union, so `construct_type` routes it through [`_convert_undiscriminated_union_type`](https://github.com/square/square-python-sdk/blob/42a14b077c208817cc8bec118e38066ac01a4d07/src/square/core/unchecked_base_model.py#L153), which trial-parses each member with [`parse_obj_as`](https://github.com/square/square-python-sdk/blob/42a14b077c208817cc8bec118e38066ac01a4d07/src/square/core/unchecked_base_model.py#L174).\n2. [`parse_obj_as`](https://github.com/square/square-python-sdk/blob/42a14b077c208817cc8bec118e38066ac01a4d07/src/square/core/pydantic_utilities.py#L256) de-aliases the input through `convert_and_respect_annotation_metadata`, which recurses the annotation tree.\n3. Every level of that recursion calls [`get_type_hints` in `_convert_mapping`](https://github.com/square/square-python-sdk/blob/42a14b077c208817cc8bec118e38066ac01a4d07/src/square/core/serialization.py#L164). The result depends only on the type, but it is recomputed for every object. The generated models use `from __future__ import annotations`, so each call compiles and evaluates every forward reference again.\n\nMemoizing `get_type_hints` by type brings the same page from 6.5s to 0.7s with identical output.\n\n## Reproduction\n\nNo network needed. Builds a 1,000-item search response and parses it the way the client does.\n\n```python\nimport functools\nimport time\n\nimport typing_extensions\nfrom square.core import serialization\nfrom square.core import unchecked_base_model as ubm\nfrom square.types.search_catalog_objects_response import SearchCatalogObjectsResponse\n\n\ndef item(i):\n    variations = [\n        {\n            \"type\": \"ITEM_VARIATION\",\n            \"id\": f\"V{i}_{j}\",\n            \"item_variation_data\": {\n                \"item_id\": f\"I{i}\",\n                \"name\": f\"Size {j}\",\n                \"pricing_type\": \"FIXED_PRICING\",\n                \"price_money\": {\"amount\": 500, \"currency\": \"USD\"},\n            },\n        }\n        for j in range(3)\n    ]\n    return {\"type\": \"ITEM\", \"id\": f\"I{i}\", \"item_data\": {\"name\": f\"Item {i}\", \"variations\": variations}}\n\n\npage = {\n    \"objects\": [item(i) for i in range(1000)],\n    \"related_objects\": [\n        {\"type\": \"IMAGE\", \"id\": f\"IMG{i}\", \"image_data\": {\"url\": \"https://example.com/1.png\"}} for i in range(1000)\n    ],\n}\n\n\ndef parse():\n    start = time.perf_counter()\n    ubm.construct_type(type_=SearchCatalogObjectsResponse, object_=page)\n    return time.perf_counter() - start\n\n\nprint(f\"unpatched:               {parse():.1f}s\")\n\ncached = functools.cache(lambda t, extras: typing_extensions.get_type_hints(t, include_extras=extras))\n\n\nclass Shim:\n    def __getattr__(self, name):\n        return getattr(typing_extensions, name)\n\n    @staticmethod\n    def get_type_hints(obj, globalns=None, localns=None, include_extras=False):\n        return cached(obj, include_extras)\n\n\nserialization.typing_extensions = Shim()\nprint(f\"get_type_hints memoized: {parse():.1f}s\")\n```\n\nOutput on my machine:\n\n```\nunpatched:               6.5s\nget_type_hints memoized: 0.7s\n```\n\n## Suggested fix\n\nCache `get_type_hints` per type inside `serialization.py`, for example with `functools.lru_cache` around the three call sites. A type's hints do not change at runtime, so the cache is safe and unbounded by object count. Alternatively, `_convert_undiscriminated_union_type` could construct an `Optional[BaseModel]` member directly instead of validating it through `parse_obj_as`.\n\n## Environment\n\n* **SDK:** squareup 46.0.0.20260916\n* **Python:** 3.14\n\n## Related\n\n[`_convert_union_type`](https://github.com/square/square-python-sdk/blob/42a14b077c208817cc8bec118e38066ac01a4d07/src/square/core/unchecked_base_model.py#L236) reads `inner_type.__fields__`, which pydantic 2 deprecates with a `PydanticDeprecatedSince20` warning on every discriminated object. Under pydantic 3 that attribute goes away, the `except Exception` swallows the error, and every `CatalogObject` would fall into the slow undiscriminated path above.\n","author":{"url":"https://github.com/mik3y","@type":"Person","name":"mik3y"},"datePublished":"2026-10-31T20:55:53Z","url":"https://github.com/square/square-python-sdk/issues/201"}

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:56fbe4b2-f980-301b-13e5-c200ce50c49e
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id80CE:3C538A:F930D5:1554AF2:6B055A3D
html-safe-nonce379ea1454d4b2ecdc6d7b8bc05db552bfd9a9d940d16e72edacc3a22494edd75
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MENFOjNDNTM4QTpGOTMwRDU6MTU1NEFGMjo2QjA1NUEzRCIsInZpc2l0b3JfaWQiOiI3MDY3NjE2MTQxOTA0MjcxOTM0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmacff79ef51760320a7e87f48a53e0cced504006aace4729a1a89df779d548fbd73
hovercard-subject-tagissue:6139667801
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/square/square-python-sdk/201/issue_layout
twitter:imagehttps://opengraph.githubassets.com/4eb76fd42a91551cf6467c799860aadd91d95307751fdcd71672bebdfd34468d/square/square-python-sdk/issues/201
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/4eb76fd42a91551cf6467c799860aadd91d95307751fdcd71672bebdfd34468d/square/square-python-sdk/issues/201
og:image:altSummary Response parsing is unnecessarily slow and CPU bound, due to repeated calls to get_type_hints. client.catalog.search(limit=1000, include_related_objects=True) returns in under 3s, then the ...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamemik3y
hostnamegithub.com
expected-hostnamegithub.com
Nonea79a02b7e7546ea306864e922ed2103ef4c04fac63168e11c2b2d4d816793080
turbo-cache-controlno-preview
go-importgithub.com/square/square-python-sdk git https://github.com/square/square-python-sdk.git
octolytics-dimension-user_id82592
octolytics-dimension-user_loginsquare
octolytics-dimension-repository_id199728156
octolytics-dimension-repository_nwosquare/square-python-sdk
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id199728156
octolytics-dimension-repository_network_root_nwosquare/square-python-sdk
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release10dff83edfd4f3617a7792a152d44991587b434c
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/square/square-python-sdk/issues/201#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fsquare%2Fsquare-python-sdk%2Fissues%2F201
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fsquare%2Fsquare-python-sdk%2Fissues%2F201
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=square%2Fsquare-python-sdk
Reloadhttps://github.com/square/square-python-sdk/issues/201
Reloadhttps://github.com/square/square-python-sdk/issues/201
Reloadhttps://github.com/square/square-python-sdk/issues/201
squarehttps://github.com/square
square-python-sdkhttps://github.com/square/square-python-sdk
Codehttps://github.com/square/square-python-sdk
Issues1 (1)https://github.com/square/square-python-sdk/issues
Pull requests2 (2)https://github.com/square/square-python-sdk/pulls
Actionshttps://github.com/square/square-python-sdk/actions
Projectshttps://github.com/square/square-python-sdk/projects
Security and qualityhttps://github.com/square/square-python-sdk/security
Insightshttps://github.com/square/square-python-sdk/insights
https://github.com/square/square-python-sdk
New issuehttps://github.com/login?return_to=https://github.com/square/square-python-sdk/issues/201
New issuehttps://github.com/login?return_to=https://github.com/square/square-python-sdk/issues/201
performance issue / optimization: response parsing is unnecessarily slowhttps://github.com/square/square-python-sdk/issues/201#top
https://github.com/mik3y
mik3yhttps://github.com/mik3y
on Oct 31, 2026https://github.com/square/square-python-sdk/issues/201#issue-6139667801
_convert_undiscriminated_union_typehttps://github.com/square/square-python-sdk/blob/42a14b077c208817cc8bec118e38066ac01a4d07/src/square/core/unchecked_base_model.py#L153
parse_obj_ashttps://github.com/square/square-python-sdk/blob/42a14b077c208817cc8bec118e38066ac01a4d07/src/square/core/unchecked_base_model.py#L174
parse_obj_ashttps://github.com/square/square-python-sdk/blob/42a14b077c208817cc8bec118e38066ac01a4d07/src/square/core/pydantic_utilities.py#L256
get_type_hints in _convert_mappinghttps://github.com/square/square-python-sdk/blob/42a14b077c208817cc8bec118e38066ac01a4d07/src/square/core/serialization.py#L164
_convert_union_typehttps://github.com/square/square-python-sdk/blob/42a14b077c208817cc8bec118e38066ac01a4d07/src/square/core/unchecked_base_model.py#L236
Sign up for freehttps://github.com/signup?return_to=https://github.com/square/square-python-sdk/issues/201
Sign in to commenthttps://github.com/login?return_to=https://github.com/square/square-python-sdk/issues/201
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Response headers:
Servergithub.com
Content-Typetext/html; charset=utf-8
Cache-Controlmax-age=0, private, must-revalidate

Viewport: width=device-width


URLs of crawlers that visited me.