JuliaPy / PythonCall.jl

Python and Julia in harmony.
https://juliapy.github.io/PythonCall.jl/stable/
MIT License
717 stars 61 forks source link

Migrate to Python 3.12, require it #421

Closed PallHaraldsson closed 5 months ago

PallHaraldsson commented 7 months ago

I realize you support older, and may want to, I'm just thinking if it could be better to only support 3.12+ already.

What might be reasons and pros and cons? I see:

Porting to Python 3.12

  • Legacy Unicode APIs based on Py_UNICODE representation has been removed. Please migrate to APIs based on UTF-8 or wchar_t.

They want to move to UTF-8, I believe it's not yet the default encoding, but it's been an alternative redundant encoding for a long time.

When you call to or from Python, then I believe it's non-copying, you can reuse arrays in either direction, and that would be ideal for strings too and all else. What are the exceptions? Strings are UTF-8 in Julia and need to be converted (or not?), if calling Python, though probably if Julia is called with a Python string.

Also Dicts are unordered in Julia, so I'm not sure what happens. Possibly OrderedDefaultDict could be handled better, or if were Julia's default, but right now dicts can't be shared in either direction. Are there more exceptions?

Python 3.12 should work already, so you don't have to do anything per se. And PythonCall will install a Python for you (now a smaller download since a lot of deprecated/outdated stuff dropped), but if you require 3.12 or higher it will be 3.12 for now. I think you already get the latest anyway? Unless you ask for older directly or indirectly, i.e. some package forces it. So then it couldn't be used. Going forward will not be a problem.

The only point of restricting is if you add capability that only exists in 3.12, so that you can use it. Older versions of your package would still work with older Python and that could be documented, and even supported for a while.

So what do you think, is there anything new and interesting in 3.12 you want to take advantage of?

I haven't looked over everything, but there are intriguing changes (probably largest Python release in a while):

[Python already has a (limited) stable C API, that I suppose you use. Plus I now see this new unstable.]

New Features

  • PEP 697: Introduce the Unstable C API tier, intended for low-level tools like debuggers and JIT compilers. This API may change in each minor release of CPython without deprecation warnings. Its contents are marked by the PyUnstable_ prefix in names.

[..]

[..]

  • The C API now permits registering callbacks via PyDict_AddWatcher(), PyDict_Watch() and related APIs to be called whenever a dictionary is modified. This is intended for use by optimizing interpreters, JIT compilers, or debuggers. (Contributed by Carl Meyer in gh-91052.)

[..]

  • PEP 683: Introduce Immortal Objects, which allows objects to bypass reference counts, and related changes to the C-API:

[..]

MilesCranmer commented 6 months ago

Just passing by to comment that as a maintainer of a Python-Julia downstream package (PySR), I would perhaps vote against this change, since some of my users have to use their institute cluster Python which is typically fixed to an older version.

I think it's important for tools at the upper levels of a dependency hierarchy, like PythonCall.jl, to impose as few subjective constraints as possible, as they have a cascading effect.

My suggestion would be to simply follow the "Security support" column of https://endoflife.date/python which is 3.8-3.12 currently. Older versions are not receiving security updates. 3.11 is still officially supported with security updates by the Python Software Foundation until October 24, 2027 – which would make that a good point at which to switch to 3.12 only. So my suggestion is to table this until then. Wdyt?

PallHaraldsson commented 6 months ago

Older versions of Python would still work in older PythonCall.jl at least. I'm thinking for the future, to be able to rely on new features in Python, at least UTF-8 if/when it will be default. If there's nothing compelling to know we can depend on then it's a moot point.

Also I was thinking default/require 3.12, but possibly still in latest PythonCall.jl there could be a workaround to still allow older, or automatically support and then with reduced (i.e. current) functionality.

About that institute cluster Python, since it's older version than 3.12, would it not help if PythonCall can (and already does) download latest for you?

MilesCranmer commented 6 months ago

Maybe one could just wrap new features with a

import sys
if sys.version_info >= (3, 12, 0):
    ...

block? I know some Python packages that do stuff like this. I guess if you want to make structural changes then it becomes a bit bloated though, so not sure.

Re: cluster python, I agree that my personal preference is to just have my own python version. But I am aware of cases where sysadmin can get annoyed by this because it can blow up the file count on $HOME (e.g., some conda environments can have 100,000 files!), which itself slows down backups, compared to if it is a centrally managed python with common packages shared among all users. So some users are simply stuck with a particular python version and need to link it to any software they use. (This is obviously institute-dependent though!)

PallHaraldsson commented 5 months ago

This intriguing UTF-8 bug on Windows was fixed, and backported, but only back to 1.10: https://bugs.python.org/issue37871

I didn't confirm it could be triggered by Julia, but very plausibly, so at least recommend 1.10 or later?

cjdoris commented 5 months ago

Maybe this isn't written down anywhere and should be but I'm aiming to remain compatible with all supported versions of Julia and Python unless there are highly compelling reasons not to.

I'm not aware of any sufficiently compelling functionality in 3.12 (including what's pointed out above) so I'll close this issue.