Skip to content

Support factory-boy decorators such as post_generation - #5836

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

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.

This branch has not been deployed

No deployments
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