mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
refactor: Use PEP-526 based variable annotation (sphinx.pycode)
This commit is contained in:
@@ -26,7 +26,7 @@ from sphinx.pycode.parser import Parser
|
|||||||
|
|
||||||
class ModuleAnalyzer:
|
class ModuleAnalyzer:
|
||||||
# cache for analyzer objects -- caches both by module and file name
|
# cache for analyzer objects -- caches both by module and file name
|
||||||
cache = {} # type: Dict[Tuple[str, str], Any]
|
cache: Dict[Tuple[str, str], Any] = {}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_module_source(modname: str) -> Tuple[Optional[str], Optional[str]]:
|
def get_module_source(modname: str) -> Tuple[Optional[str], Optional[str]]:
|
||||||
@@ -135,12 +135,12 @@ class ModuleAnalyzer:
|
|||||||
self.code = source.read()
|
self.code = source.read()
|
||||||
|
|
||||||
# will be filled by analyze()
|
# will be filled by analyze()
|
||||||
self.annotations = None # type: Dict[Tuple[str, str], str]
|
self.annotations: Dict[Tuple[str, str], str] = None
|
||||||
self.attr_docs = None # type: Dict[Tuple[str, str], List[str]]
|
self.attr_docs: Dict[Tuple[str, str], List[str]] = None
|
||||||
self.finals = None # type: List[str]
|
self.finals: List[str] = None
|
||||||
self.overloads = None # type: Dict[str, List[Signature]]
|
self.overloads: Dict[str, List[Signature]] = None
|
||||||
self.tagorder = None # type: Dict[str, int]
|
self.tagorder: Dict[str, int] = None
|
||||||
self.tags = None # type: Dict[str, Tuple[str, int, int]]
|
self.tags: Dict[str, Tuple[str, int, int]] = None
|
||||||
self._analyzed = False
|
self._analyzed = False
|
||||||
|
|
||||||
def parse(self) -> None:
|
def parse(self) -> None:
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ else:
|
|||||||
import ast # type: ignore
|
import ast # type: ignore
|
||||||
|
|
||||||
|
|
||||||
OPERATORS = {
|
OPERATORS: Dict[Type[ast.AST], str] = {
|
||||||
ast.Add: "+",
|
ast.Add: "+",
|
||||||
ast.And: "and",
|
ast.And: "and",
|
||||||
ast.BitAnd: "&",
|
ast.BitAnd: "&",
|
||||||
@@ -41,7 +41,7 @@ OPERATORS = {
|
|||||||
ast.Sub: "-",
|
ast.Sub: "-",
|
||||||
ast.UAdd: "+",
|
ast.UAdd: "+",
|
||||||
ast.USub: "-",
|
ast.USub: "-",
|
||||||
} # type: Dict[Type[ast.AST], str]
|
}
|
||||||
|
|
||||||
|
|
||||||
def parse(code: str, mode: str = 'exec') -> "ast.AST":
|
def parse(code: str, mode: str = 'exec') -> "ast.AST":
|
||||||
@@ -108,7 +108,7 @@ class _UnparseVisitor(ast.NodeVisitor):
|
|||||||
return name
|
return name
|
||||||
|
|
||||||
def visit_arguments(self, node: ast.arguments) -> str:
|
def visit_arguments(self, node: ast.arguments) -> str:
|
||||||
defaults = list(node.defaults) # type: List[Optional[ast.expr]]
|
defaults: List[Optional[ast.expr]] = list(node.defaults)
|
||||||
positionals = len(node.args)
|
positionals = len(node.args)
|
||||||
posonlyargs = 0
|
posonlyargs = 0
|
||||||
if hasattr(node, "posonlyargs"): # for py38+
|
if hasattr(node, "posonlyargs"): # for py38+
|
||||||
@@ -117,11 +117,11 @@ class _UnparseVisitor(ast.NodeVisitor):
|
|||||||
for _ in range(len(defaults), positionals):
|
for _ in range(len(defaults), positionals):
|
||||||
defaults.insert(0, None)
|
defaults.insert(0, None)
|
||||||
|
|
||||||
kw_defaults = list(node.kw_defaults) # type: List[Optional[ast.expr]]
|
kw_defaults: List[Optional[ast.expr]] = list(node.kw_defaults)
|
||||||
for _ in range(len(kw_defaults), len(node.kwonlyargs)):
|
for _ in range(len(kw_defaults), len(node.kwonlyargs)):
|
||||||
kw_defaults.insert(0, None)
|
kw_defaults.insert(0, None)
|
||||||
|
|
||||||
args = [] # type: List[str]
|
args: List[str] = []
|
||||||
if hasattr(node, "posonlyargs"): # for py38+
|
if hasattr(node, "posonlyargs"): # for py38+
|
||||||
for i, arg in enumerate(node.posonlyargs): # type: ignore
|
for i, arg in enumerate(node.posonlyargs): # type: ignore
|
||||||
args.append(self._visit_arg_with_default(arg, defaults[i]))
|
args.append(self._visit_arg_with_default(arg, defaults[i]))
|
||||||
|
|||||||
@@ -129,8 +129,8 @@ class TokenProcessor:
|
|||||||
lines = iter(buffers)
|
lines = iter(buffers)
|
||||||
self.buffers = buffers
|
self.buffers = buffers
|
||||||
self.tokens = tokenize.generate_tokens(lambda: next(lines))
|
self.tokens = tokenize.generate_tokens(lambda: next(lines))
|
||||||
self.current = None # type: Token
|
self.current: Token = None
|
||||||
self.previous = None # type: Token
|
self.previous: Token = None
|
||||||
|
|
||||||
def get_line(self, lineno: int) -> str:
|
def get_line(self, lineno: int) -> str:
|
||||||
"""Returns specified line."""
|
"""Returns specified line."""
|
||||||
@@ -178,7 +178,7 @@ class AfterCommentParser(TokenProcessor):
|
|||||||
|
|
||||||
def __init__(self, lines: List[str]) -> None:
|
def __init__(self, lines: List[str]) -> None:
|
||||||
super().__init__(lines)
|
super().__init__(lines)
|
||||||
self.comment = None # type: str
|
self.comment: str = None
|
||||||
|
|
||||||
def fetch_rvalue(self) -> List[Token]:
|
def fetch_rvalue(self) -> List[Token]:
|
||||||
"""Fetch right-hand value of assignment."""
|
"""Fetch right-hand value of assignment."""
|
||||||
@@ -221,18 +221,18 @@ class VariableCommentPicker(ast.NodeVisitor):
|
|||||||
self.counter = itertools.count()
|
self.counter = itertools.count()
|
||||||
self.buffers = buffers
|
self.buffers = buffers
|
||||||
self.encoding = encoding
|
self.encoding = encoding
|
||||||
self.context = [] # type: List[str]
|
self.context: List[str] = []
|
||||||
self.current_classes = [] # type: List[str]
|
self.current_classes: List[str] = []
|
||||||
self.current_function = None # type: ast.FunctionDef
|
self.current_function: ast.FunctionDef = None
|
||||||
self.comments = OrderedDict() # type: Dict[Tuple[str, str], str]
|
self.comments: Dict[Tuple[str, str], str] = OrderedDict()
|
||||||
self.annotations = {} # type: Dict[Tuple[str, str], str]
|
self.annotations: Dict[Tuple[str, str], str] = {}
|
||||||
self.previous = None # type: ast.AST
|
self.previous: ast.AST = None
|
||||||
self.deforders = {} # type: Dict[str, int]
|
self.deforders: Dict[str, int] = {}
|
||||||
self.finals = [] # type: List[str]
|
self.finals: List[str] = []
|
||||||
self.overloads = {} # type: Dict[str, List[Signature]]
|
self.overloads: Dict[str, List[Signature]] = {}
|
||||||
self.typing = None # type: str
|
self.typing: str = None
|
||||||
self.typing_final = None # type: str
|
self.typing_final: str = None
|
||||||
self.typing_overload = None # type: str
|
self.typing_overload: str = None
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
def get_qualname_for(self, name: str) -> Optional[List[str]]:
|
def get_qualname_for(self, name: str) -> Optional[List[str]]:
|
||||||
@@ -350,7 +350,7 @@ class VariableCommentPicker(ast.NodeVisitor):
|
|||||||
"""Handles Assign node and pick up a variable comment."""
|
"""Handles Assign node and pick up a variable comment."""
|
||||||
try:
|
try:
|
||||||
targets = get_assign_targets(node)
|
targets = get_assign_targets(node)
|
||||||
varnames = sum([get_lvar_names(t, self=self.get_self()) for t in targets], []) # type: List[str] # NOQA
|
varnames: List[str] = sum([get_lvar_names(t, self=self.get_self()) for t in targets], []) # NOQA
|
||||||
current_line = self.get_line(node.lineno)
|
current_line = self.get_line(node.lineno)
|
||||||
except TypeError:
|
except TypeError:
|
||||||
return # this assignment is not new definition!
|
return # this assignment is not new definition!
|
||||||
@@ -466,10 +466,10 @@ class DefinitionFinder(TokenProcessor):
|
|||||||
|
|
||||||
def __init__(self, lines: List[str]) -> None:
|
def __init__(self, lines: List[str]) -> None:
|
||||||
super().__init__(lines)
|
super().__init__(lines)
|
||||||
self.decorator = None # type: Token
|
self.decorator: Token = None
|
||||||
self.context = [] # type: List[str]
|
self.context: List[str] = []
|
||||||
self.indents = [] # type: List
|
self.indents: List = []
|
||||||
self.definitions = {} # type: Dict[str, Tuple[str, int, int]]
|
self.definitions: Dict[str, Tuple[str, int, int]] = {}
|
||||||
|
|
||||||
def add_definition(self, name: str, entry: Tuple[str, int, int]) -> None:
|
def add_definition(self, name: str, entry: Tuple[str, int, int]) -> None:
|
||||||
"""Add a location of definition."""
|
"""Add a location of definition."""
|
||||||
@@ -543,12 +543,12 @@ class Parser:
|
|||||||
def __init__(self, code: str, encoding: str = 'utf-8') -> None:
|
def __init__(self, code: str, encoding: str = 'utf-8') -> None:
|
||||||
self.code = filter_whitespace(code)
|
self.code = filter_whitespace(code)
|
||||||
self.encoding = encoding
|
self.encoding = encoding
|
||||||
self.annotations = {} # type: Dict[Tuple[str, str], str]
|
self.annotations: Dict[Tuple[str, str], str] = {}
|
||||||
self.comments = {} # type: Dict[Tuple[str, str], str]
|
self.comments: Dict[Tuple[str, str], str] = {}
|
||||||
self.deforders = {} # type: Dict[str, int]
|
self.deforders: Dict[str, int] = {}
|
||||||
self.definitions = {} # type: Dict[str, Tuple[str, int, int]]
|
self.definitions: Dict[str, Tuple[str, int, int]] = {}
|
||||||
self.finals = [] # type: List[str]
|
self.finals: List[str] = []
|
||||||
self.overloads = {} # type: Dict[str, List[Signature]]
|
self.overloads: Dict[str, List[Signature]] = {}
|
||||||
|
|
||||||
def parse(self) -> None:
|
def parse(self) -> None:
|
||||||
"""Parse the source code."""
|
"""Parse the source code."""
|
||||||
|
|||||||
Reference in New Issue
Block a user