8000 fix: Linting, documentation, JSR, CI, etc. (#81) · denosaurs/deno_python@99c6d09 · GitHub
[go: up one dir, main page]

Skip to content

Commit 99c6d09

Browse files
fix: Linting, documentation, JSR, CI, etc. (#81)
1 parent 93fa14f commit 99c6d09

File tree

9 files changed

+103
-47
lines changed

9 files changed

+103
-47
lines changed

.github/workflows/checks.yml

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ jobs:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- name: Checkout sources
14-
uses: actions/checkout@v2
14+
uses: actions/checkout@v4
1515

1616
- name: Setup latest deno version
17-
uses: denoland/setup-deno@v1
18-
with:
19-
deno-version: v1.x
17+
uses: denoland/setup-deno@v2
2018

2119
- name: Run deno fmt
2220
run: deno fmt --check
@@ -28,31 +26,26 @@ jobs:
2826
runs-on: ubuntu-latest
2927
steps:
3028
- name: Checkout sources
31-
uses: actions/checkout@v2
29+
uses: actions/checkout@v4
3230

3331
- name: Setup latest deno version
34-
uses: denoland/setup-deno@v1
35-
with:
36-
deno-version: v1.x
32+
uses: denoland/setup-deno@v2
3733

3834
- name: Run deno task check
3935
run: deno task check
4036

41-
4237
test:
4338
name: test ${{ matrix.os }}
4439
runs-on: ${{ matrix.os }}
4540
strategy:
4641
matrix:
47-
os: [windows-latest, ubuntu-latest, macos-latest,]
42+
os: [windows-latest, ubuntu-latest, macos-latest]
4843
steps:
4944
- name: Checkout sources
5045
uses: actions/checkout@v2
5146

5247
- name: Setup latest deno version
53-
uses: denoland/setup-deno@v1
54-
with:
55-
deno-version: v1.x
48+
uses: denoland/setup-deno@v2
5649

5750
- name: Setup Bun
5851
if: ${{ matrix.os != 'windows-latest' }}
@@ -62,7 +55,7 @@ jobs:
6255
uses: actions/setup-python@v2
6356
if: ${{ matrix.os == 'windows-latest' }}
6457
with:
65-
python-version: '3.13'
58+
python-version: "3.13"
6659

6760
- name: Install NumPy
6861
if: ${{ matrix.os != 'macos-latest' }}

.github/workflows/publish.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Publish
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
publish:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: read
13+
id-token: write
14+
steps:
15+
- name: Checkout sources
16+
uses: actions/checkout@v4
17+
18+
- name: Setup latest deno version
19+
uses: denoland/setup-deno@v2
20+
21+
- name: Publish to JSR
22+
run: deno publish

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,29 @@ the Python dynamic library, which is like `python310.dll` (Windows),
117117
`libpython310.dylib` (macOS) and `libpython310.so` (Linux) depending on
118118
platform.
119119

120+
## Usage with docker
121+
122+
Usage with docker is easiest done using the
123+
[`denoland/deno:bin` image](https://github.com/denoland/deno_docker?tab=readme-ov-file#using-your-own-base-image)
124+
along with the [official `python` image](https://hub.docker.com/_/python/).
125+
126+
```Dockerfile
127+
ARG DENO_VERSION=1.38.2
128+
ARG PYTHON_VERSION=3.12
129+
130+
FROM denoland/deno:bin-$DENO_VERSION AS deno
131+
FROM python:$PYTHON_VERSION
132+
133+
# Copy and configure deno
134+
COPY --from=deno /deno /usr/local/bin/deno
135+
ENTRYPOINT ["/usr/local/bin/deno"]
136+
137+
# Copy your project source
138+
COPY . .
139+
140+
RUN ["run", "-A", "--unstable", "https://deno.land/x/python@0.4.2/examples/hello_python.ts"]
141+
```
142+
120143
## Maintainers
121144

122145
- DjDeveloper ([@DjDeveloperr](https://github.com/DjDeveloperr))

deno.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
{
2+
"name": "@denosaurs/python",
3+
"version": "0.4.4",
4+
"exports": {
5+
".": "./mod.ts",
6+
"./ext/pip": "./ext/pip.ts"
7+
},
28
"tasks": {
39
"check": "deno task check:mod && deno task check:ext && deno task check:examples",
410
"check:mod": "deno check --unstable-ffi mod.ts",

ext/pip.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ export class Pip {
118118
*
119119
* ```
120120
*/
121-
async import(module: string, entrypoint?: string) {
121+
// deno-lint-ignore no-explicit-any
122+
async import(module: string, entrypoint?: string): Promise<any> {
122123
const { name } = getModuleNameAndVersion(module);
123124

124125
await this.install(module);
@@ -166,5 +167,5 @@ export class Pip {
166167
}
167168
}
168169

169-
export const pip = new Pip();
170+
export const pip: Pip = new Pip();
170171
export default pip;

ipy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import py, { Python } from "./mod.ts";
2-
import { Pip, pip } from "./ext/pip.ts";
1+
import py, { type Python } from "./mod.ts";
2+
import { type Pip, pip } from "./ext/pip.ts";
33

44
declare global {
55
const py: Python;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bunpy",
3-
"version": "0.3.3",
3+
"version": "0.4.4",
44
"description": "JavaScript -> Python Bridge for Deno and Bun",
55
"main": "mod.bun.ts",
66
"directories": {

src/python.ts

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,10 @@ export function kw(
138138
* ```
139139
*/
140140
export class Callback {
141-
unsafe;
141+
unsafe: Deno.UnsafeCallback<{
142+
parameters: ["pointer", "pointer", "pointer"];
143+
result: "pointer";
144+
}>;
142145

143146
constructor(public callback: PythonJSCallback) {
144147
this.unsafe = new Deno.UnsafeCallback(
@@ -200,7 +203,7 @@ export class PyObject {
200203
/**
201204
* Check if the object is NULL (pointer) or None type in Python.
202205
*/
203-
get isNone() {
206+
get isNone(): boolean {
204207
// deno-lint-ignore ban-ts-comment
205208
// @ts-expect-error
206209
return this.handle === null || this.handle === 0 ||
@@ -403,9 +406,17 @@ export class PyObject {
403406
/**
404407
* Performs an equals operation on the Python object.
405408
*/
406-
equals(rhs: PythonConvertible) {
409+
equals(rhs: PythonConvertible): boolean {
407410
const rhsObject = PyObject.from(rhs);
408-
return py.PyObject_RichCompareBool(this.handle, rhsObject.handle, 3);
411+
const comparison = py.PyObject_RichCompareBool(
412+
this.handle,
413+
rhsObject.handle,
414+
3,
415+
);
416+
if (comparison === -1) {
417+
maybeThrowError();
418+
}
419+
return comparison === 1;
409420
}
410421

411422
/**
@@ -590,7 +601,7 @@ export class PyObject {
590601
/**
591602
* Tries to set the attribute, throws an error otherwise.
592603
*/
593-
setAttr(name: string, v: PythonConvertible) {
604+
setAttr(name: string, v: PythonConvertible): void {
594605
if (
595606
py.PyObject_SetAttrString(
596607
this.handle,
@@ -603,43 +614,43 @@ export class PyObject {
603614
}
604615

605616
/** Checks if Python object has an attribute of given name. */
606-
hasAttr(attr: string) {
617+
hasAttr(attr: string): boolean {
607618
return py.PyObject_HasAttrString(this.handle, cstr(attr)) !== 0;
608619
}
609620

610621
/**
611622
* Casts a Bool Python object as JS Boolean value.
612623
*/
613-
asBoolean() {
624+
asBoolean(): boolean {
614625
return py.PyLong_AsLong(this.handle) === 1;
615626
}
616627

617628
/**
618629
* Casts a Int Python object as JS Number value.
619630
*/
620-
asLong() {
631+
asLong(): number {
621632
return py.PyLong_AsLong(this.handle) as number;
622633
}
623634

624635
/**
625636
* Casts a Float (Double) Python object as JS Number value.
626637
*/
627-
asDouble() {
638+
asDouble(): number {
628639
return py.PyFloat_AsDouble(this.handle) as number;
629640
}
630641

631642
/**
632643
* Casts a String Python object as JS String value.
633644
*/
634-
asString() {
645+
asString(): string | null {
635646
const str = py.PyUnicode_AsUTF8(this.handle);
636647
return str !== null ? Deno.UnsafePointerView.getCString(str) : null;
637648
}
638649

639650
/**
640651
* Casts a List Python object as JS Array value.
641652
*/
642-
asArray() {
653+
asArray(): PythonConvertible[] {
643654
const array: PythonConvertible[] = [];
644655
for (const i of this) {
645656
array.push(i.valueOf());
@@ -653,7 +664,7 @@ export class PyObject {
653664
* Note: `from` supports converting both Map and Object to Python Dict.
654665
* But this only supports returning a Map.
655666
*/
656-
asDict() {
667+
asDict(): Map<PythonConvertible, PythonConvertible> {
657668
const dict = new Map<PythonConvertible, PythonConvertible>();
658669
const keys = py.PyDict_Keys(this.handle);
659670
const length = py.PyList_Size(keys) as number;
10000 @@ -669,7 +680,7 @@ export class PyObject {
669680
return dict;
670681
}
671682

672-
*[Symbol.iterator]() {
683+
*[Symbol.iterator](): Generator<PyObject> {
673684
const iter = py.PyObject_GetIter(this.handle);
674685
let item = py.PyIter_Next(iter);
675686
while (item !== null) {
@@ -682,8 +693,8 @@ export class PyObject {
682693
/**
683694
* Casts a Set Python object as JS Set object.
684695
*/
685-
asSet() {
686-
const set = new Set();
696+
asSet(): Set<PythonConvertible> {
697+
const set = new Set<PythonConvertible>();
687698
for (const i of this) {
688699
set.add(i.valueOf());
689700
}
@@ -693,7 +704,7 @@ export class PyObject {
693704
/**
694705
* Casts a Tuple Python object as JS Array value.
695706
*/
696-
asTuple() {
707+
asTuple(): PythonConvertible[] {
697708
const tuple = new Array<PythonConvertible>();
698709
const length = py.PyTuple_Size(this.handle) as number;
699710
for (let i = 0; i < length; i++) {
@@ -711,7 +722,7 @@ export class PyObject {
711722
* Only primitives are casted as JS value type, otherwise returns
712723
* a proxy to Python object.
713724
*/
714-
valueOf() {
725+
valueOf(): any {
715726
const type = py.PyObject_Type(this.handle);
716727

717728
if (Deno.UnsafePointer.equals(type, python.None[ProxiedPyObject].handle)) {
@@ -759,7 +770,7 @@ export class PyObject {
759770
call(
760771
positional: (PythonConvertible | NamedArgument)[] = [],
761772
named: Record<string, PythonConvertible> = {},
762-
) {
773+
): PyObject {
763774
// count named arguments
764775
const namedCount = positional.filter(
765776
(arg) => arg instanceof NamedArgument,
@@ -808,16 +819,16 @@ export class PyObject {
808819
/**
809820
* Returns `str` representation of the Python object.
810821
*/
811-
toString() {
822+
toString(): string {
812823
return new PyObject(py.PyObject_Str(this.handle))
813-
.asString();
824+
.asString()!;
814825
}
815826

816-
[Symbol.for("Deno.customInspect")]() {
827+
[Symbol.for("Deno.customInspect")](): string {
817828
return this.toString();
818829
}
819830

820-
[Symbol.for("nodejs.util.inspect.custom")]() {
831+
[Symbol.for("nodejs.util.inspect.custom")](): string {
821832
return this.toString();
822833
}
823834
}
@@ -928,7 +939,7 @@ export class Python {
928939
/**
929940
* Runs Python script from the given string.
930941
*/
931-
run(code: string) {
942+
run(code: string): void {
932943
if (py.PyRun_SimpleString(cstr(code)) !== 0) {
933944
throw new EvalError("Failed to run python code");
934945
}
@@ -938,7 +949,7 @@ export class Python {
938949
* Runs Python script as a module and returns its module object,
939950
* for using its attributes, functions, classes, etc. from JavaScript.
940951
*/
941-
runModule(code: string, name?: string) {
952+
runModule(code: string, name?: string): any {
942953
const module = py.PyImport_ExecCodeModule(
943954
cstr(name ?? "__main__"),
944955
PyObject.from(
@@ -956,7 +967,7 @@ export class Python {
956967
/**
957968
* Import a module as PyObject.
958969
*/
959-
importObject(name: string) {
970+
importObject(name: string): PyObject {
960971
const mod = py.PyImport_ImportModule(cstr(name));
961972
if (mod === null) {
962973
maybeThrowError();
@@ -968,7 +979,7 @@ export class Python {
968979
/**
969980
* Import a Python module as a proxy object.
970981
*/
971-
import(name: string) {
982+
import(name: string): any {
972983
return this.importObject(name).proxy;
973984
}
974985

@@ -1013,7 +1024,7 @@ export class Python {
10131024
* and also make use of some common built-ins attached to
10141025
* this object, such as `str`, `int`, `tuple`, etc.
10151026
*/
1016-
export const python = new Python();
1027+
export const python: Python = new Python();
10171028

10181029
/**
10191030
* Returns true if the value can be converted into a Python slice or

test/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
ProxiedPyObject,
66
PyObject,
77
python,
8-
PythonProxy,
8+
type PythonProxy,
99
} from "../mod.ts";
1010

1111
const { version, executable } = python.import("sys");

0 commit comments

Comments
 (0)
0