Skip to content
Closed
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
Remove FreezableDefaultDict
Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Loading branch information
andersk committed Aug 27, 2021
commit b206ebc51956805f557c5eebdf3f2303bd5499f5
19 changes: 8 additions & 11 deletions importlib_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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__ = [
Expand Down Expand Up @@ -783,32 +783,29 @@ 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()
if low.endswith((".dist-info", ".egg-info")):
# 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())
)
Expand Down
23 changes: 0 additions & 23 deletions importlib_metadata/_collections.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down