Skip to content

Support factory-boy decorators such as post_generation - #5836

Closed
dhruvparanjape wants to merge 2 commits into
pyrefly-org:mainfrom
dhruvparanjape:factory-boy-decorator-receivers
Closed

dhruvparanjape wants to merge 2 commits into
pyrefly-org:mainfrom
dhruvparanjape:factory-boy-decorator-receivers

Conversation

@dhruvparanjape

Copy link
Copy Markdown
Contributor

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.

  • I am a human, and I fully understand the code I am submitting.

Test Plan

added tests and verified with a sample factory-boy project.

@meta-codesync

meta-codesync Bot commented Dec 2, 2026

Copy link
Copy Markdown
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.)

@github-actions

This comment has been minimized.

dhruvparanjape and others added 2 commits December 3, 2026 14:22
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
dhruvparanjape force-pushed the factory-boy-decorator-receivers branch from d8814e0 to b08c5f1 Compare December 3, 2026 13:22
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@kinto0 kinto0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the fix! looks great to me. I'll fix up a few things after import:

  • new test where a subclass factory overrides model with an unrelated class, with the hook defined on the base factory
  • move MRO walk into named helper for documentation

@connernilsen connernilsen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review automatically exported from Phabricator review in Meta.

@meta-codesync meta-codesync Bot closed this in 4342889 Dec 3, 2026
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
@meta-codesync meta-codesync Bot added the Merged label Dec 3, 2026
@meta-codesync

meta-codesync Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

@kinto0 merged this pull request in 4342889.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

factory_boy: @factory.post_generation first parameter is typed as the factory, not the produced model

3 participants