Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/pyscript-stubs/js/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from __future__ import annotations

from collections.abc import Buffer, Iterable
from typing import Any

from typing import ClassVar

class Blob:
@classmethod
def new(cls, blobParts: Iterable[Any], options: Any) -> Blob: ...
async def arrayBuffer(self) -> int: ...

class CSSStyleSheet:
@classmethod
def new(cls) -> CSSStyleSheet: ...
def replaceSync(self, text: str) -> None: ...

class Event:
@classmethod
def new(cls, name: str) -> Event: ...

class File(Blob):
name: str = ...

class FileList:
length: int = ...
def item(self, index: int) -> File: ...

class Node:
TEXT_NODE: ClassVar[int] = ...
nodeType: int
nodeValue: str
def append(self, *args: Node | str) -> None: ...

class NodeFilter:
SHOW_TEXT: ClassVar[int] = ...

class Text(Node): ...

class Uint8Array:
@classmethod
def new(cls, bytes_: Buffer) -> Uint8Array: ...

class URL:
@classmethod
def createObjectURL(cls, object: Any) -> str: ...
@classmethod
def revokeObjectURL(cls, url: str) -> None: ...

class Location:
def reload(self) -> None: ...

location: Location
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stub-only packaged should not contain a py.typed marker file.

File renamed without changes.
7 changes: 0 additions & 7 deletions src/pyscript-stubs/pre-existing/__init__.pyi

This file was deleted.

7 changes: 0 additions & 7 deletions src/pyscript-stubs/pre-existing/base.pyi

This file was deleted.

7 changes: 0 additions & 7 deletions src/pyscript-stubs/pre-existing/document_class.pyi

This file was deleted.

9 changes: 0 additions & 9 deletions src/pyscript-stubs/pre-existing/element.pyi

This file was deleted.

23 changes: 0 additions & 23 deletions src/pyscript-stubs/pre-existing/node.pyi

This file was deleted.

16 changes: 0 additions & 16 deletions src/pyscript-stubs/pre-existing/window_class.pyi

This file was deleted.

Empty file.
14 changes: 14 additions & 0 deletions src/pyscript-stubs/pyodide/ffi.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from __future__ import annotations
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l suggest to to add a docstring here that this submodule is (AFAIK) only available python/pyodide and not to MicroPython

