Skip to content

Commit 8a278a1

Browse files
Check annotations as strings for Python 3.10 (#400)
Co-authored-by: Olivier Grisel <[email protected]>
1 parent c68f41f commit 8a278a1

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

Diff for: .github/workflows/testing.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ jobs:
102102
run: |
103103
sudo add-apt-repository ppa:deadsnakes/nightly
104104
sudo apt update
105-
sudo apt install python3.9 python3.9-venv python3.9-dev
106-
python3.9 -m venv nightly-venv
105+
sudo apt install python3.10 python3.10-venv python3.10-dev
106+
python3.10 -m venv nightly-venv
107107
echo "$PWD/nightly-venv/bin" >> $GITHUB_PATH
108108
- name: Display Python version
109109
run: python -c "import sys; print(sys.version)"

Diff for: CHANGES.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
1.7.0 (in development)
22
======================
33

4+
dev
5+
===
6+
7+
- Support for pickling type annotations on Python 3.10 as per PEP 563:
8+
https://www.python.org/dev/peps/pep-0563/
9+
([PR #400](https://github.com/cloudpipe/cloudpickle/pull/400))
410

511
1.6.0
612
=====

Diff for: tests/cloudpickle_test.py

+25-7
Original file line numberDiff line numberDiff line change
@@ -2205,17 +2205,35 @@ def method(self, arg: type_) -> type_:
22052205
return arg
22062206
MyClass.__annotations__ = {'attribute': type_}
22072207

2208-
def check_annotations(obj, expected_type):
2208+
def check_annotations(obj, expected_type, expected_type_str):
22092209
assert obj.__annotations__["attribute"] == expected_type
2210-
assert obj.method.__annotations__["arg"] == expected_type
2211-
assert (
2212-
obj.method.__annotations__["return"] == expected_type
2213-
)
2210+
if sys.version_info >= (3, 10):
2211+
# In Python 3.10, type annotations are stored as strings.
2212+
# See PEP 563 for more details:
2213+
# https://www.python.org/dev/peps/pep-0563/
2214+
assert (
2215+
obj.method.__annotations__["arg"]
2216+
== expected_type_str
2217+
)
2218+
assert (
2219+
obj.method.__annotations__["return"]
2220+
== expected_type_str
2221+
)
2222+
else:
2223+
assert (
2224+
obj.method.__annotations__["arg"] == expected_type
2225+
)
2226+
assert (
2227+
obj.method.__annotations__["return"]
2228+
== expected_type
2229+
)
22142230
return "ok"
22152231

22162232
obj = MyClass()
2217-
assert check_annotations(obj, type_) == "ok"
2218-
assert worker.run(check_annotations, obj, type_) == "ok"
2233+
assert check_annotations(obj, type_, "type_") == "ok"
2234+
assert (
2235+
worker.run(check_annotations, obj, type_, "type_") == "ok"
2236+
)
22192237

22202238
def test_generic_extensions_literal(self):
22212239
typing_extensions = pytest.importorskip('typing_extensions')

0 commit comments

Comments
 (0)