Skip to content

Commit

Permalink
relax linting on cloudpickling
Browse files Browse the repository at this point in the history
  • Loading branch information
samster25 committed Apr 15, 2024
1 parent d11f2a1 commit 48eff98
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ ignore = [
[lint.per-file-ignores]
# Do not enforce usage and import order rules in init files
"__init__.py" = ["E402", "F401", "I"]
# ignore vendored cloudpickle old python code
"daft/pickle/cloudpickle*" = ["UP036"]
# Allow wild imports in conftest
"tests/conftest.py" = ["F405", "E402", "F403"]
50 changes: 47 additions & 3 deletions daft/pickle/cloudpickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,17 @@ def cell_set(cell, value):
test and checker libraries decide to parse the whole file.
"""

cell.cell_contents = value
if sys.version_info[:2] >= (3, 7): # pragma: no branch
cell.cell_contents = value
else:
_cell_set = types.FunctionType(
_cell_set_template_code,
{},
"_cell_set",
(),
(cell,),
)
_cell_set(value)


def _make_cell_set_template_code():
Expand Down Expand Up @@ -467,6 +477,9 @@ def _cell_set_factory(value):
return _cell_set_template_code


if sys.version_info[:2] < (3, 7):
_cell_set_template_code = _make_cell_set_template_code()

# relevant opcodes
STORE_GLOBAL = opcode.opmap["STORE_GLOBAL"]
DELETE_GLOBAL = opcode.opmap["DELETE_GLOBAL"]
Expand Down Expand Up @@ -523,8 +536,39 @@ def _extract_class_dict(cls):
return clsdict


_is_parametrized_type_hint = None
_create_parametrized_type_hint = None
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. 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) and from_typing_extensions

# typing_extensions.Final
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
is_tuple = getattr(obj, "__tuple_params__", None) is not None
is_callable = getattr(obj, "__result__", None) is not None and getattr(obj, "__args__", None) is not None
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]

else:
_is_parametrized_type_hint = None
_create_parametrized_type_hint = None


def parametrized_type_hint_getinitargs(obj):
Expand Down

0 comments on commit 48eff98

Please sign in to comment.