@@ -138,7 +138,10 @@ export function kw(
138138 * ```
139139 */
140140export 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 ;
@@ -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
0 commit comments