From b49ba59da7bf0f419638d3f506c5f67b975e4e85 Mon Sep 17 00:00:00 2001 From: HugoCasa Date: Thu, 21 Nov 2024 14:44:19 +0100 Subject: [PATCH] fix: infer python list inner type from default if unknown inner (#4771) --- backend/parsers/windmill-parser-py/src/lib.rs | 94 +++++++++++++++++-- frontend/package-lock.json | 56 +++++------ frontend/package.json | 14 +-- 3 files changed, 122 insertions(+), 42 deletions(-) diff --git a/backend/parsers/windmill-parser-py/src/lib.rs b/backend/parsers/windmill-parser-py/src/lib.rs index 5b42171478..738bc8ed7c 100644 --- a/backend/parsers/windmill-parser-py/src/lib.rs +++ b/backend/parsers/windmill-parser-py/src/lib.rs @@ -93,6 +93,12 @@ pub fn parse_python_signature( .iter() .enumerate() .map(|(i, x)| { + let mut typ = x + .as_arg() + .annotation + .as_ref() + .map_or(Typ::Unknown, |e| parse_expr(e)); + let default = if i >= def_arg_start { params .defaults() @@ -103,19 +109,28 @@ pub fn parse_python_signature( None }; - let mut typ = x - .as_arg() - .annotation - .as_ref() - .map_or(Typ::Unknown, |e| parse_expr(e)); + let should_get_type_from_default = match &typ { + Typ::Unknown => true, + // if the type is a list of unknowns, we should get the type from the default + Typ::List(inner) => matches!(inner.as_ref(), Typ::Unknown), + _ => false, + }; - if typ == Typ::Unknown + if should_get_type_from_default && default.is_some() && default != Some(json!(FUNCTION_CALL)) { typ = json_to_typ(default.as_ref().unwrap()); } + // if the type is still a list of unknowns after checking the default, we set it to a list of strings to not break past behavior + match typ { + Typ::List(inner) if matches!(inner.as_ref(), Typ::Unknown) => { + typ = Typ::List(Box::new(Typ::Str(None))); + } + _ => {} + } + Arg { otyp: None, name: x.as_arg().arg.to_string(), @@ -193,7 +208,7 @@ fn parse_typ(id: &str) -> Typ { "int" => Typ::Int, "bool" => Typ::Bool, "dict" => Typ::Object(vec![]), - "list" => Typ::List(Box::new(Typ::Str(None))), + "list" => Typ::List(Box::new(Typ::Unknown)), "bytes" => Typ::Bytes, "datetime" => Typ::Datetime, "datetime.datetime" => Typ::Datetime, @@ -593,4 +608,69 @@ def main(): return Ok(()) } + + #[test] + fn test_parse_python_sig_8() -> anyhow::Result<()> { + let code = r#" +from typing import List +def main(a: list, e: List[int], b: list = [1,2,3,4], c = [1,2,3,4], d = ["a", "b"]): return +"#; + println!( + "{}", + serde_json::to_string(&parse_python_signature(code, None)?)? + ); + assert_eq!( + parse_python_signature(code, None)?, + MainArgSignature { + star_args: false, + star_kwargs: false, + args: vec![ + Arg { + otyp: None, + name: "a".to_string(), + typ: Typ::List(Box::new(Typ::Str(None))), + default: None, + has_default: false, + oidx: None + }, + Arg { + otyp: None, + name: "e".to_string(), + typ: Typ::List(Box::new(Typ::Int)), + default: None, + has_default: false, + oidx: None + }, + Arg { + otyp: None, + name: "b".to_string(), + typ: Typ::List(Box::new(Typ::Int)), + default: Some(json!([1, 2, 3, 4])), + has_default: true, + oidx: None + }, + Arg { + otyp: None, + name: "c".to_string(), + typ: Typ::List(Box::new(Typ::Int)), + default: Some(json!([1, 2, 3, 4])), + has_default: true, + oidx: None + }, + Arg { + otyp: None, + name: "d".to_string(), + typ: Typ::List(Box::new(Typ::Str(None))), + default: Some(json!(["a", "b"])), + has_default: true, + oidx: None + } + ], + no_main_func: Some(false), + has_preprocessor: Some(false) + } + ); + + Ok(()) + } } diff --git a/frontend/package-lock.json b/frontend/package-lock.json index fc16842671..e689351d20 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -69,13 +69,13 @@ "vscode-languageclient": "~9.0.1", "vscode-uri": "~3.0.8", "vscode-ws-jsonrpc": "~3.3.2", - "windmill-parser-wasm-go": "^1.397.2", - "windmill-parser-wasm-php": "^1.397.2", - "windmill-parser-wasm-py": "^1.397.2", - "windmill-parser-wasm-regex": "^1.397.2", - "windmill-parser-wasm-rust": "^1.397.2", - "windmill-parser-wasm-ts": "^1.397.2", - "windmill-parser-wasm-yaml": "^1.398.1", + "windmill-parser-wasm-go": "^1.429.0", + "windmill-parser-wasm-php": "^1.429.0", + "windmill-parser-wasm-py": "^1.429.0", + "windmill-parser-wasm-regex": "^1.429.0", + "windmill-parser-wasm-rust": "^1.429.0", + "windmill-parser-wasm-ts": "^1.429.0", + "windmill-parser-wasm-yaml": "^1.429.0", "windmill-sql-datatype-parser-wasm": "^1.318.0", "y-monaco": "^0.1.4", "y-websocket": "^1.5.4", @@ -13641,39 +13641,39 @@ } }, "node_modules/windmill-parser-wasm-go": { - "version": "1.397.2", - "resolved": "https://registry.npmjs.org/windmill-parser-wasm-go/-/windmill-parser-wasm-go-1.397.2.tgz", - "integrity": "sha512-je5X7nsbudwlZL6QLpA/s1fwTip0Xmcc8c8eciJjK9IY6JZd2s8IRmtgVb3MB1ppQ4poCzypeqpYcvlL4MOGlg==" + "version": "1.429.0", + "resolved": "https://registry.npmjs.org/windmill-parser-wasm-go/-/windmill-parser-wasm-go-1.429.0.tgz", + "integrity": "sha512-M3jeGDqeTyPj9HyyX3msdzMrqIIzlfMfxTMsXS8m7MJp4Cm60qifMxD29Ipxb2B4WdzyGwCSlaBjLsXu0b3c5g==" }, "node_modules/windmill-parser-wasm-php": { - "version": "1.397.2", - "resolved": "https://registry.npmjs.org/windmill-parser-wasm-php/-/windmill-parser-wasm-php-1.397.2.tgz", - "integrity": "sha512-+ys3e84UojguDxSJurMT/T2m0XGYUyE59+TbIUteWjxyMnSoynC3GhYvWU3FX0yojwiLOdDaUZkGlkNy8Fl3+w==" + "version": "1.429.0", + "resolved": "https://registry.npmjs.org/windmill-parser-wasm-php/-/windmill-parser-wasm-php-1.429.0.tgz", + "integrity": "sha512-SGJAtNpfdRZftkGboxWsm/yQDnJBJodwPQUbX2cWk/aoNook6ULesZwsYtBC9WN1VH6TIskLiVPohMmu6jtXmw==" }, "node_modules/windmill-parser-wasm-py": { - "version": "1.397.2", - "resolved": "https://registry.npmjs.org/windmill-parser-wasm-py/-/windmill-parser-wasm-py-1.397.2.tgz", - "integrity": "sha512-gnqQZTRmQi2s3ok9IyixTHGK3MOfJpW2+nnH4W8vXGj1VQMGoXScaZA1W8Nt6/lFy1l+NkAX48mD+nU5s/xbJA==" + "version": "1.429.0", + "resolved": "https://registry.npmjs.org/windmill-parser-wasm-py/-/windmill-parser-wasm-py-1.429.0.tgz", + "integrity": "sha512-cqc+tblQVHVrc8wNA4esVYD1Dv59XQJ4mHXFFPa8Lx5UjXv7FO/0VQxyQuRyXaP/V6J6DDy0oeN107eFNLxrBg==" }, "node_modules/windmill-parser-wasm-regex": { - "version": "1.397.2", - "resolved": "https://registry.npmjs.org/windmill-parser-wasm-regex/-/windmill-parser-wasm-regex-1.397.2.tgz", - "integrity": "sha512-4DhxElRiz9FuwTcMMBhIfk+9cD/oA2+Nf0EBh5BZ1VK4EOD1BVutkTyzWyWP6J+Zqv4m91VyfdVK0Td0qXl+cw==" + "version": "1.429.0", + "resolved": "https://registry.npmjs.org/windmill-parser-wasm-regex/-/windmill-parser-wasm-regex-1.429.0.tgz", + "integrity": "sha512-rjgrgSYjEBU2+RB6ffFR+nmvEJdiqVKYiwugzHQE+VgVTYMJ0jLBE370n5dBjDj5KPsF+Sfmj6DIUK69uu9wSA==" }, "node_modules/windmill-parser-wasm-rust": { - "version": "1.397.2", - "resolved": "https://registry.npmjs.org/windmill-parser-wasm-rust/-/windmill-parser-wasm-rust-1.397.2.tgz", - "integrity": "sha512-7tjekDV2yuqqSjpPF/BfRzaXUwT5eckMWCuq4o18UzChOOeeA4U5goY4INBOGFzYR89m9AeMhwT1IM71nOGrIA==" + "version": "1.429.0", + "resolved": "https://registry.npmjs.org/windmill-parser-wasm-rust/-/windmill-parser-wasm-rust-1.429.0.tgz", + "integrity": "sha512-c8mjpiw8RxoaBDtecb+sKeWM/IOjNr4Y06nHudGu8sMM48MNO1LhgcISLv8wl6Z9zWd7OzQrECJ6RLorpii5Uw==" }, "node_modules/windmill-parser-wasm-ts": { - "version": "1.397.2", - "resolved": "https://registry.npmjs.org/windmill-parser-wasm-ts/-/windmill-parser-wasm-ts-1.397.2.tgz", - "integrity": "sha512-nspEC1zLfrvvh6ta8YSjKo99lDfFvsXh7rV2mmpTAmc2ZzzyuVDvW5Cm0Wmo8Ww3seO+oDi/v3jy+8QX8pIzwA==" + "version": "1.429.0", + "resolved": "https://registry.npmjs.org/windmill-parser-wasm-ts/-/windmill-parser-wasm-ts-1.429.0.tgz", + "integrity": "sha512-mUC2dpHxEqNf0vVdOWR+dLJMDCjfaYvP00ywu8PjVUdLAGr4ic366jC9DiYTymLZFEUV5316snna8DAReWGRUA==" }, "node_modules/windmill-parser-wasm-yaml": { - "version": "1.398.1", - "resolved": "https://registry.npmjs.org/windmill-parser-wasm-yaml/-/windmill-parser-wasm-yaml-1.398.1.tgz", - "integrity": "sha512-f5MCJ9PVLrJQBcVuxnTlZNw561HIJkMqX/76HLgOqKehNpO7gboZyaHx0txUJP50MIDv/IMpn0SD6S7DZnynEg==" + "version": "1.429.0", + "resolved": "https://registry.npmjs.org/windmill-parser-wasm-yaml/-/windmill-parser-wasm-yaml-1.429.0.tgz", + "integrity": "sha512-elQYkaWOvzB8LiwVV9NbNqrupSmdtRY3mMEl+qmKTJhGLvYrVOxA8zyBtwGK0MGiFhTOO3ZO96LWSlDfBnpN9g==" }, "node_modules/windmill-sql-datatype-parser-wasm": { "version": "1.318.0", diff --git a/frontend/package.json b/frontend/package.json index d4ce31532c..aacecc1977 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -142,13 +142,13 @@ "vscode-languageclient": "~9.0.1", "vscode-uri": "~3.0.8", "vscode-ws-jsonrpc": "~3.3.2", - "windmill-parser-wasm-go": "^1.397.2", - "windmill-parser-wasm-php": "^1.397.2", - "windmill-parser-wasm-py": "^1.397.2", - "windmill-parser-wasm-regex": "^1.397.2", - "windmill-parser-wasm-rust": "^1.397.2", - "windmill-parser-wasm-ts": "^1.397.2", - "windmill-parser-wasm-yaml": "^1.398.1", + "windmill-parser-wasm-go": "^1.429.0", + "windmill-parser-wasm-php": "^1.429.0", + "windmill-parser-wasm-py": "^1.429.0", + "windmill-parser-wasm-regex": "^1.429.0", + "windmill-parser-wasm-rust": "^1.429.0", + "windmill-parser-wasm-ts": "^1.429.0", + "windmill-parser-wasm-yaml": "^1.429.0", "windmill-sql-datatype-parser-wasm": "^1.318.0", "y-monaco": "^0.1.4", "y-websocket": "^1.5.4",