Skip to content

[Doc] Document Composite.keys() nested walk order - #4477

Open
YeonwooSung wants to merge 2 commits into
pytorch:mainfrom
YeonwooSung:doc/2482-composite-spec-key-order
Open

YeonwooSung wants to merge 2 commits into
pytorch:mainfrom
YeonwooSung:doc/2482-composite-spec-key-order

Conversation

@YeonwooSung

Copy link
Copy Markdown
Contributor

Description

Documents the nested walk order of :meth:~torchrl.data.Composite.keys`` as a public contract.

keys(include_nested=True, leaves_only=False) follows the same depth-first walk as :meth:tensordict.TensorDict.keys: each nested `:class:`~torchrl.data.Composite is yielded after its children (child before parent), and siblings keep insertion order. The previous docstring listed ["next", ("next", "obs")] as the set of keys, which read as parent-before-child; the runtime order is [("next", "obs"), "next"]. That order is now stated explicitly, with a copy-paste example. :class:~torchrl.data.StackedComposite`` keys() gets the same note.

The walk is not changed. Empty nested specs can be dropped in one pass over list(spec.keys(True, False)) because children are visited first.

Motivation and Context

close #2482

The issue asked whether CompositeSpec.keys(True, False) is deepest-first so empty nested specs can be deleted in a single scan. It is, matching TensorDict. This PR records that guarantee instead of adding prune_empty() or reversing the walk.

  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

  • Documentation (update in the documentation)

Checklist

  • I have read the CONTRIBUTION guide (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.

@pytorch-bot

pytorch-bot Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4477

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

⚠️ 16 Awaiting Approval

As of commit 8d278a2 with merge base 84d4602 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@github-actions github-actions Bot added the Documentation Improvements or additions to documentation label Sep 22, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 22, 2026

This branch has not been deployed

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Assert CompositeSpec order guarantees

1 participant