Skip to content

Commit

Permalink
🔧 MAINTAIN: Remove frozendict dependency (#204)
Browse files Browse the repository at this point in the history
This package has been unmaintained for years,
and now it raises issues with python 3.8
and will not work anymore in python 3.10
  • Loading branch information
chrisjsewell authored Feb 9, 2021
1 parent 9240fe6 commit bf94508
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ repos:
hooks:
- id: pylint
additional_dependencies: [
"frozendict~=1.2", "pyyaml~=5.1.2", "nest_asyncio~=1.4.0", "aio-pika~=6.6",
"pyyaml~=5.1.2", "nest_asyncio~=1.4.0", "aio-pika~=6.6",
"aiocontextvars~=0.2.2; python_version<'3.7'", "kiwipy[rmq]~=0.7.1"
]
args:
Expand Down
45 changes: 42 additions & 3 deletions plumpy/utils.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# -*- coding: utf-8 -*-
from collections import deque
from collections.abc import Mapping
import functools
import importlib
import inspect
import logging
import types
from typing import Any, Callable, Hashable, List, MutableMapping, Optional, Tuple, Type, TYPE_CHECKING
from typing import Any, Callable, Hashable, Iterator, List, MutableMapping, Optional, Tuple, Type, TYPE_CHECKING
from typing import Set # pylint: disable=unused-import

import asyncio
import frozendict

from .settings import check_protected, check_override
from . import lang
Expand Down Expand Up @@ -69,7 +69,46 @@ def fire_event(self, event_function: Callable[..., Any], *args: Any, **kwargs: A
_LOGGER.error("Listener '%s' produced an exception:\n%s", listener, exception)


class AttributesFrozendict(frozendict.frozendict):
class Frozendict(Mapping):
"""
An immutable wrapper around dictionaries that implements the complete :py:class:`collections.abc.Mapping`
interface. It can be used as a drop-in replacement for dictionaries where immutability is desired.
Adapted from: slezica/python-frozendict
"""

def __init__(self, *args: Any, **kwargs: Any) -> None:
self._dict = dict(*args, **kwargs)
self._hash: Optional[int] = None

def __getitem__(self, key: str) -> Any:
return self._dict[key]

def __contains__(self, key: Any) -> bool:
return key in self._dict

def copy(self, **add_or_replace: Any) -> 'Frozendict':
return self.__class__(self, **add_or_replace)

def __iter__(self) -> Iterator[str]:
return iter(self._dict)

def __len__(self) -> int:
return len(self._dict)

def __repr__(self) -> str:
return f'<{self.__class__.__name__} {self._dict!r}>'

def __hash__(self) -> int:
if self._hash is None:
hashed = 0
for key, value in self._dict.items():
hashed ^= hash((key, value))
self._hash = hashed
return self._hash


class AttributesFrozendict(Frozendict):

def __init__(self, *args: Any, **kwargs: Any):
super().__init__(*args, **kwargs)
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
keywords='workflow multithreaded rabbitmq',
python_requires='>=3.6',
install_requires=[
'frozendict~=1.2', 'pyyaml~=5.1.2', 'nest_asyncio~=1.4.0', 'aio-pika~=6.6',
'aiocontextvars~=0.2.2; python_version<"3.7"', 'kiwipy[rmq]~=0.7.1'
'pyyaml~=5.1.2', 'nest_asyncio~=1.4.0', 'aio-pika~=6.6', 'aiocontextvars~=0.2.2; python_version<"3.7"',
'kiwipy[rmq]~=0.7.1'
],
extras_require={
'docs': ['sphinx~=3.2.0', 'myst-nb~=0.11.0', 'sphinx-book-theme~=0.0.39', 'ipython~=7.0'],
Expand Down
3 changes: 2 additions & 1 deletion test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""Utilities for tests"""
import asyncio
import collections
from collections.abc import Mapping
import shortuuid
import unittest

Expand Down Expand Up @@ -370,7 +371,7 @@ def compare_dictionaries(bundle1, bundle2, dict1, dict2, exclude=None):


def compare_value(bundle1, bundle2, v1, v2, exclude=None):
if isinstance(v1, collections.Mapping) and isinstance(v2, collections.Mapping):
if isinstance(v1, Mapping) and isinstance(v2, Mapping):
compare_dictionaries(bundle1, bundle2, v1, v2, exclude)
elif isinstance(v1, list) and isinstance(v2, list):
for vv1, vv2 in zip(v1, v2):
Expand Down

0 comments on commit bf94508

Please sign in to comment.