Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix circular schema generation, remove None checking hack #621

merged 6 commits into from Jun 28, 2019


Copy link

@tiangolo tiangolo commented Jun 26, 2019

Change Summary

This is a continuation/refactor of #613 by @wongpat. It should solve #601

The original PR to support circular references (by me) used a "hack" here:

definitions[model_name] = None

By setting a definition to None it was only marking it to be updated later:

But it has a bug, it makes it remove other references to the same sub-model when declared in the same parent model, e.g.

class Dep(BaseModel):
    number: int

class Model(BaseModel):
    dep: Dep
    dep_l: List[Dep]

Here, by having more than one field referencing Dep, the definition is created with dep and then it's removed with dep_l (setting it to None).

#613 fixes the original hack, updating it. Instead of marking a definition as declared in the future with None in the definition itself, it creates another key (improved hack) _nested, then it is removed from the definitions and used.

This PR re-implements the same idea from #613 , but being explicit about what's it's doing. It's more verbose (with an extra variable nested_models), but presumably less "hacky", by not re-using definitions for something it is not intended to be used for, as was done in my original implementation and in the fix at #613 .

As the changes are considerable, I ended up writing this new PR. But I don't know how to include the commit/attribution to @wongpat.

I think we can start by discussing the implementation. @samuelcolvin what do you think?

Related issue number



  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • HISTORY.rst has been updated
    • if this is the first change since a release, please add a new section
    • include the issue number or this pull request number #<number>
    • include your github username @<whomever>

@tiangolo tiangolo mentioned this pull request Jun 26, 2019
4 tasks
Copy link

@samuelcolvin samuelcolvin commented Jun 26, 2019

LGTM from a brief look. Please update history.

@wongpat is this okay with you?

@tiangolo tiangolo force-pushed the circular-schema-refactor branch from dee8b21 to 75e04e3 Compare Jun 26, 2019
Copy link

@codecov codecov bot commented Jun 26, 2019

Codecov Report

Merging #621 into master will not change coverage.
The diff coverage is 100%.

@@          Coverage Diff          @@
##           master   #621   +/-   ##
  Coverage     100%   100%           
  Files          15     15           
  Lines        2616   2628   +12     
  Branches      516    516           
+ Hits         2616   2628   +12

Copy link

@wongpat wongpat left a comment

@tiangolo Looks good to me a couple lines that could be a bit optimized. Definitely a better solution than using a magical key in an existing object.

I'd say if we need any more metadata than this as we process the schemas then it could be worth using a namedtuple

(Also means that the accompanying PR in FastAPI is unnecessary)

pydantic/ Outdated Show resolved Hide resolved
pydantic/ Outdated Show resolved Hide resolved
tiangolo and others added 3 commits Jun 27, 2019
Co-Authored-By: Patrick Wong <>
Co-Authored-By: Patrick Wong <>
Copy link
Member Author

@tiangolo tiangolo commented Jun 27, 2019

Thanks for the feedback @wongpat ! I applied your suggestions and replicated them in all the other places.

@samuelcolvin samuelcolvin merged commit 3cdbbae into pydantic:master Jun 28, 2019
9 checks passed
Copy link

@samuelcolvin samuelcolvin commented Jun 28, 2019

great, thank you.

Copy link
Member Author

@tiangolo tiangolo commented Jun 28, 2019

Thanks! 🚀

@tiangolo tiangolo deleted the circular-schema-refactor branch Jun 28, 2019
Copy link

@wongpat wongpat commented Jun 28, 2019

Thank you both!

Copy link

@jorgecarleitao jorgecarleitao commented Jul 5, 2019

Any chance to release this as a patch? Got hit by this, and seems a bug on the latest version (0.29 as of writing).

Copy link

@samuelcolvin samuelcolvin commented Jul 7, 2019

released, sorry for the delay.

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

Successfully merging this pull request may close these issues.

None yet

4 participants