Skip to content

feat: generate TypedDict types for input-side models#738

Open
vdusek wants to merge 2 commits intomasterfrom
feat/generate-typeddicts-for-input-models
Open

feat: generate TypedDict types for input-side models#738
vdusek wants to merge 2 commits intomasterfrom
feat/generate-typeddicts-for-input-models

Conversation

@vdusek
Copy link
Copy Markdown
Contributor

@vdusek vdusek commented Apr 17, 2026

Summary

Adds TypedDict counterparts for input-side models so users passing plain dicts to resource-client methods get full type-checker support without importing Pydantic models.

  • Generate TypedDicts alongside Pydantic models via a second datamodel-code-generator pass (--output-model-type typing.TypedDict) in poe generate-models.
  • Split generated content into _models_generated.py / _typeddicts_generated.py. Hand-written _models.py / _typeddicts.py now only hold shapes not exposed by the OpenAPI spec (e.g. RequestInput, RequestInputDict).
  • Extend scripts/postprocess_generated_models.py to trim the TypedDict file to input-relevant classes (plus transitive deps), rename them with a Dict suffix, and add @docs_group('Typed dicts').
  • Widen resource-client input parameters (actor, task, task_collection, request_queue) to accept TypedDict | PydanticModel unions.
  • Update .rules.md and manual_regenerate_models.yaml to reflect the new layout.

Closes #666.

@vdusek vdusek added adhoc Ad-hoc unplanned task added during the sprint. t-tooling Issues with this label are in the ownership of the tooling team. labels Apr 17, 2026
@vdusek vdusek self-assigned this Apr 17, 2026
@github-actions github-actions Bot added this to the 138th sprint - Tooling team milestone Apr 17, 2026
@github-actions github-actions Bot added the tested Temporary label used only programatically for some analytics. label Apr 17, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 17, 2026

Codecov Report

❌ Patch coverage is 40.35088% with 68 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.03%. Comparing base (6c3b84d) to head (aa1c2ab).

Files with missing lines Patch % Lines
src/apify_client/_typeddicts_generated.py 0.00% 49 Missing ⚠️
src/apify_client/_typeddicts.py 0.00% 19 Missing ⚠️

❌ Your patch check has failed because the patch coverage (40.35%) is below the target coverage (50.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #738      +/-   ##
==========================================
- Coverage   95.36%   94.03%   -1.33%     
==========================================
  Files          45       48       +3     
  Lines        5118     5201      +83     
==========================================
+ Hits         4881     4891      +10     
- Misses        237      310      +73     
Flag Coverage Δ
integration 94.03% <40.35%> (-1.33%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@vdusek vdusek force-pushed the feat/generate-typeddicts-for-input-models branch from 1eb5c1d to f63bb00 Compare April 20, 2026 12:48
Add TypedDict counterparts for generated Pydantic models so users passing
plain dicts to resource-client methods get full type-checker support without
importing Pydantic models. Closes #666.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@vdusek vdusek force-pushed the feat/generate-typeddicts-for-input-models branch from f63bb00 to 00f228a Compare April 20, 2026 12:58
@vdusek vdusek requested review from Pijukatel and janbuchar April 20, 2026 13:01
@vdusek
Copy link
Copy Markdown
Contributor Author

vdusek commented Apr 20, 2026

@Pijukatel @janbuchar Let me guys know what you think, this change also has some drawbacks:

  1. The postprocess script had to be extended significantly.
  2. Some models and TypedDicts still need to be maintained manually, so I split them into two modules (generated vs. hand-written). I am not sure about this structure.

@vdusek vdusek closed this Apr 20, 2026
@vdusek vdusek reopened this Apr 20, 2026
Comment thread tests/integration/test_request_queue.py Outdated
Comment thread docs/02_concepts/code/03_nested_sync.py
The URL of the request.
"""
method: NotRequired[Literal['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH']]
retry_count: NotRequired[int]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a case for accepting camelCase here instead - it is exactly what the API accepts. On the other hand, snake_case fits in better with the rest of the Python code and works better for the "I just don't want to look up what model I need to import" use case for TypedDicts. Did you consider this @vdusek?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep the public interface Python-idiomatic and stick with snake_case. I recently made some updates in this direction, moving the interface to snake_case, see #736.

I wouldn't have a bug problem in leaving it just as the API accepts it, but we would have to do it in all places to be consistent.

Copy link
Copy Markdown
Contributor

@janbuchar janbuchar Apr 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we shove everything into Pydantic, we should also be perfectly capable of supporting both, no? Like, camelCase and snake_case version of every model where this is applicable.

Comment thread src/apify_client/_types.py Outdated
@vdusek vdusek requested review from Pijukatel and janbuchar April 21, 2026 07:13
# Seed models for the TypedDict pruning. Every TypedDict in `_typeddicts.py` that is not
# transitively reachable from this set is removed. Keep in sync with the `dict | <Model>` unions
# on resource-client method signatures.
TYPEDDICT_SEEDS: frozenset[str] = frozenset(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming is more about the pruning algorithm than about the top-level intent. Why are these three selected?

Maybe some name like:
REQUIRED_DICTS or USED_DICTS ...

and some hint in the comment about why just these 3 are selected

return refs - exclude


def _compute_transitive_closure(deps: dict[str, set[str]], seeds: set[str]) -> set[str]:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usage of closure in naming can be misleading, as it has its own meaning in Python, different from set theory, and while reading Python code, the reader will probably have the Python meaning in mind first.

Maybe better name based on what is the purpose of it:
_compute_reachable_symbols or _compute_transitive_dependencies or ...

and same for closure: set[str] = set() -> reachable_symbols or transitive_dependencies or...

Comment thread tests/unit/test_utils.py
def test_webhook_representation_list_from_dicts() -> None:
"""Test that from_webhooks accepts plain dicts with the minimal ad-hoc webhook shape."""
result = WebhookRepresentationList.from_webhooks(
# The runtime only needs the keys consumed by WebhookRepresentation (event_types, request_url,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it does not require so much, is the typing correct?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

adhoc Ad-hoc unplanned task added during the sprint. t-tooling Issues with this label are in the ownership of the tooling team. tested Temporary label used only programatically for some analytics.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generate TypedDict types for input-side models

4 participants