<!--
title: Data structure correctness
domain: app-frameworks
topic: Algorithms
language: Markdown
source: pydantic/pydantic
updated: 2025-02-07
url: https://awesomereviewers.com/reviewers/pydantic-data-structure-correctness/
-->

Ensure data structures are accurately represented with their proper constraints and valid implementations, particularly for recursive structures. When documenting or implementing collections:

1. Explicitly state data structure constraints
   - For set-like structures, clearly document hashability requirements
   - For tree-like structures, describe node relationships precisely

2. Validate that recursive type examples actually terminate
   - Include escape conditions in recursive type definitions
   - Ensure examples represent structures that can be instantiated

```python
# Incorrect recursive type definition (infinite recursion)
type B = list[C]
type C = B  # No termination condition!

# Correct recursive type definition with termination
type A = list[A] | None  # Terminates with None
```

Properly defined data structures not only prevent runtime errors but also enable more efficient algorithm implementations.
