Skip to content

Commit

Permalink
Add more types
Browse files Browse the repository at this point in the history
  • Loading branch information
FabioRosado committed Oct 22, 2022
1 parent 4595c79 commit d90bebd
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 20 deletions.
37 changes: 37 additions & 0 deletions pyscriptjs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyscriptjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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",
Expand Down
3 changes: 1 addition & 2 deletions pyscriptjs/src/components/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Runtime } from '../runtime';
import { getLogger } from '../logger';

type ImportType = { [key: string]: unknown }
type ImportMapType = {
export type ImportMapType = {
imports: ImportType | null
}

Expand All @@ -17,7 +17,6 @@ interface PyscriptElement extends Element{
}



const logger = getLogger('pyscript/base');

let Element: PyscriptElement;
Expand Down
5 changes: 3 additions & 2 deletions pyscriptjs/src/components/pyscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {

import { getAttribute, addClasses, htmlDecode } from '../utils';
import { BaseEvalElement } from './base';
import type {ImportMapType} from "./base"
import type { Runtime } from '../runtime';
import { getLogger } from '../logger';

Expand Down Expand Up @@ -87,7 +88,7 @@ export class PyScript extends BaseEvalElement {
} catch {
return null;
}
})();
})() as ImportMapType | null;

if (importmap?.imports == null) continue;

Expand All @@ -98,7 +99,7 @@ export class PyScript extends BaseEvalElement {
try {
// XXX: pyodide doesn't like Module(), failing with
// "can't read 'name' of undefined" at import time
exports = { ...(await import(url)) };
exports = { ...(await import(url)) } as object;
} catch {
logger.warn(`failed to fetch '${url}' for '${name}'`);
continue;
Expand Down
3 changes: 2 additions & 1 deletion pyscriptjs/src/components/pywidget.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Runtime } from '../runtime';
import type {PyProxy} from "pyodide"
import { getLogger } from '../logger';

const logger = getLogger('py-register-widget');
Expand All @@ -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() {
Expand Down
10 changes: 5 additions & 5 deletions pyscriptjs/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Logger>();
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pyscriptjs/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class PyScriptApp {
// and show a big error. PRs welcome :)
logger.info('searching for <py-config>');
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) {
Expand Down
11 changes: 8 additions & 3 deletions pyscriptjs/src/pyodide.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Runtime } from './runtime';
import { getLastPath } from './utils';
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';
Expand All @@ -11,12 +11,17 @@ declare const loadPyodide: typeof loadPyodideDeclaration;

const logger = getLogger('pyscript/pyodide');

interface Micropip {
install: (packageName: string | string[]) => Promise<void>;
destroy: () => void;
}

export class PyodideRuntime extends Runtime {
src: string;
name?: string;
lang?: string;
interpreter: PyodideInterface;
globals: any;
globals: PyProxy;

constructor(
config: AppConfig,
Expand Down Expand Up @@ -84,7 +89,7 @@ export class PyodideRuntime extends Runtime {
async installPackage(package_name: string | string[]): Promise<void> {
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();
}
Expand Down
11 changes: 6 additions & 5 deletions pyscriptjs/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -35,7 +35,7 @@ export abstract class Runtime extends Object {
/**
* global symbols table for the underlying interpreter.
* */
abstract globals: Map<string, unknown>;
abstract globals: PyProxy;

constructor(config: AppConfig) {
super();
Expand All @@ -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<any>;
abstract run(code: string): Promise<unknown>;

/**
* Same as run, but Python exceptions are not propagated: instead, they
Expand All @@ -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<any> {
async runButDontRaise(code: string): Promise<unknown> {
return this.run(code).catch(err => {
logger.error(err);
const error = err as Error
logger.error("Error:", error);
});
}

Expand Down
2 changes: 1 addition & 1 deletion pyscriptjs/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,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!

Expand Down

0 comments on commit d90bebd

Please sign in to comment.