Skip to content

Commit fb552b7

Browse files
migeed-zmeta-codesync[bot]
authored andcommitted
Add model metadata to class metadata
Summary: At minimum, we should know if our class is a django model, so we can add special field support for it. Reviewed By: rchen152 Differential Revision: D83796198 fbshipit-source-id: e5e3807dd4a8fdeaae158d81c8c46ffc7fa6e990
1 parent d553610 commit fb552b7

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

crates/pyrefly_python/src/module_name.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ impl ModuleName {
160160
Self::from_str("django.db.models.enums")
161161
}
162162

163+
pub fn django_models() -> Self {
164+
Self::from_str("django.db.models.base")
165+
}
166+
163167
/// The "unknown" module name, which corresponds to `__unknown__`.
164168
/// Used for files directly opened or passed on the command line which aren't on the search path.
165169
pub fn unknown() -> Self {

pyrefly/lib/alt/class/class_metadata.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,16 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
178178
}
179179
}
180180

181+
// TODO Zeina: This pattern is repeated a lot in this file. See if we can refactor it (BE).
182+
let has_django_model = bases_with_metadata.iter().any(|(base_class_object, _)| {
183+
base_class_object.has_toplevel_qname(ModuleName::django_models().as_str(), "Model")
184+
});
185+
186+
let is_django_model = has_django_model
187+
|| bases_with_metadata
188+
.iter()
189+
.any(|(_, metadata)| metadata.is_django_model());
190+
181191
// Compute various pieces of special metadata.
182192
let has_base_any = contains_base_class_any
183193
|| bases_with_metadata
@@ -318,6 +328,7 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
318328
total_ordering_metadata,
319329
dataclass_transform_metadata,
320330
pydantic_model_kind,
331+
is_django_model,
321332
)
322333
}
323334

pyrefly/lib/alt/types/class_metadata.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub struct ClassMetadata {
5858
/// that were passed to the `dataclass_transform` call.
5959
dataclass_transform_metadata: Option<DataclassTransformKeywords>,
6060
pydantic_model_kind: Option<PydanticModelKind>,
61+
is_django_model: bool,
6162
}
6263

6364
impl VisitMut<Type> for ClassMetadata {
@@ -91,6 +92,7 @@ impl ClassMetadata {
9192
total_ordering_metadata: Option<TotalOrderingMetadata>,
9293
dataclass_transform_metadata: Option<DataclassTransformKeywords>,
9394
pydantic_model_kind: Option<PydanticModelKind>,
95+
is_django_model: bool,
9496
) -> ClassMetadata {
9597
ClassMetadata {
9698
metaclass: Metaclass(metaclass),
@@ -109,6 +111,7 @@ impl ClassMetadata {
109111
total_ordering_metadata,
110112
dataclass_transform_metadata,
111113
pydantic_model_kind,
114+
is_django_model,
112115
}
113116
}
114117

@@ -130,6 +133,7 @@ impl ClassMetadata {
130133
total_ordering_metadata: None,
131134
dataclass_transform_metadata: None,
132135
pydantic_model_kind: None,
136+
is_django_model: false,
133137
}
134138
}
135139

@@ -157,6 +161,10 @@ impl ClassMetadata {
157161
self.pydantic_model_kind.is_some()
158162
}
159163

164+
pub fn is_django_model(&self) -> bool {
165+
self.is_django_model
166+
}
167+
160168
pub fn pydantic_model_kind(&self) -> Option<PydanticModelKind> {
161169
self.pydantic_model_kind.clone()
162170
}

0 commit comments

Comments
 (0)