diff options
author | Daniël van Noord <13665637+DanielNoord@users.noreply.github.com> | 2022-04-25 09:12:15 +0200 |
---|---|---|
committer | Daniël van Noord <13665637+DanielNoord@users.noreply.github.com> | 2022-04-25 09:50:00 +0200 |
commit | 17981e2b24d421e61ca9d64f46e52b2860f0a203 (patch) | |
tree | 086b88f41130ad3bcf96fd7c9c9bc99a01f46287 | |
parent | 8f1493ece6d17f11f8f47a27fd38c1405eb0ec5b (diff) | |
download | pylint-git-17981e2b24d421e61ca9d64f46e52b2860f0a203.tar.gz |
Finish typing of ``pylint/graph.py``
-rw-r--r-- | pylint/graph.py | 50 |
1 files changed, 30 insertions, 20 deletions
diff --git a/pylint/graph.py b/pylint/graph.py index a8effb65b..db4827977 100644 --- a/pylint/graph.py +++ b/pylint/graph.py @@ -15,9 +15,11 @@ import shutil import subprocess import sys import tempfile +from collections.abc import Sequence +from typing import Any -def target_info_from_filename(filename): +def target_info_from_filename(filename: str) -> tuple[str, str, str]: """Transforms /some/path/foo.png into ('/some/path', 'foo.png', 'png').""" basename = os.path.basename(filename) storedir = os.path.dirname(os.path.abspath(filename)) @@ -30,20 +32,20 @@ class DotBackend: def __init__( self, - graphname, - rankdir=None, - size=None, - ratio=None, - charset="utf-8", - renderer="dot", - additional_param=None, - ): + graphname: str, + rankdir: str | None = None, + size: Any = None, + ratio: Any = None, + charset: str = "utf-8", + renderer: str = "dot", + additional_param: dict[str, Any] | None = None, + ) -> None: if additional_param is None: additional_param = {} self.graphname = graphname self.renderer = renderer - self.lines = [] - self._source = None + self.lines: list[str] = [] + self._source: str | None = None self.emit(f"digraph {normalize_node_id(graphname)} {{") if rankdir: self.emit(f"rankdir={rankdir}") @@ -61,7 +63,7 @@ class DotBackend: for param in additional_param.items(): self.emit("=".join(param)) - def get_source(self): + def get_source(self) -> str: """Returns self._source.""" if self._source is None: self.emit("}\n") @@ -134,11 +136,11 @@ class DotBackend: os.unlink(dot_sourcepath) return outputfile - def emit(self, line): + def emit(self, line: str) -> None: """Adds <line> to final output.""" self.lines.append(line) - def emit_edge(self, name1, name2, **props): + def emit_edge(self, name1: str, name2: str, **props: Any) -> None: """Emit an edge from <name1> to <name2>. For edge properties: see https://www.graphviz.org/doc/info/attrs.html @@ -147,7 +149,7 @@ class DotBackend: n_from, n_to = normalize_node_id(name1), normalize_node_id(name2) self.emit(f"{n_from} -> {n_to} [{', '.join(sorted(attrs))}];") - def emit_node(self, name, **props): + def emit_node(self, name: str, **props: Any) -> None: """Emit a node with given properties. For node properties: see https://www.graphviz.org/doc/info/attrs.html @@ -156,26 +158,34 @@ class DotBackend: self.emit(f"{normalize_node_id(name)} [{', '.join(sorted(attrs))}];") -def normalize_node_id(nid): +def normalize_node_id(nid: str) -> str: """Returns a suitable DOT node id for `nid`.""" return f'"{nid}"' -def get_cycles(graph_dict, vertices=None): +def get_cycles( + graph_dict: dict[str, set[str]], vertices: list[str] | None = None +) -> Sequence[list[str]]: """Return a list of detected cycles in a dictionary representing an ordered graph (i.e. key are vertices and values is a list of destination vertices representing edges) """ if not graph_dict: return () - result = [] + result: list[list[str]] = [] if vertices is None: - vertices = graph_dict.keys() + vertices = list(graph_dict.keys()) for vertice in vertices: _get_cycles(graph_dict, [], set(), result, vertice) return result -def _get_cycles(graph_dict, path, visited, result, vertice): +def _get_cycles( + graph_dict: dict[str, set[str]], + path: list[str], + visited: set[str], + result: list[list[str]], + vertice: str, +) -> None: """Recursive function doing the real work for get_cycles.""" if vertice in path: cycle = [vertice] |