Skip to content

Commit

Permalink
Check annotations as strings for Python 3.10 (#400)
Browse files Browse the repository at this point in the history
Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
  • Loading branch information
frenzymadness and ogrisel committed Mar 1, 2021
1 parent c68f41f commit 8a278a1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/testing.yml
Expand Up @@ -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)"
Expand Down
6 changes: 6 additions & 0 deletions 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
=====
Expand Down
32 changes: 25 additions & 7 deletions tests/cloudpickle_test.py
Expand Up @@ -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')
Expand Down

0 comments on commit 8a278a1

Please sign in to comment.