Skip to content

Commit

Permalink
Merge pull request #1135 from pelson/feature/type-checking
Browse files Browse the repository at this point in the history
Enable type checking of the jpype and test suite codebase
  • Loading branch information
Thrameos authored Dec 10, 2023
2 parents e9a921d + b24a242 commit 45b48e2
Show file tree
Hide file tree
Showing 41 changed files with 168 additions and 205 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Mypy

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
name: mypy
steps:
- uses: actions/checkout@v1
- name: Set up Python 3.11
uses: actions/setup-python@v1
with:
python-version: 3.11
- name: Install Dependencies
run: |
pip install mypy numpy types-pyinstaller pytest packaging
- name: mypy
run: |
mypy ./jpype/ ./test/jpypetest/
17 changes: 9 additions & 8 deletions jpype/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from ._jpackage import *
from ._jproxy import *
from ._core import *
from . import _core
from ._gui import *
from ._classpath import *
from ._jclass import *
Expand All @@ -41,15 +42,15 @@
from . import _jthread # lgtm [py/import-own-module]

__all__ = ['java', 'javax']
__all__.extend(_jinit.__all__)
__all__.extend(_jinit.__all__) # type: ignore[name-defined]
__all__.extend(_core.__all__)
__all__.extend(_classpath.__all__)
__all__.extend(types.__all__)
__all__.extend(_jproxy.__all__)
__all__.extend(_jpackage.__all__)
__all__.extend(_jclass.__all__)
__all__.extend(_jcustomizer.__all__)
__all__.extend(_gui.__all__)
__all__.extend(_classpath.__all__) # type: ignore[name-defined]
__all__.extend(types.__all__) # type: ignore[name-defined]
__all__.extend(_jproxy.__all__) # type: ignore[name-defined]
__all__.extend(_jpackage.__all__) # type: ignore[name-defined]
__all__.extend(_jclass.__all__) # type: ignore[name-defined]
__all__.extend(_jcustomizer.__all__) # type: ignore[name-defined]
__all__.extend(_gui.__all__) # type: ignore[name-defined]

__version__ = "1.5.0_dev0"
__version_info__ = __version__.split('.')
Expand Down
4 changes: 2 additions & 2 deletions jpype/_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

def setupGuiEnvironment(cb):
if _sys.platform == 'darwin':
from PyObjCTools import AppHelper
from PyObjCTools import AppHelper # type: ignore
m = {'run': cb}
proxy = _jproxy.JProxy('java.lang.Runnable', m)
cbthread = _jclass.JClass("java.lang.Thread")(proxy)
Expand All @@ -40,5 +40,5 @@ def setupGuiEnvironment(cb):

def shutdownGuiEnvironment():
if _sys.platform == 'darwin':
from PyObjCTools import AppHelper
from PyObjCTools import AppHelper # type: ignore
AppHelper.stopEventLoop()
2 changes: 1 addition & 1 deletion jpype/_jarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
__all__ = ['JArray']


class JArray(_jpype._JObject, internal=True):
class JArray(_jpype._JObject, internal=True): # type: ignore[call-arg]
""" Creates a Java array class for a Java type of a given dimension.
This serves as a base type and factory for all Java array classes.
Expand Down
2 changes: 1 addition & 1 deletion jpype/_jclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def __new__(cls, jc, loader=None, initialize=True):
return _jpype._getClass(jc)


class JInterface(_jpype._JObject, internal=True):
class JInterface(_jpype._JObject, internal=True): # type: ignore[call-arg]
"""A meta class for all Java Interfaces.
``JInterface`` is serves as the base class for any Java class that is
Expand Down
2 changes: 1 addition & 1 deletion jpype/_jcollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class _JCollection(object):
""" Customizer for ``java.util.Collection``
This customizer adds the Python functions ``len()`` and ``del`` to
Java Collions to allow for Python syntax.
Java Collisions to allow for Python syntax.
"""

def __len__(self):
Expand Down
62 changes: 8 additions & 54 deletions jpype/_jcollection.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Iterable, Collection, List, Set, Mapping, Tuple, TypeVar, Iterator, Generator, Union, overload
from typing import Any, Iterable, Collection, List, Set, Mapping, Tuple, TypeVar, Iterator, Generator, Union, overload, Dict

