Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions pyrefly/lib/alt/class/factory_boy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ const CREATE: Name = Name::new_static("create");
const BUILD: Name = Name::new_static("build");
const CREATE_BATCH: Name = Name::new_static("create_batch");
const BUILD_BATCH: Name = Name::new_static("build_batch");
const NEW: Name = Name::new_static("__new__");

impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
/// Synthesize `create`, `build`, `create_batch`, and `build_batch` on
/// Synthesize `create`, `build`, `create_batch`, `new` and `build_batch` on
/// factory-boy `DjangoModelFactory` subclasses, returning the model type
/// from `class Meta: model = X`.
pub fn get_factory_boy_synthesized_fields(
Expand All @@ -52,7 +53,20 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
);
fields.insert(
BUILD,
self.factory_classmethod(cls, &BUILD, vec![], model_type),
self.factory_classmethod(cls, &BUILD, vec![], model_type.clone()),
);
fields.insert(
NEW,
self.factory_classmethod(
cls,
&NEW,
vec![Param::PosOnly(
None,
self.heap.mk_any_implicit(),
Required::Required,
)],
model_type,
),
);
let size_param = Param::Pos(
Name::new_static("size"),
Expand All @@ -72,6 +86,7 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
BUILD_BATCH,
self.factory_classmethod(cls, &BUILD_BATCH, vec![size_param], list_model_type),
);

Some(ClassSynthesizedFields::new(fields))
}

Expand Down
38 changes: 38 additions & 0 deletions pyrefly/lib/test/django/factory_boy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,41 @@ class IncompleteFactory(DjangoModelFactory):
obj = IncompleteFactory.create()
"#,
);

factory_boy_testcase!(
test_bare_call_returns_model,
r#"
from typing import assert_type

from django.db import models
from factory.django import DjangoModelFactory

class User(models.Model):
username = models.CharField(max_length=150)

class UserFactory(DjangoModelFactory):
class Meta:
model = User

user = UserFactory()
assert_type(user, User)
"#,
);

factory_boy_testcase!(
test_bare_call_attribute_access,
r#"
from django.db import models
from factory.django import DjangoModelFactory

class User(models.Model):
username = models.CharField(max_length=150)

class UserFactory(DjangoModelFactory):
class Meta:
model = User

user = UserFactory()
name = user.username
"#,
);
Loading