Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix infinite recursion in BaseModel equality checks#11581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
YassinNouh21 wants to merge2 commits intopydantic:main
base:main
Choose a base branch
Loading
fromYassinNouh21:fix/prevent-infinite-recursion-in

Conversation

YassinNouh21
Copy link
Contributor

Change Summary

This PR fixes an issue with infinite recursion when comparing BaseModel instances that contain recursive references (instances that reference themselves either directly or indirectly). It implements a solution using ContextVar to track object pairs being compared during equality checks, which prevents the comparison from getting stuck in an infinite loop.

The changes include:

  • Adding a module-level_eq_recursion_tracker ContextVar to track objects being compared
  • Implementing recursive comparison detection in the__eq__ method
  • Adding comprehensive test cases for both direct and complex recursive models

Related issue number

fix#10630

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review,please review

gersmann reacted with heart emoji
@github-actionsgithub-actionsbot added the relnotes-fixUsed for bugfixes. labelMar 19, 2025
@codspeed-hqCodSpeed HQ
Copy link

CodSpeed Performance Report

Merging#11581 willnot alter performance

ComparingYassinNouh21:fix/prevent-infinite-recursion-in (901b43c) withmain (a2846da)

Summary

✅ 46 untouched benchmarks

@github-actionsGitHub Actions
Copy link
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic
  main.py1180-1188
Project Total 

This report was generated bypython-coverage-comment-action

@Viicos
Copy link
Member

Viicos commentedMar 19, 2025
edited
Loading

Thanks for the contribution, I still need to think on this one. As mentioned in the CPythonissue, we might want to consider the recursion error as expected.

I also won't be surprised if the approach here doesn't work as expected for more complex recursive cases.

@YassinNouh21
Copy link
ContributorAuthor

@Viicos Thanks for reviewing the PR. While Python's core behavior treats the RecursionError as expected for circular references, I believe Pydantic should offer more robust handling as a high-level library:

  1. The solution uses ContextVar to efficiently track object pairs during comparison, breaking recursion cycles.
  2. It returns True when cycles are detected, consistent with Golang's approach

I've tested this with various recursive scenarios (self-references, mutual references, and deep cycles) and found it works reliably.

@MarkusSintonen
Copy link
Contributor

MarkusSintonen commentedMar 23, 2025
edited
Loading

Can you add CodSpeed benchmarks in a separate PR (eq checks for different nestings)? To then see in this PR that this wont degrade eq checking perf. This is now adding a bunch of set allocs and lookups to every eq operation. It might not be worth fixing the issue if the perf takes a hit for everyone. Considering how corner case it is which also exists in Python itself.

@Viicos
Copy link
Member

Discussed today with the team, indeed would be great to assess the potential performance hit here. The linked issue also doesn't have much demand, and it feels like using context vars for this is a bit too much.

@YassinNouh21
Copy link
ContributorAuthor

@Viicos I understand the concern about performance. Can I add CodSpeed benchmarks to measure the impact—would that help in evaluating this further? Also, would making this behavior configurable be a good compromise? Let me know your thoughts!

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees

@ViicosViicos

Labels
relnotes-fixUsed for bugfixes.
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

(🐞)RecursionError in== when object has a reference to itself
3 participants
@YassinNouh21@Viicos@MarkusSintonen

[8]ページ先頭

©2009-2025 Movatter.jp