Skip to content

Commit d84930c

Browse files
committed
Freeze the defaultdict after construction and re-enable lazy evaluation of the search results.
1 parent 8e3e4af commit d84930c

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

importlib_metadata/__init__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
import contextlib
1515
import collections
1616

17+
from ._collections import freezable_defaultdict
1718
from ._compat import (
1819
NullFinder,
20+
Protocol,
1921
PyPy_repr,
2022
install,
21-
Protocol,
2223
)
23-
2424
from ._functools import method_cache
2525
from ._itertools import unique_everseen
2626

@@ -659,8 +659,8 @@ class Lookup:
659659
def __init__(self, path: FastPath):
660660
base = os.path.basename(path.root).lower()
661661
base_is_egg = base.endswith(".egg")
662-
self.infos = collections.defaultdict(list)
663-
self.eggs = collections.defaultdict(list)
662+
self.infos = freezable_defaultdict(list)
663+
self.eggs = freezable_defaultdict(list)
664664

665665
for child in path.children():
666666
low = child.lower()
@@ -674,6 +674,9 @@ def __init__(self, path: FastPath):
674674
legacy_normalized = Prepared.legacy_normalize(name)
675675
self.eggs[legacy_normalized].append(path.joinpath(child))
676676

677+
self.infos.freeze()
678+
self.eggs.freeze()
679+
677680
def search(self, prepared):
678681
infos = (
679682
self.infos[prepared.normalized]
@@ -685,7 +688,7 @@ def search(self, prepared):
685688
if prepared
686689
else itertools.chain.from_iterable(self.eggs.values())
687690
)
688-
return list(itertools.chain(infos, eggs))
691+
return itertools.chain(infos, eggs)
689692

690693

691694
class Prepared:

0 commit comments

Comments
 (0)