Repository navigation
Fix importing unitxt on Python 3.14 and 3.15 - #1992
Open
cristianchiriac wants to merge 1 commit into
Open
cristianchiriac wants to merge 1 commit into
cristianchiriac wants to merge 1 commit into
Conversation
- Read class annotations from __annotate__ when the class namespace has no __annotations__ (PEP 649/749, Python 3.14+); fields were not found and importing unitxt failed with UnexpectedArgumentError. - Read own annotations with inspect.get_annotations in to_dict(classes=...). - Compare defaults with dataclasses.MISSING: dataclasses._MISSING_TYPE is gone in Python 3.15. - Pass type_params to typing._eval_type on 3.13+, which 3.15 requires. Signed-off-by: cristianchiriac <80774326+cristianchiriac@users.noreply.github.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
import unitxtfails on Python 3.14 and 3.15, and CI doesn't catch it because it only runs older versions.Python 3.14 (PEP 649/749): a class body no longer puts
__annotations__in the class namespace, only an__annotate__function.get_fields()readattrs.get("__annotations__", {}), so no fields were found. The firstDataclassbuilt with keyword arguments then raised at import:get_fields()now uses_namespace_annotations(attrs). It keeps__annotations__when the namespace has it (all versions before 3.14), and otherwise usesannotationlib.get_annotate_from_class_namespace()+call_annotate_function(..., Format.FORWARDREF), the documented way for metaclasses.Dataclass.to_dict(classes=...)readcls.__annotations__, which no longer resolves through an instance on 3.14. It now usesinspect.get_annotations()on the class (falling back to the class__dict__before 3.10).Python 3.15:
dataclasses._MISSING_TYPEis gone (MISSINGbecame a PEP 661 sentinel), soupdate_init_signaturecompares withdataclasses.MISSING.typing._eval_type()now requirestype_params, soeval_forward_refpassestype_params=()on 3.13+. (3.13/3.14 accept it.)Tests:
test_fields_from_annotations: fields of a base and a child class are collected, and keyword init works.test_eval_forward_ref: plain and custom forward refs.Results for
pytest tests/library/test_dataclass.py tests/library/test_type_utils.py(pip install -e ., Dockerpython:<v>):test_parse_malformed_string)import unitxtfailsimport unitxtfailstest_parse_malformed_stringalready fails onmainwith 3.12. The 4 other failures on 3.14/3.15 (test_parse_union_type,test_format_type_string,test_parse_type_string_with_literal,test_to_string_nested_generic_types) are in type-string parsing: on 3.14,safe_evalof union types raisesKeyError: '__import__'with the empty__builtins__. That needs its own change, so I left it out of this PR.ruff/ruff format(v0.1.6, as in pre-commit) andcodespellare clean. Commits are signed off (DCO).