Repository navigation
Support factory-boy decorators such as post_generation - #5836
Open
dhruvparanjape wants to merge 2 commits into
Open
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.
This branch has not been deployed
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.