From 262eabb008bc948d4cc03edbad9cd8c8a2ad473b Mon Sep 17 00:00:00 2001 From: Andrew Strelsky <46897303+astrelsky@users.noreply.github.com> Date: Mon, 9 Sep 2024 18:40:44 -0400 Subject: [PATCH] remove str support from JArray __class_getitem__ --- doc/CHANGELOG.rst | 2 +- jpype/_jarray.py | 7 ++++--- test/jpypetest/test_array.py | 9 +-------- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/doc/CHANGELOG.rst b/doc/CHANGELOG.rst index 402e1e445..034aeb0bd 100644 --- a/doc/CHANGELOG.rst +++ b/doc/CHANGELOG.rst @@ -7,7 +7,7 @@ Latest Changes: - **1.5.1_dev0 - 2023-12-15** - - Added support for typing ``JArray``. ``JArray[java.lang.Object]`` ``JArray["java.lang.Object"]`` + - Added support for typing ``JArray`` (Java type only), e.g. ``JArray[java.lang.Object]`` ``"JArray[java.lang.Object]"`` - Fixed uncaught exception while setting traceback causing issues in Python 3.11/3.12. diff --git a/jpype/_jarray.py b/jpype/_jarray.py index 26c0a1578..0c7d4fe82 100644 --- a/jpype/_jarray.py +++ b/jpype/_jarray.py @@ -16,6 +16,7 @@ # # ***************************************************************************** import _jpype +import typing from . import _jcustomizer @@ -29,7 +30,7 @@ class JArray(_jpype._JObject, internal=True): # type: ignore[call-arg] The resulting Java array class can be used to construct a new array with a given size or specified members. - JPype arrays support Python operators for iterating, length, equals, + JPype arrays support Python operators for iterating, length, equals, not equals, subscripting, and slicing. They also support Java object methods, clone, and length property. Java arrays may not be resized, and as such elements cannot be added nor deleted. Currently, @@ -96,9 +97,9 @@ def __class_getitem__(cls, key): if key is _jpype.JClass: # explicit check for JClass # _toJavaClass cannot be used - # passing int, float, etc is not allowed + # passing int, float, str, etc is not allowed key = _jpype._java_lang_Class - if isinstance(key, (str, _jpype._java_lang_Class)): + if isinstance(key, _jpype._java_lang_Class): key = _jpype.JClass(key) if isinstance(key, _jpype.JClass): return type(key[0]) diff --git a/test/jpypetest/test_array.py b/test/jpypetest/test_array.py index d84b9c0e6..8fe672a7a 100644 --- a/test/jpypetest/test_array.py +++ b/test/jpypetest/test_array.py @@ -613,13 +613,6 @@ def testJArrayGeneric_Init(self): def testJArrayInvalidGeneric(self): with self.assertRaises(TypeError): jpype.JArray[object] - + def testJArrayGenericJClass(self): self.assertEqual(type(JClass[0]), JArray[JClass]) - - def testJArrayGenericString(self): - self.assertEqual(type(JClass[0]), JArray["java.lang.Class"]) - - def testJArrayGenericStringInvalid(self): - with self.assertRaises(TypeError): - JArray["foo.bar"] # type: ignore