Repository navigation
Support factory-boy decorators such as post_generation - #5836
Closed
dhruvparanjape wants to merge 2 commits into
Closed
dhruvparanjape wants to merge 2 commits into
dhruvparanjape wants to merge 2 commits into
Conversation
Contributor
|
This pull request has been imported. If you are a Meta employee, you can view this in D135590123. (Because this pull request was imported automatically, there will not be any future comments.) |
This comment has been minimized.
This comment has been minimized.
factory-boy never passes the factory to a function wrapped by `post_generation`, `lazy_attribute`, `lazy_attribute_sequence`, `container_attribute`, `sequence` or `iterator`. Pyrefly still types the first parameter as `Self` of the factory, so attribute access resolves to the factory's declarations and a model annotation on that parameter is rejected (pyrefly-org#5733). These tests record that behaviour with `bug` markers. The test stubs gain the decorators and the declaration classes they return, matching what pyrefly infers from the untyped factory-boy source. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
factory-boy calls a function wrapped by one of its declaration decorators without the factory: `post_generation` receives the built object, the attribute declarations receive a `Resolver`, `sequence` receives a counter and `iterator` receives nothing. Typing the first parameter as `Self` of the factory made `self.<field>` resolve to the factory's declarations and rejected a model annotation (pyrefly-org#5733). A function under one of these decorators in a factory body now has no implicit receiver, and its unannotated parameters take the types factory-boy passes. `post_generation` gets the model from the nearest `Meta` that sets one. `Resolver` parameters are `Any`, because a `Resolver` exposes the factory's declarations, `Params` and call-time overrides, which the model does not describe. The types go through the existing decorator parameter hints, so explicit annotations still win, and marking the function as having no receiver skips the check that a self annotation is a superclass of the enclosing class. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
dhruvparanjape
force-pushed
the
factory-boy-decorator-receivers
branch
from
December 3, 2026 13:22
d8814e0 to
b08c5f1
Compare
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
kinto0
approved these changes
Dec 3, 2026
kinto0
left a comment
Contributor
There was a problem hiding this comment.
thanks for the fix! looks great to me. I'll fix up a few things after import:
- new test where a subclass factory overrides
modelwith an unrelated class, with the hook defined on the base factory - move MRO walk into named helper for documentation
connernilsen
approved these changes
Dec 3, 2026
connernilsen
left a comment
Contributor
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
meta-codesync Bot
pushed a commit
that referenced
this pull request
Dec 3, 2026
Summary: Stack context: follow-ups from the review of D135590123 (#5836, factory_boy declaration decorators). Plan step: follow-up 1, model lookup. - What: expand the comment on the MRO walk that finds the model for a `factory.post_generation` parameter. - Why: the old comment did not say why the walk checks the `Meta` of each class. A subclass `Meta` that sets only other options, such as `django_get_or_create`, must fall through to the base `model`. The walk has one call site, so a comment is enough; a helper would add a concept with no reuse. ___ landed-with-radar-review Differential Revision: D135982566 fbshipit-source-id: 08334d25d1f69eb2e6494e4ade0f4586ded5cde0
meta-codesync Bot
pushed a commit
that referenced
this pull request
Dec 3, 2026
…nments Summary: Stack context: follow-ups from the review of D135590123 (#5836, factory_boy declaration decorators). Plan step: follow-ups 2 and 3, tests for edge cases of the hook receiver. - What: add two tests for the first parameter of a `factory.post_generation` hook. - A subclass factory that overrides `model`: the hook keeps the model of the factory that defines it. At runtime the hook receives the subclass model. This is a known approximation, because pyrefly checks the hook body once. - `self.<attr> = ...` in the hook (`bug`): binding still treats the first parameter as the receiver, so the assignment defines an instance-only attribute on the factory. Binding runs before decorators are resolved, so it cannot see that `post_generation` drops the receiver. - Why: D135590123 types the receiver at solve time only, by setting `is_staticmethod`. These tests record the behavior where that is not the full picture. ___ landed-with-radar-review Differential Revision: D135982567 fbshipit-source-id: e6e1150872b0a4a117e15771d5db2c41dcbef3ee
meta-codesync Bot
pushed a commit
that referenced
this pull request
Dec 3, 2026
… not receive the model Summary: Stack context: follow-ups from the review of D135590123 (#5836, factory_boy declaration decorators). Plan step: follow-up 4, docs. - What: add one sentence to the factory_boy "Declaration decorators" docs. It says to annotate the first parameter of a hook when the hook does not receive the model at runtime. - Why: pyrefly types the first parameter as the model. With the stub strategy or a custom `_create` / `_build`, the hook receives another object. D135590123 accepts an explicit annotation on that parameter, but the docs do not say so. ___ landed-with-radar-review Differential Revision: D135982569 fbshipit-source-id: 866a4edc39a7416b8ebf2aa6670b3c78ec7f540f
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #5733
Note
"No receiver" reuses the staticmethod flag. As a result the coverage report now counts the first parameter of these functions as a typable slot.
Test Plan
added tests and verified with a sample factory-boy project.