{"id":15417,"library":"browser-basedpyright","title":"Browser-based Pyright","description":"This package provides a browser-compatible build of `basedpyright`, an advanced static type checker for Python. As a fork of Microsoft's Pyright, `basedpyright` offers robust type analysis, diagnostic reporting, and language server capabilities. `browser-basedpyright` enables the integration of these features directly into web environments, such as online Python playgrounds, web-based IDEs, and educational tools, without requiring a Node.js backend. It follows a rapid release cadence, frequently updating to incorporate the latest upstream Pyright features and bug fixes, with the current stable version being `1.39.3`. Its primary differentiator is enabling comprehensive Python type checking entirely within the client-side browser context.","status":"active","version":"1.39.3","language":"javascript","source_language":"en","source_url":"https://github.com/detachhead/basedpyright","tags":["javascript"],"install":[{"cmd":"npm install browser-basedpyright","lang":"bash","label":"npm"},{"cmd":"yarn add browser-basedpyright","lang":"bash","label":"yarn"},{"cmd":"pnpm add browser-basedpyright","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is primarily consumed in browser ESM environments; CommonJS `require` is generally not supported for client-side usage.","wrong":"const { createLanguageService } = require('browser-basedpyright');","symbol":"createLanguageService","correct":"import { createLanguageService } from 'browser-basedpyright';"},{"note":"A named export for creating an in-memory host, crucial for providing virtual file system capabilities to the language service.","wrong":"import createInMemoryLanguageServiceHost from 'browser-basedpyright/host';","symbol":"createInMemoryLanguageServiceHost","correct":"import { createInMemoryLanguageServiceHost } from 'browser-basedpyright';"},{"note":"This is a TypeScript type/interface definition. It should be imported using `import type` to prevent bundling issues or runtime errors if not tree-shaken.","wrong":"import { LanguageService } from 'browser-basedpyright';","symbol":"LanguageService","correct":"import type { LanguageService } from 'browser-basedpyright';"}],"quickstart":{"code":"import { createLanguageService, createInMemoryLanguageServiceHost, type LanguageService } from 'browser-basedpyright';\n\n// --- Setup the host for the language service ---\n// This host simulates a file system and provides content for the language service.\nconst pythonCode = `\nclass MyClass:\n    def __init__(self, name: str):\n        self.name = name\n\n    def greet(self) -> str:\n        return f\"Hello, {self.name}!\"\n\ndef process_data(value: int):\n    # This should be flagged if 'value' is not an int or if 'value' is a str\n    if value == \"test\": # This will cause a pyright warning\n        print(\"Test string\")\n\nobj = MyClass(\"World\")\nprint(obj.greet())\n\nprocess_data(123)\nprocess_data(\"hello\") # Pyright should flag this as type error\n`;\n\nconst virtualFilePath = '/src/main.py';\n\nconst host = createInMemoryLanguageServiceHost({\n  getOpenFileContents: (path: string) => {\n    if (path === virtualFilePath) {\n      return pythonCode;\n    }\n    return undefined; // Or throw error for unknown files\n  },\n  getWorkspaceFiles: () => [virtualFilePath],\n  getPythonVersion: () => '3.10',\n  getPyrightConfig: () => ({\n    reportMissingImports: \"warning\",\n    reportUnknownMemberType: \"warning\"\n  }),\n});\n\n// --- Initialize the language service ---\nconst service: LanguageService = createLanguageService(host);\n\n// --- Get diagnostics for the file ---\nasync function getAndPrintDiagnostics() {\n  const diagnostics = await service.getDiagnostics(virtualFilePath);\n  console.log(`Diagnostics for ${virtualFilePath}:`);\n  if (diagnostics.length === 0) {\n    console.log(\"No issues found.\");\n  } else {\n    diagnostics.forEach(d => {\n      console.log(`  [${d.severity}] ${d.message} at line ${d.range.start.line + 1}, col ${d.range.start.character + 1}`);\n    });\n  }\n}\n\ngetAndPrintDiagnostics();\n\n// Example of updating content and re-checking\nsetTimeout(async () => {\n    const updatedCode = `\nclass AnotherClass:\n    def say_hi(self):\n        print(\"Hi!\")\n\nanother_obj = AnotherClass()\nanother_obj.say_hi()\n\n# Fix the previous error\ndef process_data_fixed(value: int):\n    if value == 1:\n        print(\"One\")\n\nprocess_data_fixed(42)\n# process_data_fixed(\"oops\") // This would still be an error if uncommented\n`;\n    host.setOpenFileContents(virtualFilePath, updatedCode);\n    console.log(\"\\n--- Updated code and re-checking ---\");\n    await getAndPrintDiagnostics();\n}, 2000);","lang":"typescript","description":"This example demonstrates how to initialize the `browser-basedpyright` language service in a browser environment, provide it with virtual Python code, and retrieve type diagnostics. It also shows how to update file content and re-evaluate diagnostics."},"warnings":[{"fix":"Immediately update to `browser-basedpyright@1.39.3` or later to restore functionality. Avoid `v1.39.2` entirely.","message":"The `browser-basedpyright` package was accidentally published as an empty package in `v1.39.2`, rendering it unusable. Users updating to this specific version would encounter runtime errors or missing exports.","severity":"breaking","affected_versions":"1.39.2"},{"fix":"Review your Python codebase for instances where abstract base classes with no abstract methods are instantiated. If intended, consider removing the `ABC` inheritance or ensuring concrete methods are implemented.","message":"A new diagnostic rule, `reportEmptyAbstractUsage`, was introduced in `v1.39.0`. This rule will now report errors for Python classes explicitly extending `ABC` (or using `ABCMeta`) without abstract methods if the class itself is then instantiated, which was previously allowed without complaint.","severity":"gotcha","affected_versions":">=1.39.0"},{"fix":"Ensure you are using `browser-basedpyright@1.38.1` or a newer version to benefit from stability improvements and avoid crashes related to abstract setter definitions.","message":"A critical bug causing the language server to crash on specific Python code involving abstract setters without parameters was fixed in `v1.38.1`. Users on earlier versions might experience instability or crashes when processing such Python code.","severity":"gotcha","affected_versions":"<1.38.1"},{"fix":"It is advisable to pin exact versions or thoroughly test updates to avoid unexpected behavior changes in your application. Monitor release notes for any explicit API changes.","message":"Due to the package's rapid release cycle and its nature as a browser build mirroring `basedpyright` (which itself tracks upstream Pyright), API stability might not be strictly guaranteed across minor versions. While efforts are made to maintain compatibility, unexpected behavior or minor API adjustments may occur.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Ensure `browser-basedpyright` is installed and updated to `v1.39.3` or later. Verify the import statement: `import { createLanguageService } from 'browser-basedpyright';`.","cause":"The `browser-basedpyright` package was either not correctly installed, an incorrect import path/syntax was used, or the problematic `v1.39.2` version was installed which contained no exports.","error":"TypeError: createLanguageService is not a function"},{"fix":"Review the Python code for type correctness. For example, change a call like `process_data(\"hello\")` to `process_data(123)` if the parameter expects an integer.","cause":"This is a Pyright diagnostic indicating a type mismatch in the Python code being analyzed. The language service correctly identified that a string value was provided where an integer was expected.","error":"Pyright: Incompatible types in assignment (expression of type 'str' cannot be assigned to type 'int')"},{"fix":"Configure your web server to send `Cross-Origin-Opener-Policy: same-origin` and `Cross-Origin-Embedder-Policy: require-corp` HTTP response headers for the HTML page hosting the application and potentially for the worker script itself.","cause":"Web Workers, especially those utilizing shared memory features for performance (common in language servers), require specific Cross-Origin Isolation (COOP/COEP) headers to be served by the web server. Without these, `SharedArrayBuffer` might not be available in the worker context.","error":"ReferenceError: SharedArrayBuffer is not defined"}],"ecosystem":"npm"}