@@ -138,7 +138,10 @@ export function kw(
138
138
* ```
139
139
*/
140
140
export class Callback {
141
- unsafe ;
141
+ unsafe : Deno . UnsafeCallback < {
142
+ parameters : [ "pointer" , "pointer" , "pointer" ] ;
143
+ result : "pointer" ;
144
+ } > ;
142
145
143
146
constructor ( public callback : PythonJSCallback ) {
144
147
this . unsafe = new Deno . UnsafeCallback (
@@ -200,7 +203,7 @@ export class PyObject {
200
203
/**
201
204
* Check if the object is NULL (pointer) or None type in Python.
202
205
*/
203
- get isNone ( ) {
206
+ get isNone ( ) : boolean {
204
207
// deno-lint-ignore ban-ts-comment
205
208
// @ts -expect-error
206
209
return this . handle === null || this . handle === 0 ||
@@ -403,9 +406,17 @@ export class PyObject {
403
406
/**
404
407
* Performs an equals operation on the Python object.
405
408
*/
406
- equals ( rhs : PythonConvertible ) {
409
+ equals ( rhs : PythonConvertible ) : boolean {
407
410
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 ;
409
420
}
410
421
411
422
/**
@@ -590,7 +601,7 @@ export class PyObject {
590
601
/**
591
602
* Tries to set the attribute, throws an error otherwise.
592
603
*/
593
- setAttr ( name : string , v : PythonConvertible ) {
604
+ setAttr ( name : string , v : PythonConvertible ) : void {
594
605
if (
595
606
py . PyObject_SetAttrString (
596
607
this . handle ,
@@ -603,43 +614,43 @@ export class PyObject {
603
614
}
604
615
605
616
/** Checks if Python object has an attribute of given name. */
606
- hasAttr ( attr : string ) {
617
+ hasAttr ( attr : string ) : boolean {
607
618
return py . PyObject_HasAttrString ( this . handle , cstr ( attr ) ) !== 0 ;
608
619
}
609
620
610
621
/**
611
622
* Casts a Bool Python object as JS Boolean value.
612
623
*/
613
- asBoolean ( ) {
624
+ asBoolean ( ) : boolean {
614
625
return py . PyLong_AsLong ( this . handle ) === 1 ;
615
626
}
616
627
617
628
/**
618
629
* Casts a Int Python object as JS Number value.
619
630
*/
620
- asLong ( ) {
631
+ asLong ( ) : number {
621
632
return py . PyLong_AsLong ( this . handle ) as number ;
622
633
}
623
634
624
635
/**
625
636
* Casts a Float (Double) Python object as JS Number value.
626
637
*/
627
- asDouble ( ) {
638
+ asDouble ( ) : number {
628
639
return py . PyFloat_AsDouble ( this . handle ) as number ;
629
640
}
630
641
631
642
/**
632
643
* Casts a String Python object as JS String value.
633
644
*/
634
- asString ( ) {
645
+ asString ( ) : string | null {
635
646
const str = py . PyUnicode_AsUTF8 ( this . handle ) ;
636
647
return str !== null ? Deno . UnsafePointerView . getCString ( str ) : null ;
637
648
}
638
649
639
650
/**
640
651
* Casts a List Python object as JS Array value.
641
652
*/
642
- asArray ( ) {
653
+ asArray ( ) : PythonConvertible [ ] {
643
654
const array : PythonConvertible [ ] = [ ] ;
644
655
for ( const i of this ) {
645
656
array . push ( i . valueOf ( ) ) ;
@@ -653,7 +664,7 @@ export class PyObject {
653
664
* Note: `from` supports converting both Map and Object to Python Dict.
654
665
* But this only supports returning a Map.
655
666
*/
656
- asDict ( ) {
667
+ asDict ( ) : Map < PythonConvertible , PythonConvertible > {
657
668
const dict = new Map < PythonConvertible , PythonConvertible > ( ) ;
658
669
const keys = py . PyDict_Keys ( this . handle ) ;
659
670
const length = py . PyList_Size ( keys ) as number ;
10000
@@ -669,7 +680,7 @@ export class PyObject {
669
680
return dict ;
670
681
}
671
682
672
- * [ Symbol . iterator ] ( ) {
683
+ * [ Symbol . iterator ] ( ) : Generator < PyObject > {
673
684
const iter = py . PyObject_GetIter ( this . handle ) ;
674
685
let item = py . PyIter_Next ( iter ) ;
675
686
while ( item !== null ) {
@@ -682,8 +693,8 @@ export class PyObject {
682
693
/**
683
694
* Casts a Set Python object as JS Set object.
684
695
*/
685
- asSet ( ) {
686
- const set = new Set ( ) ;
696
+ asSet ( ) : Set < PythonConvertible > {
697
+ const set = new Set < PythonConvertible > ( ) ;
687
698
for ( const i of this ) {
688
699
set . add ( i . valueOf ( ) ) ;
689
700
}
@@ -693,7 +704,7 @@ export class PyObject {
693
704
/**
694
705
* Casts a Tuple Python object as JS Array value.
695
706
*/
696
- asTuple ( ) {
707
+ asTuple ( ) : PythonConvertible [ ] {
697
708
const tuple = new Array < PythonConvertible > ( ) ;
698
709
const length = py . PyTuple_Size ( this . handle ) as number ;
699
710
for ( let i = 0 ; i < length ; i ++ ) {
@@ -711,7 +722,7 @@ export class PyObject {
711
722
* Only primitives are casted as JS value type, otherwise returns
712
723
* a proxy to Python object.
713
724
*/
714
- valueOf ( ) {
725
+ valueOf ( ) : any {
715
726
const type = py . PyObject_Type ( this . handle ) ;
716
727
717
728
if ( Deno . UnsafePointer . equals ( type , python . None [ ProxiedPyObject ] . handle ) ) {
@@ -759,7 +770,7 @@ export class PyObject {
759
770
call (
760
771
positional : ( PythonConvertible | NamedArgument ) [ ] = [ ] ,
761
772
named : Record < string , PythonConvertible > = { } ,
762
- ) {
773
+ ) : PyObject {
763
774
// count named arguments
764
775
const namedCount = positional . filter (
765
776
( arg ) => arg instanceof NamedArgument ,
@@ -808,16 +819,16 @@ export class PyObject {
808
819
/**
809
820
* Returns `str` representation of the Python object.
810
821
*/
811
- toString ( ) {
822
+ toString ( ) : string {
812
823
return new PyObject ( py . PyObject_Str ( this . handle ) )
813
- . asString ( ) ;
824
+ . asString ( ) ! ;
814
825
}
815
826
816
- [ Symbol . for ( "Deno.customInspect" ) ] ( ) {
827
+ [ Symbol . for ( "Deno.customInspect" ) ] ( ) : string {
817
828
return this . toString ( ) ;
818
829
}
819
830
820
- [ Symbol . for ( "nodejs.util.inspect.custom" ) ] ( ) {
831
+ [ Symbol . for ( "nodejs.util.inspect.custom" ) ] ( ) : string {
821
832
return this . toString ( ) ;
822
833
}
823
834
}
@@ -928,7 +939,7 @@ export class Python {
928
939
/**
929
940
* Runs Python script from the given string.
930
941
*/
931
- run ( code : string ) {
942
+ run ( code : string ) : void {
932
943
if ( py . PyRun_SimpleString ( cstr ( code ) ) !== 0 ) {
933
944
throw new EvalError ( "Failed to run python code" ) ;
934
945
}
@@ -938,7 +949,7 @@ export class Python {
938
949
* Runs Python script as a module and returns its module object,
939
950
* for using its attributes, functions, classes, etc. from JavaScript.
940
951
*/
941
- runModule ( code : string , name ?: string ) {
952
+ runModule ( code : string , name ?: string ) : any {
942
953
const module = py . PyImport_ExecCodeModule (
943
954
cstr ( name ?? "__main__" ) ,
944
955
PyObject . from (
@@ -956,7 +967,7 @@ export class Python {
956
967
/**
957
968
* Import a module as PyObject.
958
969
*/
959
- importObject ( name : string ) {
970
+ importObject ( name : string ) : PyObject {
960
971
const mod = py . PyImport_ImportModule ( cstr ( name ) ) ;
961
972
if ( mod === null ) {
962
973
maybeThrowError ( ) ;
@@ -968,7 +979,7 @@ export class Python {
968
979
/**
969
980
* Import a Python module as a proxy object.
970
981
*/
971
- import ( name : string ) {
982
+ import ( name : string ) : any {
972
983
return this . importObject ( name ) . proxy ;
973
984
}
974
985
@@ -1013,7 +1024,7 @@ export class Python {
1013
1024
* and also make use of some common built-ins attached to
1014
1025
* this object, such as `str`, `int`, `tuple`, etc.
1015
1026
*/
1016
- export const python = new Python ( ) ;
1027
+ export const python : Python = new Python ( ) ;
1017
1028
1018
1029
/**
1019
1030
* Returns true if the value can be converted into a Python slice or
0 commit comments