eric6/Plugins/CheckerPlugins/CodeStyleChecker/DocStyle/DocStyleChecker.py

changeset 8243
cc717c2ae956
parent 8222
5994b80b8760
child 8259
2bbec88047dd
equal deleted inserted replaced
8242:aa713ac50c0d 8243:cc717c2ae956
13 # 13 #
14 14
15 import tokenize 15 import tokenize
16 import ast 16 import ast
17 from io import StringIO 17 from io import StringIO
18 import contextlib
18 19
19 try: 20 try:
20 ast.AsyncFunctionDef # __IGNORE_EXCEPTION__ 21 ast.AsyncFunctionDef # __IGNORE_EXCEPTION__
21 except AttributeError: 22 except AttributeError:
22 ast.AsyncFunctionDef = ast.FunctionDef 23 ast.AsyncFunctionDef = ast.FunctionDef
462 if moduleDocstring: 463 if moduleDocstring:
463 return moduleDocstring 464 return moduleDocstring
464 465
465 tokenGenerator = tokenize.generate_tokens( 466 tokenGenerator = tokenize.generate_tokens(
466 StringIO(context.ssource()).readline) 467 StringIO(context.ssource()).readline)
467 try: 468 with contextlib.suppress(StopIteration):
468 kind = None 469 kind = None
469 while kind != tokenize.INDENT: 470 while kind != tokenize.INDENT:
470 kind, _, _, _, _ = next(tokenGenerator) 471 kind, _, _, _, _ = next(tokenGenerator)
471 kind, value, (line, char), _, _ = next(tokenGenerator) 472 kind, value, (line, char), _, _ = next(tokenGenerator)
472 if kind == tokenize.STRING: # STRING after INDENT is a docstring 473 if kind == tokenize.STRING: # STRING after INDENT is a docstring
473 return DocStyleContext( 474 return DocStyleContext(
474 value, context.start() + line - 1, "docstring") 475 value, context.start() + line - 1, "docstring")
475 except StopIteration:
476 pass
477 476
478 return None 477 return None
479 478
480 def __parseTopLevel(self, keyword): 479 def __parseTopLevel(self, keyword):
481 """ 480 """
557 contexts = [] 556 contexts = []
558 for classContext in self.__parseClasses(): 557 for classContext in self.__parseClasses():
559 tokenGenerator = tokenize.generate_tokens( 558 tokenGenerator = tokenize.generate_tokens(
560 StringIO(classContext.ssource()).readline) 559 StringIO(classContext.ssource()).readline)
561 kind, value, char = None, None, None 560 kind, value, char = None, None, None
562 try: 561 with contextlib.suppress(StopIteration):
563 while True: 562 while True:
564 start, end = None, None 563 start, end = None, None
565 while not (kind == tokenize.NAME and value == 'def'): 564 while not (kind == tokenize.NAME and value == 'def'):
566 kind, value, (line, char), _, _ = ( 565 kind, value, (line, char), _, _ = (
567 next(tokenGenerator) 566 next(tokenGenerator)
586 self.__source[startLine - 1].strip() == 585 self.__source[startLine - 1].strip() ==
587 "@classmethod" 586 "@classmethod"
588 ): 587 ):
589 context.setSpecial("classmethod") 588 context.setSpecial("classmethod")
590 contexts.append(context) 589 contexts.append(context)
591 except StopIteration:
592 pass
593 self.__methodsCache = contexts 590 self.__methodsCache = contexts
594 591
595 return self.__methodsCache 592 return self.__methodsCache
596 593
597 def __parseContexts(self, kind): 594 def __parseContexts(self, kind):

eric ide

mercurial