Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pyrefly/lib/alt/class/class_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1560,12 +1560,20 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
self.determine_read_only_reason(name, annotation.as_ref(), &metadata, field_definition);

// Determine the final type, promoting literals when appropriate.
let mut has_literal = false;
if matches!(initialization, ClassFieldInitialization::Method) {
value_ty.universe(&mut |current_type_node| {
if matches!(current_type_node, Type::Literal(_) | Type::LiteralString) {
has_literal = true;
}
});
}
let ty = if annotation
.as_ref()
.and_then(|ann| ann.ty.as_ref())
.is_none()
&& matches!(read_only_reason, None | Some(ReadOnlyReason::NamedTuple))
&& value_ty.is_literal()
&& (value_ty.is_literal() || has_literal)
{
value_ty.promote_literals(self.stdlib)
} else {
Expand Down
13 changes: 13 additions & 0 deletions pyrefly/lib/test/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ class A:
"#,
);

testcase!(
test_unannotated_attribute_tuple_literal_promotion,
r#"
from typing import assert_type
class A:
def __init__(self):
self.x = (42, 42)
def f(a: A):
assert_type(a.x, tuple[int, int])
a.x = (0, 0)
"#,
);

testcase!(
test_super_object_bad_assignment,
r#"
Expand Down
Loading