|
40 | 40 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
41 | 41 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
42 | 42 | """ |
43 | | -from __future__ import print_function |
44 | 43 |
|
45 | 44 | import builtins |
46 | 45 | import dis |
|
56 | 55 |
|
57 | 56 | from .compat import pickle |
58 | 57 | from collections import OrderedDict |
59 | | -from typing import Generic, Union, Tuple, Callable |
| 58 | +from typing import ClassVar, Generic, Union, Tuple, Callable |
60 | 59 | from pickle import _getattribute |
61 | 60 | from importlib._bootstrap import _find_spec |
62 | 61 |
|
|
66 | 65 | except ImportError: |
67 | 66 | _typing_extensions = Literal = Final = None |
68 | 67 |
|
69 | | -if sys.version_info >= (3, 5, 3): |
70 | | - from typing import ClassVar |
71 | | -else: # pragma: no cover |
72 | | - ClassVar = None |
73 | | - |
74 | 68 | if sys.version_info >= (3, 8): |
75 | 69 | from types import CellType |
76 | 70 | else: |
@@ -604,43 +598,21 @@ def parametrized_type_hint_getinitargs(obj): |
604 | 598 | elif type(obj) is type(ClassVar): |
605 | 599 | initargs = (ClassVar, obj.__type__) |
606 | 600 | elif type(obj) is type(Generic): |
607 | | - parameters = obj.__parameters__ |
608 | | - if len(obj.__parameters__) > 0: |
609 | | - # in early Python 3.5, __parameters__ was sometimes |
610 | | - # preferred to __args__ |
611 | | - initargs = (obj.__origin__, parameters) |
612 | | - |
613 | | - else: |
614 | | - initargs = (obj.__origin__, obj.__args__) |
| 601 | + initargs = (obj.__origin__, obj.__args__) |
615 | 602 | elif type(obj) is type(Union): |
616 | | - if sys.version_info < (3, 5, 3): # pragma: no cover |
617 | | - initargs = (Union, obj.__union_params__) |
618 | | - else: |
619 | | - initargs = (Union, obj.__args__) |
| 603 | + initargs = (Union, obj.__args__) |
620 | 604 | elif type(obj) is type(Tuple): |
621 | | - if sys.version_info < (3, 5, 3): # pragma: no cover |
622 | | - initargs = (Tuple, obj.__tuple_params__) |
623 | | - else: |
624 | | - initargs = (Tuple, obj.__args__) |
| 605 | + initargs = (Tuple, obj.__args__) |
625 | 606 | elif type(obj) is type(Callable): |
626 | | - if sys.version_info < (3, 5, 3): # pragma: no cover |
627 | | - args = obj.__args__ |
628 | | - result = obj.__result__ |
629 | | - if args != Ellipsis: |
630 | | - if isinstance(args, tuple): |
631 | | - args = list(args) |
632 | | - else: |
633 | | - args = [args] |
| 607 | + (*args, result) = obj.__args__ |
| 608 | + if len(args) == 1 and args[0] is Ellipsis: |
| 609 | + args = Ellipsis |
634 | 610 | else: |
635 | | - (*args, result) = obj.__args__ |
636 | | - if len(args) == 1 and args[0] is Ellipsis: |
637 | | - args = Ellipsis |
638 | | - else: |
639 | | - args = list(args) |
| 611 | + args = list(args) |
640 | 612 | initargs = (Callable, (args, result)) |
641 | 613 | else: # pragma: no cover |
642 | 614 | raise pickle.PicklingError( |
643 | | - "Cloudpickle Error: Unknown type {}".format(type(obj)) |
| 615 | + f"Cloudpickle Error: Unknown type {type(obj)}" |
644 | 616 | ) |
645 | 617 | return initargs |
646 | 618 |
|
@@ -720,7 +692,7 @@ def instance(cls): |
720 | 692 |
|
721 | 693 |
|
722 | 694 | @instance |
723 | | -class _empty_cell_value(object): |
| 695 | +class _empty_cell_value: |
724 | 696 | """sentinel for empty closures |
725 | 697 | """ |
726 | 698 | @classmethod |
@@ -749,7 +721,7 @@ def _fill_function(*args): |
749 | 721 | keys = ['globals', 'defaults', 'dict', 'module', 'closure_values'] |
750 | 722 | state = dict(zip(keys, args[1:])) |
751 | 723 | else: |
752 | | - raise ValueError('Unexpected _fill_value arguments: %r' % (args,)) |
| 724 | + raise ValueError(f'Unexpected _fill_value arguments: {args!r}') |
753 | 725 |
|
754 | 726 | # - At pickling time, any dynamic global variable used by func is |
755 | 727 | # serialized by value (in state['globals']). |
@@ -917,15 +889,10 @@ def _make_typevar(name, bound, constraints, covariant, contravariant, |
917 | 889 |
|
918 | 890 |
|
919 | 891 | def _decompose_typevar(obj): |
920 | | - try: |
921 | | - class_tracker_id = _get_or_create_tracker_id(obj) |
922 | | - except TypeError: # pragma: nocover |
923 | | - # TypeVar instances are not weakref-able in Python 3.5.3 |
924 | | - class_tracker_id = None |
925 | 892 | return ( |
926 | 893 | obj.__name__, obj.__bound__, obj.__constraints__, |
927 | 894 | obj.__covariant__, obj.__contravariant__, |
928 | | - class_tracker_id, |
| 895 | + _get_or_create_tracker_id(obj), |
929 | 896 | ) |
930 | 897 |
|
931 | 898 |
|
|
0 commit comments