From c0192ec1bdafb1145367e862369d34c0b135214f Mon Sep 17 00:00:00 2001 From: Lumir Balhar Date: Wed, 25 Nov 2020 13:18:18 +0100 Subject: [PATCH 1/6] Check annotations as strings for Python 3.10 --- tests/cloudpickle_test.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/tests/cloudpickle_test.py b/tests/cloudpickle_test.py index a456b6372..e0b72caa1 100644 --- a/tests/cloudpickle_test.py +++ b/tests/cloudpickle_test.py @@ -2205,17 +2205,25 @@ 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') From acff0722d28df24eda21c384bfcdbcb7c2a97b53 Mon Sep 17 00:00:00 2001 From: Olivier Grisel Date: Sat, 27 Feb 2021 22:34:48 +0100 Subject: [PATCH 2/6] Trigger CI From e2a60096975843dee82be980fc68abb0e556ec48 Mon Sep 17 00:00:00 2001 From: Olivier Grisel Date: Mon, 1 Mar 2021 16:42:43 +0100 Subject: [PATCH 3/6] Try to use 3.10 in nightly CI job --- .github/workflows/testing.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 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)" From 2fb67927425e80f754d3e3932d5e53ec1498b175 Mon Sep 17 00:00:00 2001 From: Olivier Grisel Date: Mon, 1 Mar 2021 16:49:25 +0100 Subject: [PATCH 4/6] Apply suggestions from code review PEP8 --- tests/cloudpickle_test.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/tests/cloudpickle_test.py b/tests/cloudpickle_test.py index e0b72caa1..bf2e807ad 100644 --- a/tests/cloudpickle_test.py +++ b/tests/cloudpickle_test.py @@ -2209,21 +2209,31 @@ def check_annotations(obj, expected_type, expected_type_str): assert obj.__annotations__["attribute"] == 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 + # See PEP 563 for more details: + # https://www.python.org/dev/peps/pep-0563/ assert ( - obj.method.__annotations__["return"] == expected_type_str + 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 + obj.method.__annotations__["arg"] == expected_type + ) + assert ( + obj.method.__annotations__["return"] + == expected_type ) return "ok" obj = MyClass() assert check_annotations(obj, type_, "type_") == "ok" - assert worker.run(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') From e2ab0e6cade69654e8d42d5b89e230563d88f718 Mon Sep 17 00:00:00 2001 From: Olivier Grisel Date: Mon, 1 Mar 2021 17:03:12 +0100 Subject: [PATCH 5/6] Update tests/cloudpickle_test.py --- tests/cloudpickle_test.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/cloudpickle_test.py b/tests/cloudpickle_test.py index bf2e807ad..4e7eac466 100644 --- a/tests/cloudpickle_test.py +++ b/tests/cloudpickle_test.py @@ -2213,11 +2213,11 @@ def check_annotations(obj, expected_type, expected_type_str): # https://www.python.org/dev/peps/pep-0563/ assert ( obj.method.__annotations__["arg"] - == expected_type_str + == expected_type_str ) assert ( obj.method.__annotations__["return"] - == expected_type_str + == expected_type_str ) else: assert ( @@ -2225,7 +2225,7 @@ def check_annotations(obj, expected_type, expected_type_str): ) assert ( obj.method.__annotations__["return"] - == expected_type + == expected_type ) return "ok" From 8b10c816dbca1bb3c23f5c80fa1dca9f8b6de9dc Mon Sep 17 00:00:00 2001 From: Olivier Grisel Date: Mon, 1 Mar 2021 17:07:25 +0100 Subject: [PATCH 6/6] Add changelog entry --- CHANGES.md | 6 ++++++ 1 file changed, 6 insertions(+) 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 =====