E = TypeVar('E')
K = TypeVar('K')
Expand All @@ -14,80 +14,34 @@ class _JCollection(Collection[E]):

def __delitem__(self, i: E) -> None: ...

def __contains__(self, i: E) -> bool: ...
def __contains__(self, i: Any) -> bool: ...

def __iter__(self) -> Iterator[E]: ...

class _JList(List[E]):
@overload
def __getitem__(self, ndx: int) -> E: ...

@overload
def __getitem__(self, ndx: slice) -> E: ...

def __setitem__(self, ndx: int, v: E) -> None: ...

def __delitem__(self, ndx: int) -> E: ...

def __reversed__(self) -> Generator[E, None, None]: ...

def index(self, obj: E) -> int: ...

def count(self, obj: E) -> int: ...

def insert(self, idx: int, obj: E) -> '_JList'[E]: ...

def append(self, obj: E) -> '_JList'[E]: ...

def reverse(self) -> None: ...

def extend(self, lst: Iterable[E]) -> None: ...

def pop(self, idx: int = ...) -> E: ...

def __iadd__(self, obj: List[E]) -> '_JList'[E]: ...

def __add__(self, obj: List[E]) -> '_JList'[E]: ...

def remove(self, obj: E) -> None: ...
class _JList(List[E]):
pass


class _JMap(Mapping[K, V]):
class _JMap(Dict[K, V]):
def __len__(self) -> int: ...

def __iter__(self) -> Iterator[K]: ...

def __delitem__(self, i: K) -> V: ...

def __getitem__(self, ndx: K) -> V: ...

def __setitem__(self, ndx: K, v: V) -> None: ...

def items(self) -> Set['_JMapEntry'[K, V]]: ...

def keys(self) -> Set[K]: ...

def __contains__(self, item: V) -> bool: ...


class _JSet(Set[E]):
def __delitem__(self, i: E): ...
pass


class _JMapEntry(Tuple[K, V]):
def __len__(self) -> int: ...

def __getitem__(self, x: int) -> Union[K, V]: ...
pass


class _JIterator(Iterator[E]):
def __next__(self) -> E: ...

def __iter__(self) -> Iterator[E]: ...


class _JEnumeration(Iterator[E]):
def __next__(self) -> E: ...

def __iter__(self) -> Iterator[E]: ...

def next(self) -> E: ...
2 changes: 1 addition & 1 deletion jpype/_jexception.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


@_jcustomizer.JImplementationFor("java.lang.Throwable", base=True)
class JException(_jpype._JException, internal=True):
class JException(_jpype._JException, internal=True): # type: ignore[call-arg]
""" Base class for all ``java.lang.Throwable`` objects.
Use ``issubclass(cls, JException)`` to test if a class is derived
Expand Down
5 changes: 3 additions & 2 deletions jpype/_jmethod.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
# See NOTICE file for details.
#
# *****************************************************************************
import _jpype
__all__ = []

import _jpype
from . import _jclass

__all__ = [] # type: ignore[var-annotated]


def _jmethodGetDoc(method, cls, overloads):
"""Generator for _JMethod.__doc__ property
Expand Down
2 changes: 1 addition & 1 deletion jpype/_jobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
__all__ = ['JObject']


class JObject(_jpype._JObject, internal=True):
class JObject(_jpype._JObject, internal=True): # type: ignore[call-arg]
""" Base class for all object instances.
It can be used to test if an object is a Java object instance with
Expand Down
22 changes: 12 additions & 10 deletions jpype/_jstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
# See NOTICE file for details.
#
# *****************************************************************************
import typing

import _jpype
from . import _jcustomizer

__all__ = ['JString']


class JString(_jpype._JObject, internal=True):
class JString(_jpype._JObject, internal=True): # type: ignore[call-arg]
""" Base class for ``java.lang.String`` objects
When called as a function, this class will produce a ``java.lang.String``
Expand All @@ -37,14 +39,14 @@ def __new__(cls, *args, **kwargs):


