eric6/Plugins/CheckerPlugins/CodeStyleChecker/PathLib/PathlibChecker.py

changeset 8243
cc717c2ae956
parent 8218
7c09585bd960
equal deleted inserted replaced
8242:aa713ac50c0d 8243:cc717c2ae956
8 the pathlib module. 8 the pathlib module.
9 """ 9 """
10 10
11 import ast 11 import ast
12 import copy 12 import copy
13 import contextlib
13 14
14 15
15 class PathlibChecker: 16 class PathlibChecker:
16 """ 17 """
17 Class implementing a checker for functions that can be replaced by use of 18 Class implementing a checker for functions that can be replaced by use of
175 @param node reference to the AST node to check 176 @param node reference to the AST node to check
176 @type ast.AST 177 @type ast.AST
177 @param name resolved name of the node 178 @param name resolved name of the node
178 @type str 179 @type str
179 """ 180 """
180 try: 181 with contextlib.suppress(KeyError):
181 errorCode = self.Function2Code[name] 182 errorCode = self.Function2Code[name]
182 self.__error(node.lineno - 1, node.col_offset, errorCode) 183 self.__error(node.lineno - 1, node.col_offset, errorCode)
183 except KeyError:
184 # name is not in our list of replacements
185 pass
186 184
187 185
188 class PathlibVisitor(ast.NodeVisitor): 186 class PathlibVisitor(ast.NodeVisitor):
189 """ 187 """
190 Class to traverse the AST node tree and check for potential issues. 188 Class to traverse the AST node tree and check for potential issues.
258 Public method to resolve the name. 256 Public method to resolve the name.
259 257
260 @return resolved name 258 @return resolved name
261 @rtype str 259 @rtype str
262 """ 260 """
263 try: 261 with contextlib.suppress(KeyError, IndexError):
264 attr = self.__importAlias[self.__names[-1]] 262 attr = self.__importAlias[self.__names[-1]]
265 self.__names[-1] = attr 263 self.__names[-1] = attr
266 except (KeyError, IndexError):
267 # do nothing if there is no such name or the names list is empty 264 # do nothing if there is no such name or the names list is empty
268 pass
269 265
270 return ".".join(reversed(self.__names)) 266 return ".".join(reversed(self.__names))
271 267
272 def visit_Name(self, node): 268 def visit_Name(self, node):
273 """ 269 """

eric ide

mercurial