Skip to content

Commit 77a9339

Browse files
rchen152meta-codesync[bot]
authored andcommitted
Make format_deprecation a method on Deprecation
Summary: Nicer code organization. Reviewed By: fangyi-zhou Differential Revision: D87518878 fbshipit-source-id: da997052d79b73d4a1eee06cb6d19fb17c8bf9b1
1 parent a083984 commit 77a9339

File tree

6 files changed

+27
-42
lines changed

6 files changed

+27
-42
lines changed

crates/pyrefly_types/src/callable.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ use pyrefly_util::visit::Visit;
2626
use pyrefly_util::visit::VisitMut;
2727
use ruff_python_ast::Keyword;
2828
use ruff_python_ast::name::Name;
29+
use vec1::Vec1;
30+
use vec1::vec1;
2931

3032
use crate::class::Class;
3133
use crate::class::ClassType;
@@ -336,6 +338,14 @@ impl Deprecation {
336338
pub fn new(message: Option<String>) -> Self {
337339
Self { message }
338340
}
341+
342+
/// Format a base description using deprecation metadata.
343+
pub fn as_error_message(&self, base: String) -> Vec1<String> {
344+
match self.message.as_ref().map(|s| s.trim()) {
345+
Some(msg) if !msg.is_empty() => vec1![base, msg.to_owned()],
346+
_ => vec1![base],
347+
}
348+
}
339349
}
340350

341351
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]

pyrefly/lib/alt/call.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use crate::alt::class::class_field::DescriptorBase;
3434
use crate::alt::unwrap::HintRef;
3535
use crate::binding::binding::Key;
3636
use crate::config::error_kind::ErrorKind;
37-
use crate::deprecation::format_deprecation;
3837
use crate::error::collector::ErrorCollector;
3938
use crate::error::context::ErrorContext;
4039
use crate::error::context::ErrorInfo;
@@ -376,10 +375,10 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
376375
// We manually construct an error using the message from the context but a
377376
// Deprecated error kind so that the error is shown at the Deprecated severity
378377
// (default: WARN) rather than the severity of the context's error kind.
379-
let mut msg = format_deprecation(
380-
format!("`{}` is deprecated", m.kind.format(self.module().name())),
381-
Some(deprecation),
382-
);
378+
let mut msg = deprecation.as_error_message(format!(
379+
"`{}` is deprecated",
380+
m.kind.format(self.module().name())
381+
));
383382
if let Some(ctx) = context {
384383
msg.insert(0, ctx().format());
385384
}

pyrefly/lib/alt/expr.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ use crate::binding::binding::Key;
5656
use crate::binding::binding::KeyYield;
5757
use crate::binding::binding::KeyYieldFrom;
5858
use crate::config::error_kind::ErrorKind;
59-
use crate::deprecation::format_deprecation;
6059
use crate::error::collector::ErrorCollector;
6160
use crate::error::context::ErrorContext;
6261
use crate::error::context::ErrorInfo;
@@ -247,10 +246,7 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
247246
errors.add(
248247
range,
249248
ErrorInfo::Kind(ErrorKind::Deprecated),
250-
format_deprecation(
251-
format!("`{deprecated_function}` is deprecated"),
252-
Some(&deprecation),
253-
),
249+
deprecation.as_error_message(format!("`{deprecated_function}` is deprecated")),
254250
);
255251
}
256252
}

pyrefly/lib/alt/overload.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ use crate::alt::callable::CallWithTypes;
3232
use crate::alt::expr::TypeOrExpr;
3333
use crate::alt::unwrap::HintRef;
3434
use crate::config::error_kind::ErrorKind;
35-
use crate::deprecation::format_deprecation;
3635
use crate::error::collector::ErrorCollector;
3736
use crate::error::context::ErrorContext;
3837
use crate::error::context::ErrorInfo;
@@ -321,18 +320,15 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
321320
if matched {
322321
// If the selected overload is deprecated, we log a deprecation error.
323322
if let Some(deprecation) = &closest_overload.func.1.metadata.flags.deprecation {
324-
let msg = format_deprecation(
325-
format!(
326-
"Call to deprecated overload `{}`",
327-
closest_overload
328-
.func
329-
.1
330-
.metadata
331-
.kind
332-
.format(self.module().name())
333-
),
334-
Some(deprecation),
335-
);
323+
let msg = deprecation.as_error_message(format!(
324+
"Call to deprecated overload `{}`",
325+
closest_overload
326+
.func
327+
.1
328+
.metadata
329+
.kind
330+
.format(self.module().name())
331+
));
336332
errors.add(range, ErrorInfo::new(ErrorKind::Deprecated, context), msg);
337333
}
338334
(closest_overload.res, closest_overload.func.1.signature)

pyrefly/lib/binding/stmt.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ use crate::binding::scope::FlowStyle;
4444
use crate::binding::scope::LoopExit;
4545
use crate::binding::scope::Scope;
4646
use crate::config::error_kind::ErrorKind;
47-
use crate::deprecation::format_deprecation;
4847
use crate::error::context::ErrorInfo;
4948
use crate::export::definitions::MutableCaptureKind;
5049
use crate::export::exports::Export;
@@ -1002,10 +1001,7 @@ impl<'a> BindingsBuilder<'a> {
10021001
..
10031002
})) = exported.get_hashed(name)
10041003
{
1005-
let msg = format_deprecation(
1006-
format!("`{name}` is deprecated"),
1007-
Some(deprecation),
1008-
);
1004+
let msg = deprecation.as_error_message(format!("`{name}` is deprecated"));
10091005
self.error_multiline(x.range, ErrorInfo::Kind(ErrorKind::Deprecated), msg);
10101006
}
10111007
let val = if exported.contains_key_hashed(name) {
@@ -1052,10 +1048,8 @@ impl<'a> BindingsBuilder<'a> {
10521048
..
10531049
})) = exported.get(&x.name.id)
10541050
{
1055-
let msg = format_deprecation(
1056-
format!("`{}` is deprecated", x.name),
1057-
Some(deprecation),
1058-
);
1051+
let msg =
1052+
deprecation.as_error_message(format!("`{}` is deprecated", x.name));
10591053
self.error_multiline(x.range, ErrorInfo::Kind(ErrorKind::Deprecated), msg);
10601054
}
10611055
Binding::Import(m, x.name.id.clone(), original_name_range)

pyrefly/lib/deprecation.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ use ruff_python_ast::Expr;
1010
use ruff_python_ast::ExprAttribute;
1111
use ruff_python_ast::ExprCall;
1212
use ruff_python_ast::ExprStringLiteral;
13-
use vec1::Vec1;
14-
use vec1::vec1;
1513

1614
fn is_deprecated_target(e: &Expr) -> bool {
1715
let (base, value) = match e {
@@ -45,11 +43,3 @@ pub fn parse_deprecation(e: &Expr) -> Option<Deprecation> {
4543
}
4644
Some(Deprecation::new(extract_message(call)))
4745
}
48-
49-
/// Format a base description using metadata from a parsed decorator.
50-
pub fn format_deprecation(base: String, deprecation: Option<&Deprecation>) -> Vec1<String> {
51-
match deprecation.and_then(|d| d.message.as_ref().map(|s| s.trim())) {
52-
Some(msg) if !msg.is_empty() => vec1![base, msg.to_owned()],
53-
_ => vec1![base],
54-
}
55-
}

0 commit comments

Comments
 (0)