Skip to content

Commit 6d491ef

Browse files
rchen152facebook-github-bot
authored andcommitted
Add FunctionMetadata to Overload
Summary: We know that an overload is constructed from a series of functions, so it has function metadata. Store this directly on the overload. Right now, we just grab the metadata of the first signature, but once python/typing#1839 is accepted, we'll need to check the metadata for consistency between signatures and grab some of it from the overload implementation. Reviewed By: stroxler Differential Revision: D71089700 fbshipit-source-id: 0040a85ee177ade1eaa00cc71f6cbc670f5e2da2
1 parent 6e0462b commit 6d491ef

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

pyre2/lib/alt/function.rs

+22-14
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
9393
acc.split_off_first().0
9494
} else {
9595
acc.reverse();
96-
Type::Overload(Overload { signatures: acc })
96+
Type::Overload(Overload {
97+
signatures: acc,
98+
metadata: Box::new(first.metadata.clone()),
99+
})
97100
}
98101
} else {
99102
ty
@@ -117,7 +120,10 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
117120
);
118121
defs.split_off_first().0
119122
} else {
120-
Type::Overload(Overload { signatures: defs })
123+
Type::Overload(Overload {
124+
signatures: defs,
125+
metadata: Box::new(first.metadata.clone()),
126+
})
121127
}
122128
} else {
123129
first.ty.clone()
@@ -372,22 +378,23 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
372378
defining_cls.as_ref().map(|cls| cls.name()),
373379
&def.name.id,
374380
);
381+
let metadata = FuncMetadata {
382+
kind,
383+
flags: FuncFlags {
384+
is_overload,
385+
is_staticmethod,
386+
is_classmethod,
387+
is_property_getter,
388+
is_property_setter_with_getter,
389+
has_enum_member_decoration,
390+
is_override,
391+
},
392+
};
375393
let mut ty = Forall::new_type(
376394
self.type_params(def.range, tparams, errors),
377395
ForallType::Function(Function {
378396
signature: callable,
379-
metadata: FuncMetadata {
380-
kind,
381-
flags: FuncFlags {
382-
is_overload,
383-
is_staticmethod,
384-
is_classmethod,
385-
is_property_getter,
386-
is_property_setter_with_getter,
387-
has_enum_member_decoration,
388-
is_override,
389-
},
390-
},
397+
metadata: metadata.clone(),
391398
}),
392399
);
393400
for x in decorators.into_iter().rev() {
@@ -396,6 +403,7 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
396403
Arc::new(DecoratedFunction {
397404
id_range: def.name.range,
398405
ty,
406+
metadata,
399407
is_overload,
400408
})
401409
}

pyre2/lib/alt/types/decorated_function.rs

+15
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ use std::fmt;
99
use std::fmt::Debug;
1010
use std::fmt::Display;
1111

12+
use ruff_python_ast::name::Name;
1213
use ruff_text_size::TextRange;
1314

15+
use crate::module::module_name::ModuleName;
16+
use crate::types::callable::FuncFlags;
17+
use crate::types::callable::FuncId;
18+
use crate::types::callable::FuncMetadata;
19+
use crate::types::callable::FunctionKind;
1420
use crate::types::types::Type;
1521

1622
/// The type of a function definition after decorators are applied. Metadata arising from the
@@ -20,6 +26,7 @@ use crate::types::types::Type;
2026
pub struct DecoratedFunction {
2127
pub id_range: TextRange,
2228
pub ty: Type,
29+
pub metadata: FuncMetadata,
2330
pub is_overload: bool,
2431
}
2532

@@ -34,6 +41,14 @@ impl DecoratedFunction {
3441
DecoratedFunction {
3542
id_range: TextRange::default(),
3643
ty: Type::any_implicit(),
44+
metadata: FuncMetadata {
45+
kind: FunctionKind::Def(Box::new(FuncId {
46+
module: ModuleName::default(),
47+
cls: None,
48+
func: Name::default(),
49+
})),
50+
flags: FuncFlags::default(),
51+
},
3752
is_overload: false,
3853
}
3954
}

pyre2/lib/types/types.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ impl BoundMethodType {
324324
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
325325
pub struct Overload {
326326
pub signatures: Vec1<Type>,
327+
pub metadata: Box<FuncMetadata>,
327328
}
328329

329330
impl Overload {
@@ -620,7 +621,12 @@ impl Type {
620621
.signatures
621622
.try_mapped_ref(|x| x.to_unbound_callable().ok_or(()))
622623
.ok()
623-
.map(|signatures| Type::Overload(Overload { signatures })),
624+
.map(|signatures| {
625+
Type::Overload(Overload {
626+
signatures,
627+
metadata: overload.metadata.clone(),
628+
})
629+
}),
624630
_ => None,
625631
}
626632
}

0 commit comments

Comments
 (0)