Skip to content

Commit d947704

Browse files
committed
NormDict: try some cleanup to prevent memory leaks
1 parent 2bd9b21 commit d947704

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

schema_salad/ref_resolver.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,21 @@ def __init__(self, normalize: Callable[[str], str] = str) -> None:
112112
def __eq__(self, other: Any) -> bool:
113113
return super().__eq__(other)
114114

115-
def __getitem__(self, key): # type: (Any) -> Any
115+
def __getitem__(self, key: Any) -> Any:
116116
return super().__getitem__(self.normalize(key))
117117

118-
def __setitem__(self, key, value): # type: (Any, Any) -> Any
118+
def __setitem__(self, key: Any, value: Any) -> Any:
119119
return super().__setitem__(self.normalize(key), value)
120120

121-
def __delitem__(self, key): # type: (Any) -> Any
121+
def __delitem__(self, key: Any) -> Any:
122122
return super().__delitem__(self.normalize(key))
123123

124124
def __contains__(self, key: Any) -> bool:
125125
return super().__contains__(self.normalize(key))
126126

127+
def __del__(self) -> None:
128+
del self.normalize
129+
127130

128131
def SubLoader(loader: "Loader") -> "Loader":
129132
return Loader(
@@ -155,11 +158,11 @@ def __init__(
155158
allow_attachments: Optional[AttachmentsType] = None,
156159
doc_cache: Union[str, bool] = True,
157160
) -> None:
158-
self.idx = (
161+
self.idx: IdxType = (
159162
NormDict(lambda url: urllib.parse.urlsplit(url).geturl()) if idx is None else idx
160-
) # type: IdxType
163+
)
161164

162-
self.ctx = {} # type: ContextType
165+
self.ctx: ContextType = {}
163166
self.graph = schemagraph if schemagraph is not None else Graph()
164167
self.foreign_properties = (
165168
set(foreign_properties) if foreign_properties is not None else set()
@@ -187,20 +190,20 @@ def __init__(
187190
self.fetcher = self.fetcher_constructor(self.cache, self.session)
188191
self.fetch_text = self.fetcher.fetch_text
189192
self.check_exists = self.fetcher.check_exists
190-
self.url_fields = set() if url_fields is None else set(url_fields) # type: Set[str]
191-
self.scoped_ref_fields = {} # type: Dict[str, int]
192-
self.vocab_fields = set() # type: Set[str]
193-
self.identifiers = [] # type: List[str]
194-
self.identity_links = set() # type: Set[str]
193+
self.url_fields: Set[str] = set() if url_fields is None else set(url_fields)
194+
self.scoped_ref_fields: Dict[str, int] = {}
195+
self.vocab_fields: Set[str] = set()
196+
self.identifiers: List[str] = []
197+
self.identity_links: Set[str] = set()
195198
self.standalone: Optional[Set[str]] = None
196-
self.nolinkcheck = set() # type: Set[str]
197-
self.vocab = {} # type: Dict[str, str]
198-
self.rvocab = {} # type: Dict[str, str]
199-
self.idmap = {} # type: Dict[str, str]
200-
self.mapPredicate = {} # type: Dict[str, str]
201-
self.type_dsl_fields = set() # type: Set[str]
202-
self.subscopes = {} # type: Dict[str, str]
203-
self.secondaryFile_dsl_fields = set() # type: Set[str]
199+
self.nolinkcheck: Set[str] = set()
200+
self.vocab: Dict[str, str] = {}
201+
self.rvocab: Dict[str, str] = {}
202+
self.idmap: Dict[str, str] = {}
203+
self.mapPredicate: Dict[str, str] = {}
204+
self.type_dsl_fields: Set[str] = set()
205+
self.subscopes: Dict[str, str] = {}
206+
self.secondaryFile_dsl_fields: Set[str] = set()
204207
self.allow_attachments = allow_attachments
205208

206209
self.add_context(ctx)
@@ -475,9 +478,9 @@ def resolve_ref(
475478
if url in self.idx and (not mixin):
476479
resolved_obj = self.idx[url]
477480
if isinstance(resolved_obj, MutableMapping):
478-
metadata = self.idx.get(
481+
metadata: Union[CommentedMap, CommentedSeq, str, None] = self.idx.get(
479482
urllib.parse.urldefrag(url)[0], CommentedMap()
480-
) # type: Union[CommentedMap, CommentedSeq, str, None]
483+
)
481484
if isinstance(metadata, MutableMapping):
482485
if "$graph" in resolved_obj:
483486
metadata = _copy_dict_without_key(resolved_obj, "$graph")
@@ -658,7 +661,7 @@ def _secondaryFile_dsl(
658661
if not isinstance(t, str):
659662
return t
660663
pat = t[0:-1] if t.endswith("?") else t
661-
req = False if t.endswith("?") else None # type: Optional[bool]
664+
req: Optional[bool] = False if t.endswith("?") else None
662665

663666
second = CommentedMap((("pattern", pat), ("required", req)))
664667
second.lc.add_kv_line_col("pattern", lc)
@@ -714,7 +717,7 @@ def _resolve_dsl(
714717
datum2.append(self._apply_dsl(t, d, loader, LineCol(), ""))
715718
if isinstance(datum2, CommentedSeq):
716719
datum3 = CommentedSeq()
717-
seen = [] # type: List[str]
720+
seen: List[str] = []
718721
for i, item in enumerate(datum2):
719722
if isinstance(item, CommentedSeq):
720723
for j, v in enumerate(item):
@@ -907,7 +910,7 @@ def resolve_all(
907910

908911
try:
909912
for key, val in document.items():
910-
subscope = "" # type: str
913+
subscope: str = ""
911914
if key in loader.subscopes:
912915
subscope = "/" + loader.subscopes[key]
913916
document[key], _ = loader.resolve_all(

0 commit comments

Comments
 (0)