Skip to content

Commit 9861a78

Browse files
rchen152stroxler
authored andcommitted
Use call_method rather than call_method_or_error in iterate()
Summary: Using call_method gives us more flexibility. I'm working towards being able to drop in call_metaclass_method to iterate over a class. Reviewed By: yangdanny97 Differential Revision: D67615542 fbshipit-source-id: c239ec1997144576b64ff208c940f339ae314ae0
1 parent 71075e9 commit 9861a78

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

pyre2/pyre2/bin/alt/answers.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -659,16 +659,17 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
659659
fn iterate(&self, iterable: &Type, range: TextRange) -> Iterable {
660660
match iterable {
661661
Type::ClassType(cls) => {
662-
let ty = if self.has_attribute(cls.class_object(), &dunder::ITER) {
663-
let iterator_ty =
664-
self.call_method_or_error(iterable, &dunder::ITER, range, &[], &[]);
662+
let ty = if let Some(iterator_ty) =
663+
self.call_method(iterable, &dunder::ITER, range, &[], &[])
664+
{
665665
self.call_method_or_error(&iterator_ty, &dunder::NEXT, range, &[], &[])
666-
} else if self.has_attribute(cls.class_object(), &dunder::GETITEM) {
666+
} else {
667667
let int_ty = self.stdlib.int().to_type();
668668
let arg = CallArg::Type(&int_ty, range);
669-
self.call_method_or_error(iterable, &dunder::GETITEM, range, &[arg], &[])
670-
} else {
671-
self.error(range, format!("Class `{}` is not iterable", cls.name()))
669+
self.call_method(iterable, &dunder::GETITEM, range, &[arg], &[])
670+
.unwrap_or_else(|| {
671+
self.error(range, format!("Class `{}` is not iterable", cls.name()))
672+
})
672673
};
673674
Iterable::OfType(ty)
674675
}

pyre2/pyre2/bin/alt/classes.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -542,10 +542,6 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
542542
}
543543
}
544544

545-
pub fn has_attribute(&self, cls: &Class, name: &Name) -> bool {
546-
self.get_class_member(cls, name).is_some()
547-
}
548-
549545
pub fn get_instance_attribute(&self, cls: &ClassType, name: &Name) -> Option<Attribute> {
550546
self.get_class_member(cls.class_object(), name)
551547
.map(|(member_ty, defining_class)| {

0 commit comments

Comments
 (0)