Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix misdetection of some 3.6 typing[_extensions] #409

Merged
merged 4 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ dev
https://www.python.org/dev/peps/pep-0563/
([PR #400](https://github.com/cloudpipe/cloudpickle/pull/400))

- Stricter parametrized type detection heuristics in
_is_parametrized_type_hint to limit false positives.
([PR #409](https://github.com/cloudpipe/cloudpickle/pull/409))

1.6.0
=====

Expand Down
26 changes: 21 additions & 5 deletions cloudpickle/cloudpickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,15 +455,31 @@ def _extract_class_dict(cls):

if sys.version_info[:2] < (3, 7): # pragma: no branch
def _is_parametrized_type_hint(obj):
# This is very cheap but might generate false positives.
# This is very cheap but might generate false positives. So try to
# narrow it down is good as possible.
type_module = getattr(type(obj), '__module__', None)
from_typing_extensions = type_module == 'typing_extensions'
from_typing = type_module == 'typing'

# general typing Constructs
is_typing = getattr(obj, '__origin__', None) is not None

# typing_extensions.Literal
is_literal = getattr(obj, '__values__', None) is not None
is_literal = (
(getattr(obj, '__values__', None) is not None)
and from_typing_extensions
)

# typing_extensions.Final
is_final = getattr(obj, '__type__', None) is not None
is_final = (
(getattr(obj, '__type__', None) is not None)
and from_typing_extensions
)

# typing.ClassVar
is_classvar = (
(getattr(obj, '__type__', None) is not None) and from_typing
)

# typing.Union/Tuple for old Python 3.5
is_union = getattr(obj, '__union_params__', None) is not None
Expand All @@ -472,8 +488,8 @@ def _is_parametrized_type_hint(obj):
getattr(obj, '__result__', None) is not None and
getattr(obj, '__args__', None) is not None
)
return any((is_typing, is_literal, is_final, is_union, is_tuple,
is_callable))
return any((is_typing, is_literal, is_final, is_classvar, is_union,
is_tuple, is_callable))

def _create_parametrized_type_hint(origin, args):
return origin[args]
Expand Down
20 changes: 20 additions & 0 deletions tests/cloudpickle_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2301,6 +2301,26 @@ def reduce_myclass(x):
finally:
copyreg.dispatch_table.pop(MyClass)

def test_literal_misdetection(self):
# see https://github.com/cloudpipe/cloudpickle/issues/403
class MyClass:
@property
def __values__(self):
return ()

o = MyClass()
pickle_depickle(o, protocol=self.protocol)

def test_final_or_classvar_misdetection(self):
# see https://github.com/cloudpipe/cloudpickle/issues/403
class MyClass:
@property
def __type__(self):
return int

o = MyClass()
pickle_depickle(o, protocol=self.protocol)


class Protocol2CloudPickleTest(CloudPickleTest):

Expand Down