Skip to content

Commit e9dedf7

Browse files
stroxlermeta-codesync[bot]
authored andcommitted
Add test for int64 constructor call pattern
Summary: Add test verifying that `int64(0)` constructor calls get correct contextual type in both annotated (`x: int64 = int64(0)`) and unannotated (`y = int64(0)`) assignments. Both cases correctly produce contextual_type pointing to `__static__.int64`. Reviewed By: alexmalyshev Differential Revision: D96509478 fbshipit-source-id: 19df7f41b3c305246fca9c549c6abde7c7482ea4
1 parent 107aea0 commit e9dedf7

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

pyrefly/lib/test/cinderx.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,70 @@ class Bar:
11171117
);
11181118
}
11191119

1120+
/// Test that `int64(0)` (a constructor call) gets contextual type treatment.
1121+
/// This mirrors a pattern the CinderX team hits in practice: using the
1122+
/// `int64` constructor to initialize a primitive-typed local.
1123+
///
1124+
/// Both annotated (`x: int64 = int64(0)`) and unannotated (`y = int64(0)`)
1125+
/// assignments should work: `x` gets contextual_type from the AnnAssign,
1126+
/// and `y` gets it from the Assign because pyrefly infers `y`'s type as
1127+
/// `__static__.int64` from the constructor call.
1128+
#[test]
1129+
fn test_static_int64_constructor_call() {
1130+
let state = create_state_with_static(
1131+
"test",
1132+
r#"
1133+
import __static__
1134+
from __static__ import int64
1135+
1136+
def main() -> None:
1137+
x: int64 = int64(0)
1138+
y = int64(0)
1139+
"#,
1140+
);
1141+
let transaction = state.transaction();
1142+
let handle = get_handle("test", &transaction);
1143+
1144+
let data = collect_module_types(&transaction, &handle).expect("should collect types");
1145+
1146+
// The type table should contain `__static__.int64` as a class entry.
1147+
let int64_idx = data
1148+
.entries
1149+
.iter()
1150+
.position(|entry| {
1151+
matches!(
1152+
&entry.ty,
1153+
StructuredType::Class { qname, .. } if qname == "__static__.int64"
1154+
)
1155+
})
1156+
.expect("__static__.int64 should exist in the type table");
1157+
1158+
// Both `int64(0)` call expressions should get contextual_type pointing
1159+
// to __static__.int64. Count the locations with this contextual type.
1160+
let locs_with_contextual: Vec<_> = data
1161+
.locations
1162+
.iter()
1163+
.filter(|loc| loc.contextual_type == Some(int64_idx))
1164+
.collect();
1165+
1166+
// We expect at least 2: the RHS of `x: int64 = int64(0)` and `y = int64(0)`.
1167+
assert!(
1168+
locs_with_contextual.len() >= 2,
1169+
"expected at least 2 located types with contextual_type __static__.int64, got {}: {:#?}",
1170+
locs_with_contextual.len(),
1171+
data.locations,
1172+
);
1173+
1174+
// Each of these should have inferred type __static__.int64 as well
1175+
// (since `int64(0)` returns `int64`).
1176+
for loc in &locs_with_contextual {
1177+
assert_eq!(
1178+
loc.type_index, int64_idx,
1179+
"expected inferred type to also be __static__.int64 for int64(0) call",
1180+
);
1181+
}
1182+
}
1183+
11201184
#[test]
11211185
fn test_literal_promoted_type() {
11221186
let state = create_state("test", "x = 42");

0 commit comments

Comments
 (0)