-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add typing / drop Python < 3.8 support (#30)
since python2 and many versions of 3.7 are end-of-life, and type hints would be be valuable for the developer experience, remove support for these EOL versions of Python while adding type hints. Fixes: #27 At a high level the following changes have been made: - Update code to use new features available from 3.8 (e.g. `f"{foo=}` notation). - Type annotations - note: Because we use `from __future__ import annotations` we can use "new style" annotations (e.g. `str | list[int] | None` instead of `Optional[Union[str, List[int]]]`) which are normally only available in higher versions of python. However it does mean that these annotations cannot be resolved from their string representation (all type annotations are stored as strings which is why this works) into their concrete classes unless you are running a supported version of Python. - Completely move to `pyrproject.toml` - Move all development dependencies to `pyproject.toml`, make `make .venv` more portable - `virtualenv` isn't always available so use `-m venv .venv` which is just as effective, and not all systems have `python(2)` installed or `python3-is-python` so explicitly use `python3` - Add some new badges to README
- Loading branch information
Showing
21 changed files
with
276 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
include VERSION | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
.venv: pyproject.toml | ||
python -m virtualenv .venv | ||
python3 -m venv .venv | ||
|
||
.venv/deps: .venv pyproject.toml setup.cfg | ||
.venv/bin/python -m pip install . build pytest twine | ||
.venv/deps: .venv pyproject.toml | ||
.venv/bin/python -m pip install .[dev] | ||
touch .venv/deps | ||
|
||
build: .venv/deps | ||
rm -rf ./dist/ | ||
.venv/bin/python -m build . | ||
.venv/bin/python -m build | ||
|
||
# only works with python 3+ | ||
lint: .venv/deps | ||
.venv/bin/python -m pip install black==22.3.0 | ||
.venv/bin/python -m black --check . | ||
.venv/bin/validate-pyproject pyproject.toml | ||
.venv/bin/black --check deepmerge | ||
.venv/bin/mypy deepmerge | ||
|
||
test: .venv/deps | ||
.venv/bin/python -m pytest deepmerge | ||
.venv/bin/pytest deepmerge | ||
|
||
ready-pr: test lint | ||
ready-pr: test lint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,35 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
import deepmerge.merger | ||
|
||
|
||
class DeepMergeException(Exception): | ||
pass | ||
"Base class for all `deepmerge` Exceptions" | ||
|
||
|
||
class StrategyNotFound(DeepMergeException): | ||
pass | ||
"Exception for when a strategy cannot be located" | ||
|
||
|
||
class InvalidMerge(DeepMergeException): | ||
def __init__(self, strategy_list_name, merge_args, merge_kwargs): | ||
super(InvalidMerge, self).__init__( | ||
"no more strategies found for {0} and arguments {1}, {2}".format( | ||
strategy_list_name, merge_args, merge_kwargs | ||
) | ||
"Exception for when unable to complete a merge operation" | ||
|
||
def __init__( | ||
self, | ||
strategy_list_name: str, | ||
config: deepmerge.merger.Merger, | ||
path: list, | ||
base: Any, | ||
nxt: Any, | ||
) -> None: | ||
super().__init__( | ||
f"Could not merge using {strategy_list_name!r} [{config=}, {path=}, {base=}, {nxt=}]" | ||
) | ||
self.strategy_list_name = strategy_list_name | ||
self.merge_args = merge_args | ||
self.merge_kwargs = merge_kwargs | ||
self.config = config | ||
self.path = path | ||
self.base = base | ||
self.nxt = nxt | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,60 @@ | ||
from .strategy.list import ListStrategies | ||
from .strategy.dict import DictStrategies | ||
from .strategy.set import SetStrategies | ||
from .strategy.type_conflict import TypeConflictStrategies | ||
from .strategy.fallback import FallbackStrategies | ||
from __future__ import annotations | ||
|
||
from typing import Any, Sequence, Callable | ||
|
||
class Merger(object): | ||
from . import strategy as s | ||
|
||
|
||
class Merger: | ||
""" | ||
Merges objects based on provided strategies | ||
:param type_strategies, List[Tuple]: a list of (Type, Strategy) pairs | ||
that should be used against incoming types. For example: (dict, "override"). | ||
""" | ||
|
||
PROVIDED_TYPE_STRATEGIES = { | ||
list: ListStrategies, | ||
dict: DictStrategies, | ||
set: SetStrategies, | ||
PROVIDED_TYPE_STRATEGIES: dict[type, type[s.StrategyList]] = { | ||
list: s.ListStrategies, | ||
dict: s.DictStrategies, | ||
set: s.SetStrategies, | ||
} | ||
|
||
def __init__(self, type_strategies, fallback_strategies, type_conflict_strategies): | ||
self._fallback_strategy = FallbackStrategies(fallback_strategies) | ||
def __init__( | ||
self, | ||
type_strategies: Sequence[tuple[type, s.StrategyCallable | s.StrategyListInitable]], | ||
fallback_strategies: s.StrategyListInitable, | ||
type_conflict_strategies: s.StrategyListInitable, | ||
) -> None: | ||
self._fallback_strategy = s.FallbackStrategies(fallback_strategies) | ||
self._type_conflict_strategy = s.TypeConflictStrategies(type_conflict_strategies) | ||
|
||
expanded_type_strategies = [] | ||
self._type_strategies: list[tuple[type, s.StrategyCallable]] = [] | ||
for typ, strategy in type_strategies: | ||
if typ in self.PROVIDED_TYPE_STRATEGIES: | ||
strategy = self.PROVIDED_TYPE_STRATEGIES[typ](strategy) | ||
expanded_type_strategies.append((typ, strategy)) | ||
self._type_strategies = expanded_type_strategies | ||
|
||
self._type_conflict_strategy = TypeConflictStrategies(type_conflict_strategies) | ||
|
||
def merge(self, base, nxt): | ||
# Customise a StrategyList instance for this type | ||
self._type_strategies.append((typ, self.PROVIDED_TYPE_STRATEGIES[typ](strategy))) | ||
elif callable(strategy): | ||
self._type_strategies.append((typ, strategy)) | ||
else: | ||
raise ValueError(f"Cannot handle ({typ}, {strategy})") | ||
return | ||
|
||
def merge(self, base: Any, nxt: Any) -> Any: | ||
return self.value_strategy([], base, nxt) | ||
|
||
def type_conflict_strategy(self, *args): | ||
return self._type_conflict_strategy(self, *args) | ||
def type_conflict_strategy(self, path: list, base: Any, nxt: Any) -> Any: | ||
return self._type_conflict_strategy(self, path, base, nxt) | ||
|
||
def value_strategy(self, path, base, nxt): | ||
def value_strategy(self, path: list, base: Any, nxt: Any) -> Any: | ||
# Check for strategy based on type of base, next | ||
for typ, strategy in self._type_strategies: | ||
if isinstance(base, typ) and isinstance(nxt, typ): | ||
# We have a strategy for this type | ||
return strategy(self, path, base, nxt) | ||
if not (isinstance(base, type(nxt)) or isinstance(nxt, type(base))): | ||
return self.type_conflict_strategy(path, base, nxt) | ||
return self._fallback_strategy(self, path, base, nxt) | ||
|
||
if isinstance(base, type(nxt)) or isinstance(nxt, type(base)): | ||
# no known strategy but base, next are similar types | ||
return self._fallback_strategy(self, path, base, nxt) | ||
|
||
# No known strategy and base, next are different types. | ||
return self.type_conflict_strategy(path, base, nxt) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from .core import StrategyList, StrategyCallable, StrategyListInitable | ||
from .dict import DictStrategies | ||
from .fallback import FallbackStrategies | ||
from .list import ListStrategies | ||
from .set import SetStrategies | ||
from .type_conflict import TypeConflictStrategies |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.