diff --git a/.gitignore b/.gitignore index edd9d60..779d236 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ build/ dist/ +.* +*.egg-info diff --git a/pyproject.toml b/pyproject.toml index ea88bca..7337833 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,9 +4,10 @@ build-backend = "setuptools.build_meta" [project] name = "pyscript_stubs" -version = "0.0.1" +version = "0.0.2" authors = [ { name="Greger Stolt Nilsen", email="gregersn@gmail.com" }, + { name="Jos Verlinde", email="jos_verlinde@hotmail.com" }, ] description = "Typing stubs for Pyscript" readme = "README.md" @@ -14,9 +15,18 @@ requires-python = ">=3.8" classifiers = [ "Programming Language :: Python :: 3", "Operating System :: OS Independent", - "License :: OSI Approved :: MIT License", ] - +license = "MIT AND Apache-2.0" [project.urls] -Homepage = "https://github.com/gregersn/pyscript-stubs" -Issues = "https://github.com/gregersn/pyscript-stubs/issues" \ No newline at end of file +Homepage = "https://github.com/pyscript/pyscript-stubs" +Issues = "https://github.com/pyscript/pyscript-stubs/issues" + +[tool.setuptools] +include-package-data = true + +[tool.setuptools.packages.find] +where = ["."] +include = ["pyscript*"] + +[tool.setuptools.package-data] +pyscript = ["**/*.pyi"] \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..3dcd31f --- /dev/null +++ b/readme.md @@ -0,0 +1,10 @@ +# PyScript Stubs + +Because there is no `pyscript` package to install in the Python environment +used by your IDE, there are no type hints for the IDE to use for code +analysis while coding a PyScript project. + +This project contains [stub files](https://typing.python.org/en/latest/spec/distributing.html#stub-files) +for PyScript and related projects (for example, MicroPython). + +It is a work in progress. diff --git a/src/pyscript-stubs/node.pyi b/src/pyscript-stubs/node.pyi deleted file mode 100644 index 2505ea8..0000000 --- a/src/pyscript-stubs/node.pyi +++ /dev/null @@ -1,23 +0,0 @@ -from typing import Optional -from .base import EventTarget -from .document_class import Document -from .element import Element - -class Node(EventTarget): - baseURI: str - childNodes: list['Node'] - firstChild: 'Node' - isConnected: bool - lastChild: 'Node' - nextSibling: 'Node' - nodeName: str - nodeType: int - nodeValue: any - ownerDocument: 'Document' - parentNode: Optional['Node'] - parentElement: Optional['Element'] - previousSibling: Optional['Node'] - textContent: str - - def appendChild(self, childNode: 'Node'): ... - def cloneNode(self) -> 'Node': ... \ No newline at end of file diff --git a/src/pyscript-stubs/__init__.pyi b/src/pyscript-stubs/pre-existing/__init__.pyi similarity index 100% rename from src/pyscript-stubs/__init__.pyi rename to src/pyscript-stubs/pre-existing/__init__.pyi diff --git a/src/pyscript-stubs/base.pyi b/src/pyscript-stubs/pre-existing/base.pyi similarity index 87% rename from src/pyscript-stubs/base.pyi rename to src/pyscript-stubs/pre-existing/base.pyi index 11297c4..7607ef6 100644 --- a/src/pyscript-stubs/base.pyi +++ b/src/pyscript-stubs/pre-existing/base.pyi @@ -1,7 +1,7 @@ class CSSStyleDeclaration: display: str -class EventTarget(): +class EventTarget: def addEventListener(self): ... def removeEventListener(self): ... def dispatchEvent(self): ... diff --git a/src/pyscript-stubs/document_class.pyi b/src/pyscript-stubs/pre-existing/document_class.pyi similarity index 100% rename from src/pyscript-stubs/document_class.pyi rename to src/pyscript-stubs/pre-existing/document_class.pyi diff --git a/src/pyscript-stubs/element.pyi b/src/pyscript-stubs/pre-existing/element.pyi similarity index 73% rename from src/pyscript-stubs/element.pyi rename to src/pyscript-stubs/pre-existing/element.pyi index 69d5d8a..07a03ee 100644 --- a/src/pyscript-stubs/element.pyi +++ b/src/pyscript-stubs/pre-existing/element.pyi @@ -2,10 +2,8 @@ from . import CSSStyleDeclaration from node import Node class Element(Node): - classList: list[str] - onclick: callable - + classList: list[str] + onclick: callable class HTMLElement(Element): style: CSSStyleDeclaration - diff --git a/src/pyscript-stubs/pre-existing/node.pyi b/src/pyscript-stubs/pre-existing/node.pyi new file mode 100644 index 0000000..15f9f2c --- /dev/null +++ b/src/pyscript-stubs/pre-existing/node.pyi @@ -0,0 +1,23 @@ +from typing import Optional +from .base import EventTarget +from .document_class import Document +from .element import Element + +class Node(EventTarget): + baseURI: str + childNodes: list["Node"] + firstChild: "Node" + isConnected: bool + lastChild: "Node" + nextSibling: "Node" + nodeName: str + nodeType: int + nodeValue: any + ownerDocument: "Document" + parentNode: Optional["Node"] + parentElement: Optional["Element"] + previousSibling: Optional["Node"] + textContent: str + + def appendChild(self, childNode: "Node"): ... + def cloneNode(self) -> "Node": ... diff --git a/src/pyscript-stubs/py.typed b/src/pyscript-stubs/pre-existing/py.typed similarity index 100% rename from src/pyscript-stubs/py.typed rename to src/pyscript-stubs/pre-existing/py.typed diff --git a/src/pyscript-stubs/window_class.pyi b/src/pyscript-stubs/pre-existing/window_class.pyi similarity index 91% rename from src/pyscript-stubs/window_class.pyi rename to src/pyscript-stubs/pre-existing/window_class.pyi index a75de2f..862f8e5 100644 --- a/src/pyscript-stubs/window_class.pyi +++ b/src/pyscript-stubs/pre-existing/window_class.pyi @@ -13,4 +13,4 @@ class FileReader(EventTarget): class Window(EventTarget): console: Console FileReader: FileReader - location: Location \ No newline at end of file + location: Location diff --git a/src/pyscript-stubs/pyscript/__init__.pyi b/src/pyscript-stubs/pyscript/__init__.pyi new file mode 100644 index 0000000..4e7a204 --- /dev/null +++ b/src/pyscript-stubs/pyscript/__init__.pyi @@ -0,0 +1,61 @@ +""" +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: + - both main thread and worker, + - main thread only, + - and worker only. + + Most features work in both contexts in exactly the same manner, but please be aware that some are specific to either the main thread + or a worker context. + +""" +# Copyright (c) 2020-2025 Jos Verlinde +# MIT Licensed + +__all__ = [ + "PyWorker", + "config", + "current_target", + "display", + "document", + "fetch", + "js_import", + "js_modules", + "py_import", + "storage", + "sync", + "window", + "workers", + "HTML", + "Event", + "WebSocket", + "create_named_worker" +] + + +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 ( + RUNNING_IN_WORKER as RUNNING_IN_WORKER, + PyWorker as PyWorker, + config as config, + current_target as current_target, + document as document, + js_import as js_import, + sync as sync, + window as window, + js_modules as js_modules +) + +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 + +if not RUNNING_IN_WORKER: + ... diff --git a/src/pyscript-stubs/pyscript/_pyscript.pyi b/src/pyscript-stubs/pyscript/_pyscript.pyi new file mode 100644 index 0000000..b47d1aa --- /dev/null +++ b/src/pyscript-stubs/pyscript/_pyscript.pyi @@ -0,0 +1,39 @@ +# Copyright (c) 2020-2025 Jos Verlinde +# MIT Licensed + +from typing import Any, Callable +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: ... + +class Worker: + async def sync(self) -> Callable: ... + +class XWorker(Worker): + # https://pyscript.github.io/polyscript/#xworker-options + + polyfill: bool = False + window: Incomplete = ... + + def __init__( + self, + file: str, + a_sync: bool = True, + config: str = "", + type: str = "pyodide", # 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: ... + +xworker: XWorker = ... diff --git a/src/pyscript-stubs/pyscript/display.pyi b/src/pyscript-stubs/pyscript/display.pyi new file mode 100644 index 0000000..474f646 --- /dev/null +++ b/src/pyscript-stubs/pyscript/display.pyi @@ -0,0 +1,34 @@ +""" +Pyscript 2025.2.3 type-stub. +see: https://docs.pyscript.net/2025.2.3/api/ +""" +# Copyright (c) 2020-2025 Jos Verlinde +# MIT Licensed + +_MIME_METHODS = ... +_MIME_RENDERERS = ... + +class HTML: + """ + Wrap a string so that display() can render it as plain HTML + """ + def __init__(self, html) -> None: ... + +def display(*values, target=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:: + - text/plain to show the content as text + - text/html to show the content as HTML + - image/png to show the content as + - image/jpeg to show the content as + - image/svg+xml to show the content as + - application/json to show the content as JSON + - application/javascript to put the content in