@_jcustomizer.JImplementationFor("java.lang.String")
class _JStringProto(object):
def __add__(self, other):
return self.concat(other)
class _JStringProto:
def __add__(self, other: str) -> str:
return self.concat(other) # type: ignore[attr-defined]

def __len__(self):
return self.length()
def __len__(self) -> int:
return self.length() # type: ignore[attr-defined]

def __getitem__(self, i):
def __getitem__(self, i: typing.Union[slice, int]):
if isinstance(i, slice):
return str(self)[i]

Expand All @@ -54,10 +56,10 @@ def __getitem__(self, i):
raise IndexError("Array index is negative")
if i >= len(self):
raise IndexError("Array index exceeds length")
return self.charAt(i)
return self.charAt(i) # type: ignore[attr-defined]

def __contains__(self, other):
return self.contains(other)
def __contains__(self, other: str) -> bool:
return self.contains(other) # type: ignore[attr-defined]

def __hash__(self):
if self == None: # lgtm [py/test-equals-none]
Expand Down
13 changes: 0 additions & 13 deletions jpype/_jstring.pyi

This file was deleted.

10 changes: 5 additions & 5 deletions jpype/_jthread.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@


@_jcustomizer.JImplementationFor('java.lang.Thread')
class _JThread(object):
class _JThread:
""" Customizer for ``java.land.Thread``
This adds addition JPype methods to java.lang.Thread to support
Python.
"""

@staticmethod
def isAttached():
def isAttached() -> bool:
""" Checks if a thread is attached to the JVM.
Python automatically attaches as daemon threads when a Java method is
Expand All @@ -44,7 +44,7 @@ def isAttached():
return _jpype.isThreadAttachedToJVM()

@staticmethod
def attach():
def attach() -> None:
""" Attaches the current thread to the JVM as a user thread.
User threads that are attached to the JVM will prevent the JVM from
Expand All @@ -57,7 +57,7 @@ def attach():
return _jpype.attachThreadToJVM()

@staticmethod
def attachAsDaemon():
def attachAsDaemon() -> None:
""" Attaches the current thread to the JVM as a daemon.
Daemon threads act as background tasks and do not prevent the JVM from
Expand All @@ -71,7 +71,7 @@ def attachAsDaemon():
return _jpype.attachThreadAsDaemon()

@staticmethod
def detach():
def detach() -> None:
""" Detaches a thread from the JVM.
This function detaches the thread and frees the associated resource in
Expand Down
12 changes: 0 additions & 12 deletions jpype/_jthread.pyi

This file was deleted.

4 changes: 2 additions & 2 deletions jpype/_jvmfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
try:
import winreg
except ImportError:
winreg = None
winreg = None # type: ignore[assignment]


class JVMNotFoundException(ValueError):
Expand All @@ -45,7 +45,7 @@ class JVMNotSupportedException(ValueError):
This exception is raised after a search found a valid Java home directory
was found, but the JVM shared library found is not supported. Typically
this occures when the JVM does not match the architecture of Python
this occurs when the JVM does not match the architecture of Python
32 vs 64 bit, or the JVM is older than the version used to compile
JPype.
"""
Expand Down
8 changes: 4 additions & 4 deletions jpype/dbapi2.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,11 @@ def _asPython(x):
LONGNVARCHAR: STRING, DOUBLE: DOUBLE, OTHER: OBJECT
}

_default_setters = {}
_default_setters = {} # type: ignore[var-annotated]

_default_converters = {}
_default_converters = {} # type: ignore[var-annotated]

_default_adapters = {}
_default_adapters = {} # type: ignore[var-annotated]


# Setters take (connection, meta, col, type) -> JDBCTYPE
Expand All @@ -251,7 +251,7 @@ def SETTERS_BY_META(cx, meta, col, ptype):
return _default_map[_registry[meta.getParameterType(col + 1)]]


SETTERS_BY_META._cachable = True
SETTERS_BY_META._cachable = True # type: ignore[attr-defined]


def SETTERS_BY_TYPE(cx, meta, col, ptype):
Expand Down
Loading

0 comments on commit 45b48e2

Please sign in to comment.