Skip to content

Commit 6d9dc7f

Browse files
fangyi-zhoumeta-codesync[bot]
authored andcommitted
Improve error message for assignments to final variables (#1923)
Summary: - Unify two different error messages depending on call sites - Add backticks around variable names Pull Request resolved: #1923 Test Plan: `python test.py` Reviewed By: yangdanny97 Differential Revision: D89740922 Pulled By: grievejia fbshipit-source-id: 428db5dc654961de068fd78819f1da28dbe39d06
1 parent 562abca commit 6d9dc7f

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

conformance/third_party/conformance.exp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9590,8 +9590,8 @@
95909590
{
95919591
"code": -2,
95929592
"column": 5,
9593-
"concise_description": "Cannot assign to var x because it is marked final",
9594-
"description": "Cannot assign to var x because it is marked final",
9593+
"concise_description": "Cannot assign to variable `x` because it is marked final",
9594+
"description": "Cannot assign to variable `x` because it is marked final",
95959595
"line": 145,
95969596
"name": "bad-assignment",
95979597
"severity": "error",
@@ -9601,8 +9601,8 @@
96019601
{
96029602
"code": -2,
96039603
"column": 15,
9604-
"concise_description": "Assignment target is marked final",
9605-
"description": "Assignment target is marked final",
9604+
"concise_description": "Cannot assign to variable `x` because it is marked final",
9605+
"description": "Cannot assign to variable `x` because it is marked final",
96069606
"line": 147,
96079607
"name": "bad-assignment",
96089608
"severity": "error",

pyrefly/lib/alt/solve.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2799,7 +2799,10 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
27992799
errors,
28002800
e.range(),
28012801
ErrorInfo::Kind(ErrorKind::BadAssignment),
2802-
"Assignment target is marked final".to_owned(),
2802+
format!(
2803+
"Cannot assign to {} because it is marked final",
2804+
annot.target
2805+
),
28032806
);
28042807
}
28052808
self.expr(e, annot.ty(self.stdlib).as_ref().map(|t| (t, tcc)), errors)
@@ -2836,7 +2839,10 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
28362839
errors,
28372840
*range,
28382841
ErrorInfo::Kind(ErrorKind::BadAssignment),
2839-
"Assignment target is marked final".to_owned(),
2842+
format!(
2843+
"Cannot assign to {} because it is marked final",
2844+
annot.target
2845+
),
28402846
);
28412847
}
28422848
if let Some(annot_ty) = annot.ty(self.stdlib)

pyrefly/lib/binding/binding.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,12 +1893,12 @@ pub enum AnnotationTarget {
18931893
impl Display for AnnotationTarget {
18941894
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
18951895
match self {
1896-
Self::Param(name) => write!(f, "param {name}"),
1897-
Self::ArgsParam(name) => write!(f, "args {name}"),
1898-
Self::KwargsParam(name) => write!(f, "kwargs {name}"),
1899-
Self::Return(name) => write!(f, "{name} return"),
1900-
Self::Assign(name, _initialized) => write!(f, "var {name}"),
1901-
Self::ClassMember(name) => write!(f, "attr {name}"),
1896+
Self::Param(name) => write!(f, "parameter `{name}`"),
1897+
Self::ArgsParam(name) => write!(f, "args `{name}`"),
1898+
Self::KwargsParam(name) => write!(f, "kwargs `{name}`"),
1899+
Self::Return(name) => write!(f, "`{name}` return"),
1900+
Self::Assign(name, _initialized) => write!(f, "variable `{name}`"),
1901+
Self::ClassMember(name) => write!(f, "attribute `{name}`"),
19021902
}
19031903
}
19041904
}

pyrefly/lib/test/assign.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ testcase!(
477477
r#"
478478
from typing import Final
479479
x: Final = [""]
480-
x += [""] # E: Cannot assign to var x because it is marked final
480+
x += [""] # E: Cannot assign to variable `x` because it is marked final
481481
x[0] += ""
482482
"#,
483483
);

pyrefly/lib/test/simple.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,18 @@ class C:
372372
"#,
373373
);
374374

375+
testcase!(
376+
test_final_reassign,
377+
r#"
378+
from typing import Final
379+
x: Final[int] = 0
380+
x = 1 # E: `x` is marked final
381+
x += 1 # E: Cannot assign to variable `x` because it is marked final
382+
y = x = 3 # E: Cannot assign to variable `x` because it is marked final
383+
y = (x := 3) # E: Cannot assign to variable `x` because it is marked final
384+
"#,
385+
);
386+
375387
testcase!(
376388
test_reveal_type,
377389
r#"

0 commit comments

Comments
 (0)