bpo-32873: Treat type variables and special typing forms as immutable by copy and pickle (GH-6216)

This also fixes python/typing#512
This also fixes python/typing#511

As was discussed in both issues, some typing forms deserve to be treated
as immutable by copy and pickle modules, so that:
* copy(X) is X
* deepcopy(X) is X
* loads(dumps(X)) is X  # pickled by reference

This PR adds such behaviour to:
* Type variables
* Special forms like Union, Any, ClassVar
* Unsubscripted generic aliases to containers like List, Mapping, Iterable

This not only resolves inconsistencies mentioned in the issues, but also
improves backwards compatibility with previous versions of Python
(including 3.6).

Note that this requires some dances with __module__ for type variables
(similar to NamedTuple) because the class TypeVar itself is define in typing,
while type variables should get module where they were defined.

https://bugs.python.org/issue32873
This commit is contained in:
Ivan Levkivskyi 2018-03-26 23:01:12 +01:00 committed by GitHub
parent 0e7144b064
commit 834940375a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 11 deletions

View file

@ -0,0 +1,3 @@
Treat type variables and special typing forms as immutable by copy and
pickle. This fixes several minor issues and inconsistencies, and improves
backwards compatibility with Python 3.6.