Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Generic" JArray support #1214

Merged
merged 9 commits into from
Oct 2, 2024
Merged

"Generic" JArray support #1214

merged 9 commits into from
Oct 2, 2024

Conversation

astrelsky
Copy link
Contributor

This allows the use of a parameterized JArray in type annotations. There was no other way to represent a java array type.

This also includes two small fixes where you could not do JArray(JClass) or JClass[0].

jpype/_jarray.py Fixed Show resolved Hide resolved
jpype/_jarray.py Outdated Show resolved Hide resolved
Copy link

codecov bot commented Sep 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.21%. Comparing base (66f8c6c) to head (4e3b6d8).
Report is 46 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1214      +/-   ##
==========================================
- Coverage   87.21%   87.21%   -0.01%     
==========================================
  Files         113      113              
  Lines       10281    10293      +12     
  Branches     4065     4066       +1     
==========================================
+ Hits         8967     8977      +10     
- Misses        718      719       +1     
- Partials      596      597       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

jpype/_jarray.pyi Outdated Show resolved Hide resolved
Copy link
Contributor

@Thrameos Thrameos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything looks good, but there needs to be a message in doc/CHANGELOG.rst as to changes to the public API. Just one sentence should be sufficient. Thanks for the contribution.

test/jpypetest/test_array.py Show resolved Hide resolved
@@ -339,6 +339,7 @@ def initializeResources():
_jpype._type_classes[object] = _jpype._java_lang_Object
_jpype._type_classes[_jpype.JString] = _jpype._java_lang_String
_jpype._type_classes[_jpype.JObject] = _jpype._java_lang_Object
_jpype._type_classes[_jpype.JClass] = _jpype._java_lang_Class
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems reasonable. Though technically all uses of _type_classes needs checking. There used to be an edge case though I don't see it any more. The only user is _jarray which i checked.

jpype/_jarray.py Outdated Show resolved Hide resolved
jpype/_jarray.py Outdated Show resolved Hide resolved
@Thrameos
Copy link
Contributor

Thrameos commented Sep 3, 2024

For some reason github ate part of my review and I had to repost. I always forget to hit start review before commenting. Sorry if there are repeats.

@Thrameos
Copy link
Contributor

Thrameos commented Sep 3, 2024 via email

@astrelsky
Copy link
Contributor Author

astrelsky commented Sep 3, 2024

I would recommend trying jpype.JArray(_jpype._JClass) to hit that code path. The backend classes all have unoccupied java slots unless the front end has specifically added it during initialization.

You are correct. I just checked and it's actually already protected here.

PyErr_SetString(PyExc_TypeError, "Java class required");

However, if I do JArray[_jpype._JClass] I get the TypeError from __class_getitem__ which now says "TypeError: <class '_jpype._JClass'> is not a Java class".

Would you prefer I explicitly check for this case and raise the original TypeError? I can see how the error message in that specific case could confuse someone.

doc/CHANGELOG.rst Outdated Show resolved Hide resolved
jpype/_jarray.py Show resolved Hide resolved
jpype/_jarray.pyi Outdated Show resolved Hide resolved
test/jpypetest/test_array.py Outdated Show resolved Hide resolved
jpype/_jarray.py Outdated Show resolved Hide resolved
test/jpypetest/test_array.py Outdated Show resolved Hide resolved
Unfortunately isinstance will fail on some things
such as typing.Collection. Using typing.Generic
will prevent confusion.
jpype/_jarray.py Fixed Show fixed Hide fixed
Copy link
Contributor

@pelson pelson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

jpype/_jarray.pyi Outdated Show resolved Hide resolved
@astrelsky
Copy link
Contributor Author

astrelsky commented Sep 10, 2024

If there is an option to squash the commits, may as well use it. I don't see it from my end. Don't worry about the "Unverified" code signature I have something messed up on my end and can't be bothered to deal with it.

@pelson
Copy link
Contributor

pelson commented Sep 23, 2024

@Thrameos - this PR is good to go from my perspective. I appreciate you are deep in Python 3.13 work, so let me know if there is anything I can do to help with this.

@Thrameos
Copy link
Contributor

@pelson I think long term we need to either formalize the Unstable with extra calls such that they work and get them to move the extra data that they are adding into the preheader space. Unfortunately, I tested heavily with the allocate with extra memory version they provided and it didn't work In addition the with extra memory is fundamentally incompatible with most recent changes in Python 3.13. I am also happy if they just adopted the user data patch which accomplishes the same thing, Eventually they will get a stable object model which provides for true multiple inheritance and this sort of problem won't haunt us every version.

I am not sure what it will take to get there. I can provide some patches or instructions for patching Python which we can provide for Python 3.14. Moving the inline dict to its proper space was only about 10 lines. (changing 2 macros, adding a type slot for the preheader allocation, and then the setting the free point.) But if we can't find a way to get patches accepted it won't get better. I am unfortunately too high a stress of person to navigate the bureaucracy that is Python (and maintain my very high stress job.)

@marscher
Copy link
Member

marscher commented Oct 2, 2024

Thank you @astrelsky for your first contribution and taking the review process.
And of course thanks @pelson and @Thrameos for the discussions and code reviews!

@marscher marscher merged commit a566bae into jpype-project:master Oct 2, 2024
26 checks passed
@astrelsky astrelsky deleted the jarray branch October 6, 2024 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants