Skip to content

Commit 1896ffe

Browse files
arthaudmeta-codesync[bot]
authored andcommitted
Handle sys.version_info.minor in static evaluation
Summary: We can statically evaluate `sys.version_info >= (3, 12)` and `sys.version_info[1] >= 10`, but not `sys.version_info.minor >= 10` (named attribute access). This caused Pysa crashes when using Pyrefly as the type checker backend: Pyrefly would report function definitions from dead branches as reachable callables, but Pyre's preprocessing (which handles `.minor`) would remove them, leading to an unmatched callable error in `pyreflyApi.ml`. Reviewed By: yangdanny97 Differential Revision: D105690953 fbshipit-source-id: 3151823eb3d81e07292887d111274bfd6821fd46
1 parent d6c88a5 commit 1896ffe

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

crates/pyrefly_python/src/sys_info.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,15 @@ impl SysInfo {
554554
}) if value.is_name_expr() && Self::is_type_checking_constant_name(attr.as_str()) => {
555555
Some(Value::Bool(self.type_checking()))
556556
}
557+
Expr::Attribute(ExprAttribute { value, attr, .. }) => match self.evaluate(value)? {
558+
Value::VersionInfo(version) => match attr.as_str() {
559+
"major" => Some(Value::Int(version.major as i64)),
560+
"minor" => Some(Value::Int(version.minor as i64)),
561+
"micro" => Some(Value::Int(version.micro as i64)),
562+
_ => None,
563+
},
564+
_ => None,
565+
},
557566
Expr::Call(ExprCall {
558567
func, arguments, ..
559568
}) if let Expr::Attribute(ExprAttribute { value, attr, .. }) = &**func

pyrefly/lib/test/sys_info.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ if sys.version_info[1] >= 6:
5151
else:
5252
C = int
5353
assert_type(C(), str)
54+
55+
if sys.version_info.minor >= 6:
56+
D = str
57+
else:
58+
D = int
59+
assert_type(D(), str)
60+
61+
if sys.version_info.major == 3:
62+
E = str
63+
else:
64+
E = int
65+
assert_type(E(), str)
5466
"#,
5567
);
5668

0 commit comments

Comments
 (0)