(currently it is not yet possible for typecheckers / IDEs to filet on this , so we need to communicate this in a human readable manner.


from js import Node

class ArrayBuffer:
def to_bytes(self) -> bytes: ...

class JsNull: ...

class JsProxy:
nodeType: int
nodeValue: str
def append(self, *args: Node | str) -> None: ...
async def arrayBuffer(self) -> ArrayBuffer: ...
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the should not be any py.typed in stub only packages.

Empty file.
44 changes: 13 additions & 31 deletions src/pyscript-stubs/pyscript/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ PyScript makes available convenience objects, functions and attributes.

These APIs will work with both Pyodide and MicroPython in exactly the same way.

PyScript can run in two contexts: the main browser thread, or on a web worker. T
he following three categories of API functionality explain features that are common for:
PyScript can run in two contexts: the main browser thread, or on a web worker.
The following three categories of API functionality explain features that are common for:
- both main thread and worker,
- main thread only,
- and worker only.
Expand All @@ -16,32 +16,13 @@ he following three categories of API functionality explain features that are com
# Copyright (c) 2020-2025 Jos Verlinde
# MIT Licensed

__all__ = [
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the purpose of removing __all__ ?

removing it will expose module attributes that are not public , or not actually available at runtime.
I know it is a bit of a hassle to add to it , but that is part of the price to pay for stability and reliability,

"PyWorker",
"config",
"current_target",
"display",
"document",
"fetch",
"js_import",
"js_modules",
"py_import",
"storage",
"sync",
"window",
"workers",
"HTML",
"Event",
"WebSocket",
"create_named_worker"
]
from __future__ import annotations


from polyscript import lazy_py_modules as py_import # type: ignore
from pyscript.display import HTML as HTML, display as display
from pyscript.events import Event as Event, when as when
from pyscript.fetch import fetch as fetch
from pyscript.magic_js import (
from .polyscript import lazy_py_modules as py_import
from .display import HTML as HTML, display as display
from .events import Event as Event, when as when
from .fetch import fetch as fetch
from .magic_js import (
RUNNING_IN_WORKER as RUNNING_IN_WORKER,
PyWorker as PyWorker,
config as config,
Expand All @@ -50,12 +31,13 @@ from pyscript.magic_js import (
js_import as js_import,
sync as sync,
window as window,
js_modules as js_modules
js_modules as js_modules,
TreeWalker as TreeWalker,
)

from pyscript.storage import Storage as Storage, storage as storage
from pyscript.websocket import WebSocket as WebSocket
from pyscript.workers import create_named_worker as create_named_worker, workers as workers
from .storage import Storage as Storage, storage as storage
from .websocket import WebSocket as WebSocket
from .workers import create_named_worker as create_named_worker, workers as workers

if not RUNNING_IN_WORKER:
...
20 changes: 12 additions & 8 deletions src/pyscript-stubs/pyscript/_pyscript.pyi
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
# Copyright (c) 2020-2025 Jos Verlinde
# MIT Licensed

from typing import Any, Callable
from __future__ import annotations

from collections.abc import Callable
from typing import Any

from _typeshed import Incomplete

def js_import(name: str) -> JSModule:
"""Module level __getattr__ that returns an JSModule object for any requested attribute."""
...

class JSModule:
def __init__(self, name) -> None: ...
def __getattr__(self, field) -> Any | None: ...
def __init__(self, name: str) -> None: ...
def __getattr__(self, field: str) -> Any | None: ...

class Worker:
async def sync(self) -> Callable: ...
async def sync(self) -> Callable[[Any], Any]: ...

class XWorker(Worker):
# https://pyscript.github.io/polyscript/#xworker-options
Expand All @@ -24,16 +28,16 @@ class XWorker(Worker):
def __init__(
self,
file: str,
a_sync: bool = True,
config: str = "",
type: str = "pyodide", # pyodide, micropython, ruby-wasm-wasi, wasmoon, webr
a_sync: bool = ...,
config: str = ...,
type: str = ..., # pyodide, micropython, ruby-wasm-wasi, wasmoon, webr
version: str = ...,
serviceWorker: str = ...,
) -> None: ...

# def isWindowProxy(self, ref:Incomplete) -> bool: ...

class PyWorker(XWorker):
def __init__(self, name) -> None: ...
def __init__(self, name: str) -> None: ...

xworker: XWorker = ...
2 changes: 2 additions & 0 deletions src/pyscript-stubs/pyscript/_typeshed.pyi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the _typeshed.pyi stub is already available in all typecheckers and should not be overwritten from this package.
doing that will likely disrupt all static typecheckers and IDE;s

ref : https://github.com/python/typeshed/blob/main/stdlib/_typeshed/__init__.pyi

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

class Incomplete: ...
10 changes: 8 additions & 2 deletions src/pyscript-stubs/pyscript/display.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ see: https://docs.pyscript.net/2025.2.3/api/
# Copyright (c) 2020-2025 Jos Verlinde
# MIT Licensed

from __future__ import annotations

from typing import Any

from .web import Element

_MIME_METHODS = ...
_MIME_RENDERERS = ...

class HTML:
"""
Wrap a string so that display() can render it as plain HTML
"""
def __init__(self, html) -> None: ...
def __init__(self, html: str) -> None: ...

def display(*values, target=None, append: bool = True) -> None:
def display(*values: Any, target: Element | None = ..., append: bool = True) -> None:
"""
A function used to display content. The function is intelligent enough to introspect the object[s] it is passed and work out how to correctly
display the object[s] in the web page based on the following mime types::
Expand Down
15 changes: 10 additions & 5 deletions src/pyscript-stubs/pyscript/events.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,39 @@ see: https://docs.pyscript.net/2025.2.3/api/
# Copyright (c) 2020-2025 Jos Verlinde
# MIT Licensed

from __future__ import annotations

from collections.abc import Callable
from typing import Any

class Event:
"""
Represents something that may happen at some point in the future.
"""

def __init__(self) -> None: ...
def trigger(self, result) -> None:
def trigger(self, result: Any) -> None:
"""
Trigger the event with a result to pass into the handlers.
"""
...

def add_listener(self, listener) -> None:
def add_listener(self, listener: Callable[[Event], Any]) -> None:
"""
Add a callable/awaitable to listen to when this event is triggered.
"""
...

def remove_listener(self, *args) -> None:
def remove_listener(self, *args: Callable[[Event], Any]) -> None:
"""
Clear the specified handler functions in *args. If no handlers
provided, clear all handlers.
"""
...

def when(
target, *args, **kwargs
): # -> _Wrapped[Callable[..., Any], Any, Callable[..., Any], CoroutineType[Any, Any, Any]] | Callable[..., _Wrapped[Callable[..., Any], Any, Callable[..., Any], CoroutineType[Any, Any, Any]]]:
target: str, *args: Any, **kwargs: Any
) -> Callable[..., Any]: # -> _Wrapped[Callable[..., Any], Any, Callable[..., Any], CoroutineType[Any, Any, Any]] | Callable[..., _Wrapped[Callable[..., Any], Any, Callable[..., Any], CoroutineType[Any, Any, Any]]]:
"""
Add an event listener to the target element(s) for the specified event type.

Expand Down
7 changes: 6 additions & 1 deletion src/pyscript-stubs/pyscript/fetch.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ see: https://docs.pyscript.net/2025.2.3/api/
# Copyright (c) 2020-2025 Jos Verlinde
# MIT Licensed

from typing import Any, Awaitable
from __future__ import annotations

from builtins import bytearray

from collections.abc import Awaitable
from typing import Any

class _Response:
"""
Expand Down
5 changes: 4 additions & 1 deletion src/pyscript-stubs/pyscript/ffi.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ see: https://docs.pyscript.net/2025.2.3/api/
# Copyright (c) 2020-2025 Jos Verlinde
# MIT Licensed

from typing import Any, Callable, Iterable, Mapping
from __future__ import annotations

from collections.abc import Callable, Iterable, Mapping
from typing import Any

def from_entries(iterable: Iterable[tuple[str, Any]]) -> Any:
"""
Expand Down
5 changes: 4 additions & 1 deletion src/pyscript-stubs/pyscript/flatted.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ see: https://docs.pyscript.net/2025.2.3/api/
# Copyright (c) 2020-2025 Jos Verlinde
# MIT Licensed

from typing import Any, Callable
from __future__ import annotations

from collections.abc import Callable
from typing import Any

class _Known:
"""
Expand Down
6 changes: 4 additions & 2 deletions src/pyscript-stubs/pyscript/fs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ allowing mounting, syncing, and unmounting of virtual filesystems.
# Copyright (c) 2020-2025 Jos Verlinde
# MIT Licensed

from typing import Any, Dict, Literal
from __future__ import annotations

mounted: Dict[str, Dict[str, Any]]
from typing import Any, Literal

mounted: dict[str, dict[str, Any]]
"""Dictionary mapping mount points to filesystem information"""

async def mount(
Expand Down
Loading