From 00f2e02dd56b643321a92ed0b7503456ec9e61b7 Mon Sep 17 00:00:00 2001 From: FabioRosado Date: Sat, 22 Oct 2022 20:04:59 +0100 Subject: [PATCH] Add more types --- pyscriptjs/package-lock.json | 37 +++++++++++++++++++++++++++ pyscriptjs/package.json | 1 + pyscriptjs/src/components/pywidget.ts | 3 ++- pyscriptjs/src/logger.ts | 10 ++++---- pyscriptjs/src/main.ts | 2 +- pyscriptjs/src/pyodide.ts | 11 +++++--- pyscriptjs/src/runtime.ts | 11 ++++---- pyscriptjs/src/utils.ts | 2 +- 8 files changed, 61 insertions(+), 16 deletions(-) diff --git a/pyscriptjs/package-lock.json b/pyscriptjs/package-lock.json index 1f81b3e6be1..bdb741ed81d 100644 --- a/pyscriptjs/package-lock.json +++ b/pyscriptjs/package-lock.json @@ -22,6 +22,7 @@ "@rollup/plugin-legacy": "2.2.0", "@rollup/plugin-node-resolve": "14.1.0", "@rollup/plugin-typescript": "8.5.0", + "@types/codemirror": "^5.60.5", "@types/jest": "29.1.2", "@types/node": "18.8.3", "@typescript-eslint/eslint-plugin": "5.39.0", @@ -1510,6 +1511,15 @@ "@babel/types": "^7.3.0" } }, + "node_modules/@types/codemirror": { + "version": "5.60.5", + "resolved": "https://registry.npmjs.org/@types/codemirror/-/codemirror-5.60.5.tgz", + "integrity": "sha512-TiECZmm8St5YxjFUp64LK0c8WU5bxMDt9YaAek1UqUb9swrSCoJhh92fWu1p3mTEqlHjhB5sY7OFBhWroJXZVg==", + "dev": true, + "dependencies": { + "@types/tern": "*" + } + }, "node_modules/@types/estree": { "version": "0.0.39", "resolved": "https://registry.npmjs.org/@types/estree/-/estree-0.0.39.tgz", @@ -1628,6 +1638,15 @@ "integrity": "sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==", "dev": true }, + "node_modules/@types/tern": { + "version": "0.23.4", + "resolved": "https://registry.npmjs.org/@types/tern/-/tern-0.23.4.tgz", + "integrity": "sha512-JAUw1iXGO1qaWwEOzxTKJZ/5JxVeON9kvGZ/osgZaJImBnyjyn0cjovPsf6FNLmyGY8Vw9DoXZCMlfMkMwHRWg==", + "dev": true, + "dependencies": { + "@types/estree": "*" + } + }, "node_modules/@types/tough-cookie": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/@types/tough-cookie/-/tough-cookie-4.0.2.tgz", @@ -7364,6 +7383,15 @@ "@babel/types": "^7.3.0" } }, + "@types/codemirror": { + "version": "5.60.5", + "resolved": "https://registry.npmjs.org/@types/codemirror/-/codemirror-5.60.5.tgz", + "integrity": "sha512-TiECZmm8St5YxjFUp64LK0c8WU5bxMDt9YaAek1UqUb9swrSCoJhh92fWu1p3mTEqlHjhB5sY7OFBhWroJXZVg==", + "dev": true, + "requires": { + "@types/tern": "*" + } + }, "@types/estree": { "version": "0.0.39", "resolved": "https://registry.npmjs.org/@types/estree/-/estree-0.0.39.tgz", @@ -7482,6 +7510,15 @@ "integrity": "sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==", "dev": true }, + "@types/tern": { + "version": "0.23.4", + "resolved": "https://registry.npmjs.org/@types/tern/-/tern-0.23.4.tgz", + "integrity": "sha512-JAUw1iXGO1qaWwEOzxTKJZ/5JxVeON9kvGZ/osgZaJImBnyjyn0cjovPsf6FNLmyGY8Vw9DoXZCMlfMkMwHRWg==", + "dev": true, + "requires": { + "@types/estree": "*" + } + }, "@types/tough-cookie": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/@types/tough-cookie/-/tough-cookie-4.0.2.tgz", diff --git a/pyscriptjs/package.json b/pyscriptjs/package.json index 22192398c99..b2f456339d3 100644 --- a/pyscriptjs/package.json +++ b/pyscriptjs/package.json @@ -20,6 +20,7 @@ "@rollup/plugin-legacy": "2.2.0", "@rollup/plugin-node-resolve": "14.1.0", "@rollup/plugin-typescript": "8.5.0", + "@types/codemirror": "^5.60.5", "@types/jest": "29.1.2", "@types/node": "18.8.3", "@typescript-eslint/eslint-plugin": "5.39.0", diff --git a/pyscriptjs/src/components/pywidget.ts b/pyscriptjs/src/components/pywidget.ts index 716e6d5acb3..cbd2f4d9833 100644 --- a/pyscriptjs/src/components/pywidget.ts +++ b/pyscriptjs/src/components/pywidget.ts @@ -1,4 +1,5 @@ import type { Runtime } from '../runtime'; +import type {PyProxy} from "pyodide" import { getLogger } from '../logger'; const logger = getLogger('py-register-widget'); @@ -12,7 +13,7 @@ function createWidget(runtime: Runtime, name: string, code: string, klass: strin name: string = name; klass: string = klass; code: string = code; - proxy: any; + proxy: PyProxy; proxyClass: any; constructor() { diff --git a/pyscriptjs/src/logger.ts b/pyscriptjs/src/logger.ts index 5269685f4e4..63d945c3d5a 100644 --- a/pyscriptjs/src/logger.ts +++ b/pyscriptjs/src/logger.ts @@ -24,10 +24,10 @@ */ interface Logger { - debug(message: string, ...args: any[]): void; - info(message: string, ...args: any[]): void; - warn(message: string, ...args: any[]): void; - error(message: string, ...args: any[]): void; + debug(message: string, ...args: unknown[]): void; + info(message: string, ...args: unknown[]): void; + warn(message: string, ...args: unknown[]): void; + error(message: string, ...args: unknown[]): void; } const _cache = new Map(); @@ -46,7 +46,7 @@ function _makeLogger(prefix: string): Logger { function make(level: string) { const out_fn = console[level].bind(console); - function fn(fmt: string, ...args: any[]) { + function fn(fmt: string, ...args: unknown[]) { out_fn(prefix + fmt, ...args); } return fn diff --git a/pyscriptjs/src/main.ts b/pyscriptjs/src/main.ts index 97e453d3866..f730a4d1595 100644 --- a/pyscriptjs/src/main.ts +++ b/pyscriptjs/src/main.ts @@ -69,7 +69,7 @@ class PyScriptApp { // and show a big error. PRs welcome :) logger.info('searching for '); const elements = document.getElementsByTagName('py-config'); - let el = null; + let el: Element | null = null; if (elements.length > 0) el = elements[0]; if (elements.length >= 2) { diff --git a/pyscriptjs/src/pyodide.ts b/pyscriptjs/src/pyodide.ts index e11b101809f..4d87ac4e2c1 100644 --- a/pyscriptjs/src/pyodide.ts +++ b/pyscriptjs/src/pyodide.ts @@ -1,6 +1,6 @@ import { Runtime } from './runtime'; import { getLogger } from './logger'; -import type { loadPyodide as loadPyodideDeclaration, PyodideInterface } from 'pyodide'; +import type { loadPyodide as loadPyodideDeclaration, PyodideInterface, PyProxy } from 'pyodide'; // eslint-disable-next-line // @ts-ignore import pyscript from './python/pyscript.py'; @@ -10,12 +10,17 @@ declare const loadPyodide: typeof loadPyodideDeclaration; const logger = getLogger('pyscript/pyodide'); +interface Micropip { + install: (packageName: string | string[]) => Promise; + destroy: () => void; +} + export class PyodideRuntime extends Runtime { src: string; name?: string; lang?: string; interpreter: PyodideInterface; - globals: any; + globals: PyProxy; constructor( config: AppConfig, @@ -83,7 +88,7 @@ export class PyodideRuntime extends Runtime { async installPackage(package_name: string | string[]): Promise { if (package_name.length > 0) { logger.info(`micropip install ${package_name.toString()}`); - const micropip = this.globals.get('micropip'); + const micropip = this.globals.get('micropip') as Micropip; await micropip.install(package_name); micropip.destroy(); } diff --git a/pyscriptjs/src/runtime.ts b/pyscriptjs/src/runtime.ts index 7aab55501bf..448656f480c 100644 --- a/pyscriptjs/src/runtime.ts +++ b/pyscriptjs/src/runtime.ts @@ -1,5 +1,5 @@ import type { AppConfig } from './pyconfig'; -import type { PyodideInterface } from 'pyodide'; +import type { PyodideInterface, PyProxy } from 'pyodide'; import { getLogger } from './logger'; const logger = getLogger('pyscript/runtime'); @@ -35,7 +35,7 @@ export abstract class Runtime extends Object { /** * global symbols table for the underlying interpreter. * */ - abstract globals: Map; + abstract globals: PyProxy; constructor(config: AppConfig) { super(); @@ -54,7 +54,7 @@ export abstract class Runtime extends Object { * (asynchronously) which can call its own API behind the scenes. * Python exceptions are turned into JS exceptions. * */ - abstract run(code: string): Promise; + abstract run(code: string): Promise; /** * Same as run, but Python exceptions are not propagated: instead, they @@ -63,9 +63,10 @@ export abstract class Runtime extends Object { * This is a bad API and should be killed/refactored/changed eventually, * but for now we have code which relies on it. * */ - async runButDontRaise(code: string): Promise { + async runButDontRaise(code: string): Promise { return this.run(code).catch(err => { - logger.error(err); + const error = err as Error + logger.error("Error:", error); }); } diff --git a/pyscriptjs/src/utils.ts b/pyscriptjs/src/utils.ts index 7458591b182..595325272d0 100644 --- a/pyscriptjs/src/utils.ts +++ b/pyscriptjs/src/utils.ts @@ -94,7 +94,7 @@ export function inJest(): boolean { return typeof process === 'object' && process.env.JEST_WORKER_ID !== undefined; } -export function globalExport(name: string, obj: any) { +export function globalExport(name: string, obj: object) { // attach the given object to the global object, so that it is globally // visible everywhere. Should be used very sparingly!