Skip to content

Conversation

@abrookins
Copy link
Collaborator

@abrookins abrookins commented Jan 15, 2026

Summary

Fixes #763 - Python 3.14 compatibility for JsonModel.schema_for_fields().

The Problem

In Python 3.14, iterating over cls.__dict__.items() directly can raise:

RuntimeError: dictionary changed size during iteration

This occurs during class construction when Pydantic is still modifying the class namespace.

The Solution

Instead of copying the entire __dict__ before iteration (which wastes memory), this PR iterates over cls.__annotations__ keys and looks up each value in __dict__ individually:

# Before (original - crashes on 3.14):
for name, field in cls.__dict__.items():
    if isinstance(field, FieldInfo):
        ...

# After (this PR):
for name in cls.__annotations__:
    field = cls.__dict__.get(name)
    if isinstance(field, FieldInfo):
        ...

This approach:

  • ✅ Avoids iterating over a mutating dict
  • ✅ No memory overhead from copying __dict__
  • ✅ More targeted - only checks annotated fields (which is what we care about)
  • ✅ Works on all Python versions

Tests Added

Comprehensive test coverage for schema_for_fields() edge cases:

  • Indexed fields (text, numeric, tag, sortable, fulltext)
  • Optional fields with defaults
  • Inherited fields from parent models
  • Embedded models
  • List[str] fields
  • FieldInfo annotation verification
  • Custom primary keys
  • Case-sensitive fields

In Python 3.14, iterating over cls.__dict__.items() directly can raise
RuntimeError: dictionary changed size during iteration. This fix creates
a copy of the dictionary before iteration to prevent this error.

Fixes #763
@jit-ci
Copy link

jit-ci bot commented Jan 15, 2026

Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset.

In case there are security findings, they will be communicated to you as a comment inside the PR.

Hope you’ll enjoy using Jit.

Questions? Comments? Want to learn more? Get in touch with us.

Instead of copying cls.__dict__ to avoid Python 3.14 iteration errors,
iterate over cls.__annotations__ keys and look up values individually.
This avoids the memory overhead of copying the entire __dict__.

Also adds comprehensive tests for schema_for_fields edge cases.
@jit-ci
Copy link

jit-ci bot commented Jan 15, 2026

❌ Jit Scanner failed - Our team is investigating

Jit Scanner failed - Our team has been notified and is working to resolve the issue. Please contact support if you have any questions.


💡 Need to bypass this check? Comment @sera bypass to override.

@abrookins abrookins merged commit 9f11bc7 into main Jan 15, 2026
9 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python 3.14: RuntimeError: dictionary changed size during iteration during JsonModel class creation (schema_for_fields iterates cls.__dict__)

2 participants