Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

Add Pyrefly support (#7567)

Danny Yang8820949e3b4a
diff --git a/frontend/src/components/app-config/user-config-form.tsx b/frontend/src/components/app-config/user-config-form.tsx
index 5bcce0326a..1084791b1e 100644
--- a/frontend/src/components/app-config/user-config-form.tsx
+++ b/frontend/src/components/app-config/user-config-form.tsx
@@ -228,6 +228,7 @@ export const UserConfigForm: React.FC = () => {
pylsp: true,
ty: true,
basedpyright: true,
+ pyrefly: true,
};
}
@@ -672,6 +673,48 @@ export const UserConfigForm: React.FC = () => {
</div>
)}
/>
+ <FormField
+ control={form.control}
+ name="language_servers.pyrefly.enabled"
+ render={({ field }) => (
+ <div className="flex flex-col gap-1">
+ <FormItem className={formItemClasses}>
+ <FormLabel>
+ <Badge variant="defaultOutline" className="mr-2">
+ Beta
+ </Badge>
+ Pyrefly (
+ <ExternalLink href="https://github.com/facebook/pyrefly">
+ docs
+ </ExternalLink>
+ )
+ </FormLabel>
+ <FormControl>
+ <Checkbox
+ data-testid="pyrefly-checkbox"
+ checked={field.value}
+ disabled={field.disabled}
+ onCheckedChange={(checked) => {
+ field.onChange(Boolean(checked));
+ }}
+ />
+ </FormControl>
+ <FormMessage />
+ <IsOverridden
+ userConfig={config}
+ name="language_servers.pyrefly.enabled"
+ />
+ </FormItem>
+ {field.value && !capabilities.pyrefly && (
+ <Banner kind="danger">
+ Pyrefly is not available in your current environment.
+ Please install <Kbd className="inline">pyrefly</Kbd> in
+ your environment.
+ </Banner>
+ )}
+ </div>
+ )}
+ />
<FormField
control={form.control}
name="language_servers.ty.enabled"
diff --git a/frontend/src/core/codemirror/language/languages/python.ts b/frontend/src/core/codemirror/language/languages/python.ts
index ea57ec53be..b8aa3372a0 100644
--- a/frontend/src/core/codemirror/language/languages/python.ts
+++ b/frontend/src/core/codemirror/language/languages/python.ts
@@ -175,6 +175,44 @@ const tyLspClient = once((_: LSPConfig) => {
return notebookClient;
});
+const pyreflyClient = once(
+ (lspConfig: LSPConfig & { diagnostics: DiagnosticsConfig }) => {
+ let resyncCallback: (() => Promise<void>) | undefined;
+
+ const transport = createTransport("pyrefly", async () => {
+ await resyncCallback?.();
+ });
+
+ const lspClientOpts = {
+ transport,
+ rootUri: getLSPDocumentRootUri(),
+ workspaceFolders: [],
+ };
+
+ // We wrap the client in a NotebookLanguageServerClient to add some
+ // additional functionality to handle multiple cells
+ const notebookClient = new NotebookLanguageServerClient(
+ new LanguageServerClient({
+ ...lspClientOpts,
+ initializationOptions: {
+ pyrefly: {
+ displayTypeErrors:
+ (lspConfig.diagnostics?.enabled ?? false)
+ ? "force-on"
+ : "force-off",
+ },
+ },
+ }),
+ {},
+ );
+
+ // Set the resync callback now that the client exists
+ resyncCallback = () => notebookClient.resyncAllDocuments();
+
+ return notebookClient;
+ },
+);
+
const pyrightClient = once((_: LSPConfig) => {
let resyncCallback: (() => Promise<void>) | undefined;
@@ -263,6 +301,9 @@ export class PythonLanguageAdapter implements LanguageAdapter<{}> {
if (lspConfig?.ty?.enabled && hasCapability("ty")) {
clients.push(tyLspClient(lspConfig));
}
+ if (lspConfig?.pyrefly?.enabled && hasCapability("pyrefly")) {
+ clients.push(pyreflyClient(lspConfig));
+ }
if (lspConfig?.basedpyright?.enabled && hasCapability("basedpyright")) {
clients.push(pyrightClient(lspConfig));
}
diff --git a/frontend/src/core/codemirror/lsp/transports.ts b/frontend/src/core/codemirror/lsp/transports.ts
index 64adf39678..77f357158e 100644
--- a/frontend/src/core/codemirror/lsp/transports.ts
+++ b/frontend/src/core/codemirror/lsp/transports.ts
@@ -14,7 +14,7 @@ import { getRuntimeManager } from "../../runtime/config";
* @returns The transport.
*/
export function createTransport(
- serverName: "pylsp" | "basedpyright" | "copilot" | "ty",
+ serverName: "pylsp" | "basedpyright" | "copilot" | "ty" | "pyrefly",
onReconnect?: () => Promise<void>,
) {
const runtimeManager = getRuntimeManager();
diff --git a/frontend/src/core/config/capabilities.ts b/frontend/src/core/config/capabilities.ts
index 7e12e95890..bc1f22546e 100644
--- a/frontend/src/core/config/capabilities.ts
+++ b/frontend/src/core/config/capabilities.ts
@@ -8,6 +8,7 @@ export const capabilitiesAtom = atom<Capabilities>({
pylsp: false,
basedpyright: false,
ty: false,
+ pyrefly: false,
});
export function hasCapability(key: keyof Capabilities): boolean {
diff --git a/frontend/src/core/runtime/runtime.ts b/frontend/src/core/runtime/runtime.ts
index 96b87c8a6d..53668293d0 100644
--- a/frontend/src/core/runtime/runtime.ts
+++ b/frontend/src/core/runtime/runtime.ts
@@ -140,7 +140,7 @@ export class RuntimeManager {
/**
* The URL of the copilot server.
*/
- getLSPURL(lsp: "pylsp" | "basedpyright" | "copilot" | "ty"): URL {
+ getLSPURL(lsp: "pylsp" | "basedpyright" | "copilot" | "ty" | "pyrefly"): URL {
if (lsp === "copilot") {
// For copilot, don't include any query parameters
const url = this.formatWsURL(`/lsp/${lsp}`);
diff --git a/marimo/_config/config.py b/marimo/_config/config.py
index d2353e1d43..55277e0e23 100644
--- a/marimo/_config/config.py
+++ b/marimo/_config/config.py
@@ -460,6 +460,17 @@ class TyLanguageServerConfig(TypedDict, total=False):
enabled: bool
+@dataclass
+class PyreflyLanguageServerConfig(TypedDict, total=False):
+ """
+ Configuration options for Pyrefly Language Server.
+
+ Pyrefly handles completion, hover, go-to-definition, and diagnostics.
+ """
+
+ enabled: bool
+
+
@dataclass
class LanguageServersConfig(TypedDict, total=False):
"""Configuration options for language servers.
@@ -467,11 +478,15 @@ class LanguageServersConfig(TypedDict, total=False):
**Keys.**
- `pylsp`: the pylsp config
+ - `basedpyright`: the basedpyright config
+ - `ty`: the ty config
+ - `pyrefly`: the pyrefly config
"""
pylsp: PythonLanguageServerConfig
basedpyright: BasedpyrightServerConfig
ty: TyLanguageServerConfig
+ pyrefly: PyreflyLanguageServerConfig
@dataclass
diff --git a/marimo/_dependencies/dependencies.py b/marimo/_dependencies/dependencies.py
index baf673f4f0..f9763bcb37 100644
--- a/marimo/_dependencies/dependencies.py
+++ b/marimo/_dependencies/dependencies.py
@@ -234,6 +234,7 @@ class DependencyManager:
pylsp = Dependency("pylsp")
basedpyright = Dependency("basedpyright")
ty = Dependency("ty")
+ pyrefly = Dependency("pyrefly")
pytest = Dependency("pytest")
vegafusion = Dependency("vegafusion")
vl_convert_python = Dependency("vl_convert")
diff --git a/marimo/_messaging/notification.py b/marimo/_messaging/notification.py
index 67c245bfbb..bb28cdaffe 100644
--- a/marimo/_messaging/notification.py
+++ b/marimo/_messaging/notification.py
@@ -249,6 +249,7 @@ class KernelCapabilitiesNotification(msgspec.Struct):
pylsp: bool = False
ty: bool = False
basedpyright: bool = False
+ pyrefly: bool = False
def __post_init__(self) -> None:
# Only available in mac/linux
@@ -256,6 +257,7 @@ class KernelCapabilitiesNotification(msgspec.Struct):
self.pylsp = DependencyManager.pylsp.has()
self.basedpyright = DependencyManager.basedpyright.has()
self.ty = DependencyManager.ty.has()
+ self.pyrefly = DependencyManager.pyrefly.has()
class KernelReadyNotification(Notification, tag="kernel-ready"):
diff --git a/marimo/_schemas/generated/notifications.yaml b/marimo/_schemas/generated/notifications.yaml
index b95eef3e6c..a7649e0257 100644
--- a/marimo/_schemas/generated/notifications.yaml
+++ b/marimo/_schemas/generated/notifications.yaml
@@ -768,6 +768,9 @@ components:
ty:
default: false
type: boolean
+ pyrefly:
+ default: false
+ type: boolean
required: []
title: KernelCapabilitiesNotification
type: object
diff --git a/marimo/_server/lsp.py b/marimo/_server/lsp.py
index a7f2ace9a5..3807194590 100644
--- a/marimo/_server/lsp.py
+++ b/marimo/_server/lsp.py
@@ -607,6 +607,51 @@ class TyServer(BaseLspServer):
)
+class PyreflyServer(BaseLspServer):
+ id = "pyrefly"
+
+ def __init__(self, port: int) -> None:
+ super().__init__(port)
+ self.log_file = _loggers.get_log_directory() / "pyrefly-lsp.log"
+
+ async def start(self) -> Optional[AlertNotification]:
+ # Pyrefly is not required, so we don't want to alert or fail if it is not installed
+ if not DependencyManager.pyrefly.has():
+ LOGGER.debug("Pyrefly is not installed. Skipping LSP server.")
+ return None
+ return await super().start()
+
+ def validate_requirements(self) -> Union[str, Literal[True]]:
+ if not DependencyManager.pyrefly.has():
+ return "Pyrefly is missing. Install it with `pip install pyrefly`."
+ if not DependencyManager.which("node"):
+ return "node.js binary is missing. Install node at https://nodejs.org/."
+ return True
+
+ def get_command(self) -> list[str]:
+ from pyrefly.__main__ import get_pyrefly_bin # type: ignore
+
+ lsp_bin = marimo_package_path() / "_lsp" / "index.cjs"
+ pyrefly_command = f"pyrefly:{get_pyrefly_bin()}"
+ return [
+ "node",
+ str(lsp_bin),
+ "--port",
+ str(self.port),
+ "--lsp",
+ pyrefly_command,
+ "--log-file",
+ str(self.log_file),
+ ]
+
+ def missing_binary_alert(self) -> AlertNotification:
+ return AlertNotification(
+ title="Pyrefly: Connection Error",
+ description="<span><a class='hyperlink' href='https://github.com/facebook/pyrefly'>Install pyrefly</a> for type checking support.</span>",
+ variant="danger",
+ )
+
+
class NoopLspServer(LspServer):
port: int = 0
id: str = "noop"
@@ -635,6 +680,7 @@ class CompositeLspServer(LspServer):
"pylsp": PyLspServer,
"basedpyright": BasedpyrightServer,
"ty": TyServer,
+ "pyrefly": PyreflyServer,
"copilot": CopilotLspServer,
}
diff --git a/packages/lsp/index.ts b/packages/lsp/index.ts
index d04842d4b1..f04ad99914 100644
--- a/packages/lsp/index.ts
+++ b/packages/lsp/index.ts
@@ -121,6 +121,8 @@ export function parseTypedCommand(typedCommand: string): string[] {
return ["node", binaryPath, "--stdio"];
case "basedpyright":
return [binaryPath, "--stdio"];
+ case "pyrefly":
+ return [binaryPath, "lsp"];
case "ty":
return [binaryPath, "server"];
default:
diff --git a/packages/openapi/api.yaml b/packages/openapi/api.yaml
index 48074bd3da..22f2ee7c50 100644
--- a/packages/openapi/api.yaml
+++ b/packages/openapi/api.yaml
@@ -2184,6 +2184,9 @@ components:
pylsp:
default: false
type: boolean
+ pyrefly:
+ default: false
+ type: boolean
terminal:
default: false
type: boolean
@@ -2493,12 +2496,15 @@ components:
type: object
LanguageServersConfig:
description: "Configuration options for language servers.\n\n **Keys.**\n\
- \n - `pylsp`: the pylsp config"
+ \n - `pylsp`: the pylsp config\n - `basedpyright`: the basedpyright\
+ \ config\n - `ty`: the ty config\n - `pyrefly`: the pyrefly config"
properties:
basedpyright:
$ref: '#/components/schemas/BasedpyrightServerConfig'
pylsp:
$ref: '#/components/schemas/PythonLanguageServerConfig'
+ pyrefly:
+ $ref: '#/components/schemas/PyreflyLanguageServerConfig'
ty:
$ref: '#/components/schemas/TyLanguageServerConfig'
required: []
@@ -3491,6 +3497,17 @@ components:
- tableName
title: PreviewSQLTableRequest
type: object
+ PyreflyLanguageServerConfig:
+ description: 'Configuration options for Pyrefly Language Server.
+
+
+ Pyrefly handles completion, hover, go-to-definition, and diagnostics.'
+ properties:
+ enabled:
+ type: boolean
+ required: []
+ title: PyreflyLanguageServerConfig
+ type: object
PythonLanguageServerConfig:
description: 'Configuration options for Python Language Server.
diff --git a/packages/openapi/src/api.ts b/packages/openapi/src/api.ts
index 0448aa39d8..e965ab21a5 100644
--- a/packages/openapi/src/api.ts
+++ b/packages/openapi/src/api.ts
@@ -4387,6 +4387,8 @@ export interface components {
/** @default false */
pylsp?: boolean;
/** @default false */
+ pyrefly?: boolean;
+ /** @default false */
terminal?: boolean;
/** @default false */
ty?: boolean;
@@ -4560,10 +4562,14 @@ export interface components {
* **Keys.**
*
* - `pylsp`: the pylsp config
+ * - `basedpyright`: the basedpyright config
+ * - `ty`: the ty config
+ * - `pyrefly`: the pyrefly config
*/
LanguageServersConfig: {
basedpyright?: components["schemas"]["BasedpyrightServerConfig"];
pylsp?: components["schemas"]["PythonLanguageServerConfig"];
+ pyrefly?: components["schemas"]["PyreflyLanguageServerConfig"];
ty?: components["schemas"]["TyLanguageServerConfig"];
};
/** LayoutConfig */
@@ -5127,6 +5133,15 @@ export interface components {
schema: string;
tableName: string;
};
+ /**
+ * PyreflyLanguageServerConfig
+ * @description Configuration options for Pyrefly Language Server.
+ *
+ * Pyrefly handles completion, hover, go-to-definition, and diagnostics.
+ */
+ PyreflyLanguageServerConfig: {
+ enabled?: boolean;
+ };
/**
* PythonLanguageServerConfig
* @description Configuration options for Python Language Server.
diff --git a/tests/_server/test_lsp.py b/tests/_server/test_lsp.py
index 8cebdcc899..9454b95994 100644
--- a/tests/_server/test_lsp.py
+++ b/tests/_server/test_lsp.py
@@ -23,6 +23,7 @@ from marimo._server.lsp import (
CompositeLspServer,
CopilotLspServer,
PyLspServer,
+ PyreflyServer,
TyServer,
any_lsp_server_running,
)
@@ -317,11 +318,12 @@ def test_composite_server():
with mock.patch("marimo._server.lsp.DependencyManager") as mock_dm:
mock_dm.pylsp = mock.MagicMock()
mock_dm.pylsp.has.return_value = True
- total_lsp_servers = 4
+ total_lsp_servers = 5
config = LanguageServersConfig(
{
"pylsp": {"enabled": True},
"ty": {"enabled": True},
+ "pyrefly": {"enabled": True},
"basedpyright": {"enabled": True},
}
)
@@ -341,6 +343,7 @@ def test_composite_server():
assert server._is_enabled(config, "pylsp") is True
assert server._is_enabled(config, "copilot") is True
assert server._is_enabled(config, "ty") is True
+ assert server._is_enabled(config, "pyrefly") is True
assert server._is_enabled(config, "basedpyright") is True
# Test with only pylsp
@@ -359,6 +362,7 @@ def test_composite_server():
assert server._is_enabled(config, "pylsp") is True
assert server._is_enabled(config, "copilot") is False
assert server._is_enabled(config, "ty") is False
+ assert server._is_enabled(config, "pyrefly") is False
# Test with only ty enabled
config = LanguageServersConfig(
@@ -366,6 +370,7 @@ def test_composite_server():
"ty": {"enabled": True},
"pylsp": {"enabled": False},
"basedpyright": {"enabled": False},
+ "pyrefly": {"enabled": False},
}
)
completion_config = CompletionConfig(
@@ -383,6 +388,7 @@ def test_composite_server():
assert server._is_enabled(config, "basedpyright") is False
assert server._is_enabled(config, "copilot") is False
assert server._is_enabled(config, "ty") is True
+ assert server._is_enabled(config, "pyrefly") is False
# Test with only basedpyright enabled
config = LanguageServersConfig(
@@ -390,6 +396,7 @@ def test_composite_server():
"basedpyright": {"enabled": True},
"pylsp": {"enabled": False},
"ty": {"enabled": False},
+ "pyrefly": {"enabled": False},
}
)
completion_config = CompletionConfig(
@@ -407,6 +414,33 @@ def test_composite_server():
assert server._is_enabled(config, "basedpyright") is True
assert server._is_enabled(config, "copilot") is False
assert server._is_enabled(config, "ty") is False
+ assert server._is_enabled(config, "pyrefly") is False
+
+ # Test with only pyrefly enabled
+ config = LanguageServersConfig(
+ {
+ "pyrefly": {"enabled": True},
+ "pylsp": {"enabled": False},
+ "basedpyright": {"enabled": False},
+ "ty": {"enabled": False},
+ }
+ )
+ completion_config = CompletionConfig(
+ {
+ "copilot": False,
+ "activate_on_typing": True,
+ "signature_hint_on_typing": False,
+ }
+ )
+ config_reader = as_reader(completion_config, config)
+ config = config_reader.get_config()
+ server = CompositeLspServer(config_reader, min_port=8000)
+ assert len(server.servers) == total_lsp_servers
+ assert server._is_enabled(config, "pylsp") is False
+ assert server._is_enabled(config, "basedpyright") is False
+ assert server._is_enabled(config, "copilot") is False
+ assert server._is_enabled(config, "ty") is False
+ assert server._is_enabled(config, "pyrefly") is True
# Test with nothing enabled
config = LanguageServersConfig({"pylsp": {"enabled": False}})
@@ -442,6 +476,37 @@ def test_basedpyright_server_uses_typed_format():
assert "--stdio" not in lsp_command
+def test_pyrefly_server_uses_typed_format():
+ """Test that pyrefly server uses the new typed format."""
+ from unittest import mock
+
+ # Mock the entire pyrefly module and its find_pyrefly_bin function
+ mock_pyrefly_module = mock.MagicMock()
+ mock_pyrefly_module.get_pyrefly_bin.return_value = (
+ "/path/to/pyrefly/binary"
+ )
+
+ with mock.patch.dict(
+ "sys.modules",
+ {
+ "pyrefly": mock_pyrefly_module,
+ "pyrefly.__main__": mock_pyrefly_module,
+ },
+ ):
+ server = PyreflyServer(port=8000)
+ command = server.get_command()
+
+ # Find the --lsp argument
+ lsp_arg_index = command.index("--lsp")
+ lsp_command = command[lsp_arg_index + 1]
+
+ # Should use typed format
+ assert lsp_command == "pyrefly:/path/to/pyrefly/binary"
+ assert lsp_command.startswith("pyrefly:")
+ # Should NOT contain shell arguments
+ assert "server" not in lsp_command
+
+
def test_ty_server_uses_typed_format():
"""Test that ty server uses the new typed format."""
from unittest import mock