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

Deprecate make_immutable in favor of frozendict.deepfreeze #13801

Merged
merged 4 commits into from
Apr 18, 2024
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
1 change: 1 addition & 0 deletions conda/auxlib/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .._vendor.frozendict import frozendict


@deprecated("24.9", "25.3", addendum="Use `frozendict.deepfreeze` instead.")
def make_immutable(value):
# this function is recursive, and if nested data structures fold back on themselves,
# there will likely be recursion errors
Expand Down
13 changes: 9 additions & 4 deletions conda/auxlib/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,22 @@
from boltons.timeutils import isoparse

from . import NULL
from .collection import AttrDict, make_immutable
from .compat import isiterable, odict
from .collection import AttrDict
from .exceptions import Raise, ValidationError
from .ish import find_or_raise
from .logz import DumpEncoder
from .type_coercion import maybecall

try:
from frozendict import frozendict
from frozendict import deepfreeze, frozendict, register as _register

# leave enums as is, deepfreeze will flatten it into a dict
# see https://github.com/Marco-Sulla/python-frozendict/issues/98
_register(Enum, lambda x : x)
except ImportError:
from .._vendor.frozendict import frozendict
from ..auxlib.collection import make_immutable as deepfreeze

Check warning on line 264 in conda/auxlib/entity.py

View check run for this annotation

Codecov / codecov/patch

conda/auxlib/entity.py#L264

Added line #L264 was not covered by tests

log = getLogger(__name__)

Expand Down Expand Up @@ -599,7 +604,7 @@
if isinstance(et, type) and issubclass(et, Entity):
return self._type(v if isinstance(v, et) else et(**v) for v in val)
else:
return make_immutable(val) if self.immutable else self._type(val)
return deepfreeze(val) if self.immutable else self._type(val)
else:
raise ValidationError(
val, msg=f"Cannot assign a non-iterable value to {self.name}"
Expand Down Expand Up @@ -650,7 +655,7 @@
if val is None:
return self._type()
elif isiterable(val):
val = make_immutable(val)
val = deepfreeze(val)

Check warning on line 658 in conda/auxlib/entity.py

View check run for this annotation

Codecov / codecov/patch

conda/auxlib/entity.py#L658

Added line #L658 was not covered by tests
if not isinstance(val, Mapping):
raise ValidationError(
val, msg=f"Cannot assign a non-iterable value to {self.name}"
Expand Down
23 changes: 14 additions & 9 deletions conda/common/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,35 @@
from string import Template
from typing import TYPE_CHECKING

from ..deprecations import deprecated

if TYPE_CHECKING: # pragma: no cover
from re import Match
from typing import Any, Hashable, Iterable, Sequence

from boltons.setutils import IndexedSet
from ruamel.yaml.comments import CommentedMap, CommentedSeq
from ruamel.yaml.reader import ReaderError
from ruamel.yaml.scanner import ScannerError

from .. import CondaError, CondaMultiError
from ..auxlib.collection import AttrDict, first, last, make_immutable
from ..auxlib.collection import AttrDict, first, last
from ..auxlib.exceptions import ThisShouldNeverHappenError
from ..auxlib.type_coercion import TypeCoercionError, typify, typify_data_structure
from ..common.iterators import unique
from ..deprecations import deprecated
from .compat import isiterable, primitive_types
from .constants import NULL
from .serialize import yaml_round_trip_load

try:
from frozendict import frozendict
from frozendict import deepfreeze, frozendict
from frozendict import register as _register

# leave enums as is, deepfreeze will flatten it into a dict
# see https://github.com/Marco-Sulla/python-frozendict/issues/98
_register(Enum, lambda x: x)
except ImportError:
from .._vendor.frozendict import frozendict
from ..auxlib.collection import make_immutable as deepfreeze

Check warning on line 57 in conda/common/configuration.py

View check run for this annotation

Codecov / codecov/patch

conda/common/configuration.py#L57

Added line #L57 was not covered by tests

if TYPE_CHECKING:
from re import Match
from typing import Any, Hashable, Iterable, Sequence

log = getLogger(__name__)

Expand Down Expand Up @@ -266,7 +271,7 @@
)
return tuple(children_values)
else:
return make_immutable(self._raw_value)
return deepfreeze(self._raw_value)

def keyflag(self):
return None
Expand Down
19 changes: 19 additions & 0 deletions news/13801-deprecate-make_immutable
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Enhancements

* <news item>

### Bug fixes

* <news item>

### Deprecations

* Mark `conda.auxlib.collections.make_immutable` as pending deprecation. Use `frozendict.deepfreeze` instead. (#13801)

### Docs

* <news item>

### Other

* <news item>
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dependencies = [
"conda-libmamba-solver >=23.11.0",
"conda-package-handling >=2.2.0",
"distro >=1.5.0",
"frozendict >=1.2",
"frozendict >=2.4.2",
"jsonpatch >=1.32",
"menuinst >=2",
"packaging >=23.0",
Expand Down
2 changes: 1 addition & 1 deletion recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ requirements:
- conda-libmamba-solver >=23.11.0
- conda-package-handling >=2.2.0
- distro >=1.5.0
- frozendict >=1.2
- frozendict >=2.4.2
- jsonpatch >=1.32
- menuinst >=2
- packaging >=23.0
Expand Down
2 changes: 1 addition & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ conda-content-trust >=0.2.0 # for signature verification
conda-libmamba-solver >=23.11.0
conda-package-handling >=2.2.0
distro >=1.5.0
frozendict >=1.2
frozendict >=2.4.2
jsonpatch >=1.32 # jlap
menuinst >=2
packaging >=23.0
Expand Down
44 changes: 44 additions & 0 deletions tests/test_auxlib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
from __future__ import annotations

from enum import Enum
from typing import TYPE_CHECKING

import pytest
from frozendict import deepfreeze

from conda.auxlib.collection import make_immutable
from conda.base.constants import UpdateModifier

if TYPE_CHECKING:
from typing import Any


IMMUTABLE = (
"text",
42,
42.0,
True,
UpdateModifier.FREEZE_INSTALLED,
(1, 1, 2),
frozenset({1, 1, 2}),
)
MUTABLE = ([1, 1, 2], {1, 1, 2}, {1: 1, 2: 2})


@pytest.mark.parametrize(
"unfrozen",
[
*IMMUTABLE,
*MUTABLE,
# make_immutable leaves a tuple as is, deepfreeze does a better job
# (*IMMUTABLE, *MUTABLE),
[*IMMUTABLE, *MUTABLE],
{*IMMUTABLE},
frozenset({*IMMUTABLE}),
dict(enumerate((*IMMUTABLE, *MUTABLE))),
],
)
def test_deepfreeze(unfrozen: Any) -> None:
assert make_immutable(unfrozen) == deepfreeze(unfrozen, {Enum: lambda x: x})
Loading