Skip to content

Commit

Permalink
Presented FreezableDefaultDict from jaraco.collections. Keep inline t…
Browse files Browse the repository at this point in the history
…o minimize dependencies.
  • Loading branch information
jaraco committed Mar 28, 2021
1 parent d84930c commit a6bff7e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
6 changes: 3 additions & 3 deletions importlib_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import contextlib
import collections

from ._collections import freezable_defaultdict
from ._collections import FreezableDefaultDict
from ._compat import (
NullFinder,
Protocol,
Expand Down Expand Up @@ -659,8 +659,8 @@ class Lookup:
def __init__(self, path: FastPath):
base = os.path.basename(path.root).lower()
base_is_egg = base.endswith(".egg")
self.infos = freezable_defaultdict(list)
self.eggs = freezable_defaultdict(list)
self.infos = FreezableDefaultDict(list)
self.eggs = FreezableDefaultDict(list)

for child in path.children():
low = child.lower()
Expand Down
9 changes: 6 additions & 3 deletions importlib_metadata/_collections.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import collections


class freezable_defaultdict(collections.defaultdict):
# from jaraco.collections 3.3
class FreezableDefaultDict(collections.defaultdict):
"""
Mix-in to freeze a 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 = freezable_defaultdict(list)
>>> dd = FreezableDefaultDict(list)
>>> dd[0].append('1')
>>> dd.freeze()
>>> dd[1]
Expand Down

0 comments on commit a6bff7e

Please sign in to comment.