Skip to content

Commit 1fefa1f

Browse files
rchen152meta-codesync[bot]
authored andcommitted
Apply LitStyle-preserving logic to augmented assignment (+=)
Reviewed By: kinto0 Differential Revision: D103092401 fbshipit-source-id: 645f34cbdb213d3fb4eebfe7d342eb22a61fe13e
1 parent eed3356 commit 1fefa1f

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

pyrefly/lib/alt/operators.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -616,10 +616,13 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
616616
} else if let Type::Any(style) = &rhs {
617617
style.propagate()
618618
} else if x.op == Operator::Add
619-
&& base.is_literal_string()
620-
&& rhs.is_literal_string()
619+
&& let Some(lhs_style) = lhs.lit_string_style()
620+
&& let Some(rhs_style) = rhs.lit_string_style()
621621
{
622-
self.heap.mk_literal_string(LitStyle::Implicit)
622+
self.heap.mk_literal_string(match (lhs_style, rhs_style) {
623+
(LitStyle::Explicit, LitStyle::Explicit) => LitStyle::Explicit,
624+
_ => LitStyle::Implicit,
625+
})
623626
} else if x.op == Operator::Add
624627
&& let Type::Tuple(ref l) = base
625628
&& let Type::Tuple(r) = rhs

pyrefly/lib/test/attributes.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,6 +2430,22 @@ def f(c: C):
24302430
"#,
24312431
);
24322432

2433+
testcase!(
2434+
test_do_not_promote_augmented_addition_of_explicit_literal_strings,
2435+
r#"
2436+
from typing import LiteralString, assert_type
2437+
2438+
class C:
2439+
def __init__(self, x: LiteralString, y: LiteralString) -> None:
2440+
z = x
2441+
z += y
2442+
self.attr = z
2443+
2444+
def f(c: C):
2445+
assert_type(c.attr, LiteralString)
2446+
"#,
2447+
);
2448+
24332449
testcase!(
24342450
test_top_level_anonymous_typeddict,
24352451
r#"

0 commit comments

Comments
 (0)