From 8a278a12aa3e469ea727861be2c8627d971a31e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lum=C3=ADr=20=27Frenzy=27=20Balhar?= Date: Mon, 1 Mar 2021 17:21:58 +0100 Subject: [PATCH] Check annotations as strings for Python 3.10 (#400) Co-authored-by: Olivier Grisel --- .github/workflows/testing.yml | 4 ++-- CHANGES.md | 6 ++++++ tests/cloudpickle_test.py | 32 +++++++++++++++++++++++++------- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index 8d98bc930..8dbf357f9 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -102,8 +102,8 @@ jobs: run: | sudo add-apt-repository ppa:deadsnakes/nightly sudo apt update - sudo apt install python3.9 python3.9-venv python3.9-dev - python3.9 -m venv nightly-venv + sudo apt install python3.10 python3.10-venv python3.10-dev + python3.10 -m venv nightly-venv echo "$PWD/nightly-venv/bin" >> $GITHUB_PATH - name: Display Python version run: python -c "import sys; print(sys.version)" diff --git a/CHANGES.md b/CHANGES.md index 0335c694d..4eda950fd 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,12 @@ 1.7.0 (in development) ====================== +dev +=== + +- Support for pickling type annotations on Python 3.10 as per PEP 563: + https://www.python.org/dev/peps/pep-0563/ + ([PR #400](https://github.com/cloudpipe/cloudpickle/pull/400)) 1.6.0 ===== diff --git a/tests/cloudpickle_test.py b/tests/cloudpickle_test.py index a456b6372..4e7eac466 100644 --- a/tests/cloudpickle_test.py +++ b/tests/cloudpickle_test.py @@ -2205,17 +2205,35 @@ def method(self, arg: type_) -> type_: return arg MyClass.__annotations__ = {'attribute': type_} - def check_annotations(obj, expected_type): + def check_annotations(obj, expected_type, expected_type_str): assert obj.__annotations__["attribute"] == expected_type - assert obj.method.__annotations__["arg"] == expected_type - assert ( - obj.method.__annotations__["return"] == expected_type - ) + if sys.version_info >= (3, 10): + # In Python 3.10, type annotations are stored as strings. + # See PEP 563 for more details: + # https://www.python.org/dev/peps/pep-0563/ + assert ( + obj.method.__annotations__["arg"] + == expected_type_str + ) + assert ( + obj.method.__annotations__["return"] + == expected_type_str + ) + else: + assert ( + obj.method.__annotations__["arg"] == expected_type + ) + assert ( + obj.method.__annotations__["return"] + == expected_type + ) return "ok" obj = MyClass() - assert check_annotations(obj, type_) == "ok" - assert worker.run(check_annotations, obj, type_) == "ok" + assert check_annotations(obj, type_, "type_") == "ok" + assert ( + worker.run(check_annotations, obj, type_, "type_") == "ok" + ) def test_generic_extensions_literal(self): typing_extensions = pytest.importorskip('typing_extensions')