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

Add 3.13 compatibility #129

Merged
merged 5 commits into from
Nov 5, 2024
Merged

Add 3.13 compatibility #129

merged 5 commits into from
Nov 5, 2024

Conversation

benjjs
Copy link
Contributor

@benjjs benjjs commented Oct 21, 2024

Package is currently not compatible with Python 3.13 because it uses 2 functions from the dataclasses module that were removed from 3.12 to 3.13.

To resolve this problem, I have simply pulled the relevant functions (along with one helper that was also removed) into the package. I've documented which version they were removed in and the original functions in 3.12 that they are copied from.

The only other change is to explicitly pass empty type_params to typing._eval_type(), as leaving it implicit raises a warning in 3.13. Given that we previously did not supply this argument and the implicit default value was (), explicitly passing () will not change the behavior.

There is almost certainly some more elegant way to do this that would involve removing the need for these functions in the first place, but my priority here was to add compatibility quickly and without modifying any underlying logic and hence potentially breaking things. If you (the commenter or reviewer) have the more elegant implementation, by all means put it up and let's do that instead!

return typing._eval_type(base_type, base_globals, _TYPING_LOCALS)
if sys.version_info >= (3, 13):
# noinspection PyProtectedMember
return typing._eval_type(base_type, base_globals, _TYPING_LOCALS, type_params=())
Copy link
Owner

@rnag rnag Nov 4, 2024

Choose a reason for hiding this comment

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

@benjjs Minor thing, can you pull this out to the top level? That way we won't need to do a python version check each time eval_forward_ref() is called. Thanks!

if sys.version_info >= (3, 13):
  _eval_type = functools.partial(typing._eval_type, type_params=())
else:
  _eval_type = typing._eval_type

Copy link
Owner

@rnag rnag Nov 4, 2024

Choose a reason for hiding this comment

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

Follow-up, we can add this to utils/typing_compat.py and import/use it instead:

# Check if currently running Python 3.13 or higher
PY313_OR_ABOVE = _PY_VERSION >= (3, 13)

Thanks!

Copy link
Owner

Choose a reason for hiding this comment

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

@benjjs Minor thing, can you pull this out to the top level? That way we won't need to do a python version check each time eval_forward_ref() is called. Thanks!

if sys.version_info >= (3, 13):
  _eval_type = functools.partial(typing._eval_type, type_params=())
else:
  _eval_type = typing._eval_type

This was not resolved, but its OK, I can do that. Simple fix.

from types import FunctionType


def _set_qualname(cls, value):
Copy link
Owner

Choose a reason for hiding this comment

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

Good idea, thanks!

@@ -10,6 +8,8 @@
from .dumpers import asdict
from .loaders import fromdict, fromlist
from .type_def import Decoder, Encoder, JSONObject, ListOfJSONObject
# noinspection PyProtectedMember
from .utils.compatibility import _create_fn, _set_new_attribute
Copy link
Owner

Choose a reason for hiding this comment

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

I am wondering if we can come up with a better name for the module. Maybe something like dataclasses_compat? I think compatability is a bit vague overall.

@rnag
Copy link
Owner

rnag commented Nov 4, 2024

@benjjs Thanks so much for creating this PR!

I recently upgraded my laptop to Python 3.13, and I also noticed that Dataclass Wizard breaks on import due to the errors mentioned in the PR. I feel that was my bad on importing hidden or private functions from a (builtin) module, which is a big no-no as I always say 😅 .

Thanks again for your work and effort in creating this PR! I have added some comments, please let me know when you're able to look into the notes I added, and I will merge the bugfix for 3.13 once complete. Have a great day!

@benjjs
Copy link
Contributor Author

benjjs commented Nov 4, 2024

Thanks for reviewing @rnag ! Addressed the above comments.

@rnag
Copy link
Owner

rnag commented Nov 4, 2024

Thanks @benjjs ! Everything looks good to go. I'll review/merge later today.

@rnag rnag merged commit e5f37cb into rnag:main Nov 5, 2024
2 of 7 checks passed
@rnag
Copy link
Owner

rnag commented Nov 5, 2024

Ok, looks good to go. Thanks! One minor change left, and I will work on the release.

I actually dozed off yesterday night, so I was not able to do it then 😓 . Stay tuned for the release!

@rnag rnag mentioned this pull request Nov 5, 2024
@rnag
Copy link
Owner

rnag commented Nov 5, 2024

@benjjs Done - Thanks for your effort and help on this (also your patience!)

v0.26.0 is officially live, and adds support for PY 3.13 🎉

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.

2 participants