From b206ebc51956805f557c5eebdf3f2303bd5499f5 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Thu, 26 Aug 2021 17:49:15 -0700 Subject: [PATCH] Remove FreezableDefaultDict Signed-off-by: Anders Kaseorg --- importlib_metadata/__init__.py | 19 ++++++++----------- importlib_metadata/_collections.py | 23 ----------------------- 2 files changed, 8 insertions(+), 34 deletions(-) diff --git a/importlib_metadata/__init__.py b/importlib_metadata/__init__.py index 6c554558..34fa389f 100644 --- a/importlib_metadata/__init__.py +++ b/importlib_metadata/__init__.py @@ -15,7 +15,7 @@ import collections from . import _adapters, _meta -from ._collections import FreezableDefaultDict, Pair +from ._collections import Pair from ._compat import ( NullFinder, PyPy_repr, @@ -30,7 +30,7 @@ from importlib import import_module from importlib.abc import MetaPathFinder from itertools import starmap -from typing import List, Mapping, Optional, Union +from typing import Dict, List, Mapping, Optional, Union __all__ = [ @@ -783,8 +783,8 @@ class Lookup: def __init__(self, path: FastPath): base = os.path.basename(path.root).lower() base_is_egg = base.endswith(".egg") - self.infos = FreezableDefaultDict(list) - self.eggs = FreezableDefaultDict(list) + self.infos: Dict[Optional[str], List[pathlib.Path]] = {} + self.eggs: Dict[Optional[str], List[pathlib.Path]] = {} for child in path.children(): low = child.lower() @@ -792,23 +792,20 @@ def __init__(self, path: FastPath): # rpartition is faster than splitext and suitable for this purpose. name = low.rpartition(".")[0].partition("-")[0] normalized = Prepared.normalize(name) - self.infos[normalized].append(path.joinpath(child)) + self.infos.setdefault(normalized, []).append(path.joinpath(child)) elif base_is_egg and low == "egg-info": name = base.rpartition(".")[0].partition("-")[0] legacy_normalized = Prepared.legacy_normalize(name) - self.eggs[legacy_normalized].append(path.joinpath(child)) - - self.infos.freeze() - self.eggs.freeze() + self.eggs.setdefault(legacy_normalized, []).append(path.joinpath(child)) def search(self, prepared): infos = ( - self.infos[prepared.normalized] + self.infos.get(prepared.normalized, []) if prepared else itertools.chain.from_iterable(self.infos.values()) ) eggs = ( - self.eggs[prepared.legacy_normalized] + self.eggs.get(prepared.legacy_normalized, []) if prepared else itertools.chain.from_iterable(self.eggs.values()) ) diff --git a/importlib_metadata/_collections.py b/importlib_metadata/_collections.py index cf0954e1..359b3ba0 100644 --- a/importlib_metadata/_collections.py +++ b/importlib_metadata/_collections.py @@ -1,29 +1,6 @@ import collections -# from jaraco.collections 3.3 -class FreezableDefaultDict(collections.defaultdict): - """ - Often it is desirable to prevent the mutation of - a default dict after its initial construction, such - as to prevent mutation during iteration. - - >>> dd = FreezableDefaultDict(list) - >>> dd[0].append('1') - >>> dd.freeze() - >>> dd[1] - [] - >>> len(dd) - 1 - """ - - def __missing__(self, key): - return getattr(self, '_frozen', super().__missing__)(key) - - def freeze(self): - self._frozen = lambda key: self.default_factory() - - class Pair(collections.namedtuple('Pair', 'name value')): @classmethod def parse(cls, text):