From 23a713f2081f9a0d5efe8abc7776dda4e5206916 Mon Sep 17 00:00:00 2001 From: Curtis Rueden Date: Thu, 3 Nov 2022 11:37:49 -0500 Subject: [PATCH] Add type hints in more places --- src/scyjava/_arrays.py | 8 +++++--- src/scyjava/_convert.py | 4 ++-- src/scyjava/_java.py | 12 ++++++------ src/scyjava/_versions.py | 4 ++-- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/scyjava/_arrays.py b/src/scyjava/_arrays.py index 306568b2..be3b9715 100644 --- a/src/scyjava/_arrays.py +++ b/src/scyjava/_arrays.py @@ -2,8 +2,10 @@ Utility functions for working with and reasoning about arrays. """ +from typing import Any -def is_arraylike(arr): + +def is_arraylike(arr: Any) -> bool: """ Return True iff the object is arraylike: possessing .shape, .dtype, .__array__, and .ndim attributes. @@ -19,7 +21,7 @@ def is_arraylike(arr): ) -def is_memoryarraylike(arr): +def is_memoryarraylike(arr: Any) -> bool: """ Return True iff the object is memoryarraylike: an arraylike object whose .data type is memoryview. @@ -34,7 +36,7 @@ def is_memoryarraylike(arr): ) -def is_xarraylike(xarr): +def is_xarraylike(xarr: Any) -> bool: """ Return True iff the object is xarraylike: possessing .values, .dims, and .coords attributes, diff --git a/src/scyjava/_convert.py b/src/scyjava/_convert.py index c3db643b..7370cf54 100644 --- a/src/scyjava/_convert.py +++ b/src/scyjava/_convert.py @@ -99,7 +99,7 @@ def _convertIterable(obj: collections.abc.Iterable): java_converters: List[Converter] = [] -def add_java_converter(converter: Converter): +def add_java_converter(converter: Converter) -> None: """ Add a converter to the list used by to_java. :param converter: A Converter going from python to java @@ -467,7 +467,7 @@ def __str__(self): py_converters: List[Converter] = [] -def add_py_converter(converter: Converter): +def add_py_converter(converter: Converter) -> None: """ Add a converter to the list used by to_python. :param converter: A Converter from java to python diff --git a/src/scyjava/_java.py b/src/scyjava/_java.py index 65c16a37..f661a339 100644 --- a/src/scyjava/_java.py +++ b/src/scyjava/_java.py @@ -10,7 +10,7 @@ import sys from functools import lru_cache from pathlib import Path -from typing import Callable +from typing import Callable, Sequence import jpype import jpype.config @@ -76,7 +76,7 @@ def inner(self): # -- JVM functions -- -def jvm_version(): +def jvm_version() -> str: """ Gets the version of the JVM as a tuple, with each dot-separated digit as one element. @@ -147,7 +147,7 @@ def jvm_version(): return tuple(map(int, m.group(1).split("."))) -def start_jvm(options=None): +def start_jvm(options=None) -> None: """ Explicitly connect to the Java virtual machine (JVM). Only one JVM can be active; does nothing if the JVM has already been started. Calling @@ -231,7 +231,7 @@ def start_jvm(options=None): callback() -def shutdown_jvm(): +def shutdown_jvm() -> None: """Shutdown the JVM. This function makes a best effort to clean up Java resources first. @@ -355,7 +355,7 @@ def is_jarray(data) -> bool: @lru_cache(maxsize=None) -def jimport(class_name): +def jimport(class_name: str): """ Import a class from Java to Python. @@ -430,7 +430,7 @@ def jstacktrace(exc) -> str: return "" -def jarray(kind, lengths): +def jarray(kind, lengths: Sequence): """ Create a new n-dimensional Java array. diff --git a/src/scyjava/_versions.py b/src/scyjava/_versions.py index 31a0178e..2fb19db8 100644 --- a/src/scyjava/_versions.py +++ b/src/scyjava/_versions.py @@ -10,7 +10,7 @@ _logger = logging.getLogger(__name__) -def get_version(java_class_or_python_package): +def get_version(java_class_or_python_package) -> str: """ Return the version of a Java class or Python package. @@ -48,7 +48,7 @@ def get_version(java_class_or_python_package): raise RuntimeError("Cannot determine version! Is pkg_resources installed?") -def is_version_at_least(actual_version, minimum_version): +def is_version_at_least(actual_version: str, minimum_version: str) -> bool: """ Return a boolean on a version comparison. Requires org.scijava:scijava-common on the classpath.