7
7
"""
8
8
9
9
from copy import deepcopy
10
- from typing import Dict , List , Set
10
+ from typing import Dict , List , Set , Optional
11
11
import ast
12
12
import collections
13
+ import functools
13
14
import importlib
14
15
import inspect
15
16
import os
111
112
IGNORE_COMMENT_PATTERN = re .compile ("(?:.* numpydoc ignore[=|:] ?)(.+)" )
112
113
113
114
114
- def extract_ignore_validation_comments (filepath : os .PathLike ) -> Dict [int , List [str ]]:
115
+ # This function gets called once per function/method to be validated.
116
+ # We have to balance memory usage with performance here. It shouldn't be too
117
+ # bad to store these `dict`s (they should be rare), but to be safe let's keep
118
+ # the limit low-ish. This was set by looking at scipy, numpy, matplotlib,
119
+ # and pandas and they had between ~500 and ~1300 .py files as of 2023-08-16.
120
+ @functools .lru_cache (maxsize = 2000 )
121
+ def extract_ignore_validation_comments (
122
+ filepath : Optional [os .PathLike ],
123
+ ) -> Dict [int , List [str ]]:
115
124
"""
116
125
Extract inline comments indicating certain validation checks should be ignored.
117
126
@@ -125,8 +134,12 @@ def extract_ignore_validation_comments(filepath: os.PathLike) -> Dict[int, List[
125
134
dict[int, list[str]]
126
135
Mapping of line number to a list of checks to ignore.
127
136
"""
128
- with open (filepath ) as file :
129
- numpydoc_ignore_comments = {}
137
+ numpydoc_ignore_comments = {}
138
+ try :
139
+ file = open (filepath )
140
+ except (OSError , TypeError ): # can be None, nonexistent, or unreadable
141
+ return numpydoc_ignore_comments
142
+ with file :
130
143
last_declaration = 1
131
144
declarations = ["def" , "class" ]
132
145
for token in tokenize .generate_tokens (file .readline ):
0 commit comments