summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniël van Noord <13665637+DanielNoord@users.noreply.github.com>2022-04-14 10:58:45 +0200
committerDaniël van Noord <13665637+DanielNoord@users.noreply.github.com>2022-04-14 11:21:58 +0200
commit5bff0d8a54a803bd24d9bce49e271af46fee5935 (patch)
tree671bce4705bab05bb545e75e763ef50da102cc55
parent47e168cf607e2069b103fc466edfe1c6522e2fb2 (diff)
downloadpylint-git-5bff0d8a54a803bd24d9bce49e271af46fee5935.tar.gz
Use ``python-typing-update`` on ``pylint/pyreverse`` directory
-rw-r--r--pylint/message/message.py25
-rw-r--r--pylint/pyreverse/diadefslib.py6
-rw-r--r--pylint/pyreverse/dot_printer.py24
-rw-r--r--pylint/pyreverse/mermaidjs_printer.py11
-rw-r--r--pylint/pyreverse/plantuml_printer.py11
-rw-r--r--pylint/pyreverse/printer.py29
-rw-r--r--pylint/pyreverse/printer_factory.py6
-rw-r--r--pylint/pyreverse/utils.py12
-rw-r--r--pylint/pyreverse/vcg_printer.py15
9 files changed, 76 insertions, 63 deletions
diff --git a/pylint/message/message.py b/pylint/message/message.py
index 297442e45..afe10f7f3 100644
--- a/pylint/message/message.py
+++ b/pylint/message/message.py
@@ -2,8 +2,10 @@
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
+from __future__ import annotations
+
import collections
-from typing import Optional, Tuple, Union, overload
+from typing import overload
from warnings import warn
from pylint.constants import MSG_TYPES
@@ -41,8 +43,8 @@ class Message(_MsgBase):
symbol: str,
location: MessageLocationTuple,
msg: str,
- confidence: Optional[Confidence],
- ) -> "Message":
+ confidence: Confidence | None,
+ ) -> Message:
...
@overload
@@ -50,10 +52,10 @@ class Message(_MsgBase):
cls,
msg_id: str,
symbol: str,
- location: Tuple[str, str, str, str, int, int],
+ location: tuple[str, str, str, str, int, int],
msg: str,
- confidence: Optional[Confidence],
- ) -> "Message":
+ confidence: Confidence | None,
+ ) -> Message:
# Remove for pylint 3.0
...
@@ -61,13 +63,10 @@ class Message(_MsgBase):
cls,
msg_id: str,
symbol: str,
- location: Union[
- Tuple[str, str, str, str, int, int],
- MessageLocationTuple,
- ],
+ location: (tuple[str, str, str, str, int, int] | MessageLocationTuple),
msg: str,
- confidence: Optional[Confidence],
- ) -> "Message":
+ confidence: Confidence | None,
+ ) -> Message:
if not isinstance(location, MessageLocationTuple):
warn(
"In pylint 3.0, Messages will only accept a MessageLocationTuple as location parameter",
@@ -82,7 +81,7 @@ class Message(_MsgBase):
msg_id[0],
MSG_TYPES[msg_id[0]],
confidence,
- *location
+ *location,
)
def format(self, template: str) -> str:
diff --git a/pylint/pyreverse/diadefslib.py b/pylint/pyreverse/diadefslib.py
index b5dd3fb50..35df9cb02 100644
--- a/pylint/pyreverse/diadefslib.py
+++ b/pylint/pyreverse/diadefslib.py
@@ -4,7 +4,9 @@
"""Handle diagram generation options for class diagram or default diagrams."""
-from typing import Any, Optional
+from __future__ import annotations
+
+from typing import Any
import astroid
from astroid import nodes
@@ -126,7 +128,7 @@ class DefaultDiadefGenerator(LocalsVisitor, DiaDefGenerator):
"""
mode = self.config.mode
if len(node.modules) > 1:
- self.pkgdiagram: Optional[PackageDiagram] = PackageDiagram(
+ self.pkgdiagram: PackageDiagram | None = PackageDiagram(
f"packages {node.name}", mode
)
else:
diff --git a/pylint/pyreverse/dot_printer.py b/pylint/pyreverse/dot_printer.py
index 0cca0ab61..df4e94a00 100644
--- a/pylint/pyreverse/dot_printer.py
+++ b/pylint/pyreverse/dot_printer.py
@@ -3,25 +3,27 @@
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
"""Class to generate files in dot format and image formats supported by Graphviz."""
+
+from __future__ import annotations
+
import os
import subprocess
import sys
import tempfile
from pathlib import Path
-from typing import Dict, FrozenSet, List, Optional
from astroid import nodes
from pylint.pyreverse.printer import EdgeType, Layout, NodeProperties, NodeType, Printer
from pylint.pyreverse.utils import get_annotation_label
-ALLOWED_CHARSETS: FrozenSet[str] = frozenset(("utf-8", "iso-8859-1", "latin1"))
-SHAPES: Dict[NodeType, str] = {
+ALLOWED_CHARSETS: frozenset[str] = frozenset(("utf-8", "iso-8859-1", "latin1"))
+SHAPES: dict[NodeType, str] = {
NodeType.PACKAGE: "box",
NodeType.INTERFACE: "record",
NodeType.CLASS: "record",
}
-ARROWS: Dict[EdgeType, Dict[str, str]] = {
+ARROWS: dict[EdgeType, dict[str, str]] = {
EdgeType.INHERITS: dict(arrowtail="none", arrowhead="empty"),
EdgeType.IMPLEMENTS: dict(arrowtail="node", arrowhead="empty", style="dashed"),
EdgeType.ASSOCIATION: dict(
@@ -37,8 +39,8 @@ class DotPrinter(Printer):
def __init__(
self,
title: str,
- layout: Optional[Layout] = None,
- use_automatic_namespace: Optional[bool] = None,
+ layout: Layout | None = None,
+ use_automatic_namespace: bool | None = None,
):
layout = layout or Layout.BOTTOM_TO_TOP
self.charset = "utf-8"
@@ -59,7 +61,7 @@ class DotPrinter(Printer):
self,
name: str,
type_: NodeType,
- properties: Optional[NodeProperties] = None,
+ properties: NodeProperties | None = None,
) -> None:
"""Create a new node.
@@ -80,7 +82,7 @@ class DotPrinter(Printer):
)
def _build_label_for_node(
- self, properties: NodeProperties, is_interface: Optional[bool] = False
+ self, properties: NodeProperties, is_interface: bool | None = False
) -> str:
if not properties.label:
return ""
@@ -95,11 +97,11 @@ class DotPrinter(Printer):
return label
# Add class attributes
- attrs: List[str] = properties.attrs or []
+ attrs: list[str] = properties.attrs or []
label = "{" + label + "|" + r"\l".join(attrs) + r"\l|"
# Add class methods
- methods: List[nodes.FunctionDef] = properties.methods or []
+ methods: list[nodes.FunctionDef] = properties.methods or []
for func in methods:
args = self._get_method_arguments(func)
label += rf"{func.name}({', '.join(args)})"
@@ -114,7 +116,7 @@ class DotPrinter(Printer):
from_node: str,
to_node: str,
type_: EdgeType,
- label: Optional[str] = None,
+ label: str | None = None,
) -> None:
"""Create an edge from one node to another to display relationships."""
arrowstyle = ARROWS[type_]
diff --git a/pylint/pyreverse/mermaidjs_printer.py b/pylint/pyreverse/mermaidjs_printer.py
index b1b56fcd2..e125e8046 100644
--- a/pylint/pyreverse/mermaidjs_printer.py
+++ b/pylint/pyreverse/mermaidjs_printer.py
@@ -3,7 +3,8 @@
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
"""Class to generate files in mermaidjs format."""
-from typing import Dict, Optional
+
+from __future__ import annotations
from pylint.pyreverse.printer import EdgeType, NodeProperties, NodeType, Printer
from pylint.pyreverse.utils import get_annotation_label
@@ -14,12 +15,12 @@ class MermaidJSPrinter(Printer):
DEFAULT_COLOR = "black"
- NODES: Dict[NodeType, str] = {
+ NODES: dict[NodeType, str] = {
NodeType.CLASS: "class",
NodeType.INTERFACE: "class",
NodeType.PACKAGE: "class",
}
- ARROWS: Dict[EdgeType, str] = {
+ ARROWS: dict[EdgeType, str] = {
EdgeType.INHERITS: "--|>",
EdgeType.IMPLEMENTS: "..|>",
EdgeType.ASSOCIATION: "--*",
@@ -35,7 +36,7 @@ class MermaidJSPrinter(Printer):
self,
name: str,
type_: NodeType,
- properties: Optional[NodeProperties] = None,
+ properties: NodeProperties | None = None,
) -> None:
"""Create a new node.
@@ -68,7 +69,7 @@ class MermaidJSPrinter(Printer):
from_node: str,
to_node: str,
type_: EdgeType,
- label: Optional[str] = None,
+ label: str | None = None,
) -> None:
"""Create an edge from one node to another to display relationships."""
from_node = from_node.split(".")[-1]
diff --git a/pylint/pyreverse/plantuml_printer.py b/pylint/pyreverse/plantuml_printer.py
index 57fe3a9d0..45106152d 100644
--- a/pylint/pyreverse/plantuml_printer.py
+++ b/pylint/pyreverse/plantuml_printer.py
@@ -3,7 +3,8 @@
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
"""Class to generate files in dot format and image formats supported by Graphviz."""
-from typing import Dict, Optional
+
+from __future__ import annotations
from pylint.pyreverse.printer import EdgeType, Layout, NodeProperties, NodeType, Printer
from pylint.pyreverse.utils import get_annotation_label
@@ -14,12 +15,12 @@ class PlantUmlPrinter(Printer):
DEFAULT_COLOR = "black"
- NODES: Dict[NodeType, str] = {
+ NODES: dict[NodeType, str] = {
NodeType.CLASS: "class",
NodeType.INTERFACE: "class",
NodeType.PACKAGE: "package",
}
- ARROWS: Dict[EdgeType, str] = {
+ ARROWS: dict[EdgeType, str] = {
EdgeType.INHERITS: "--|>",
EdgeType.IMPLEMENTS: "..|>",
EdgeType.ASSOCIATION: "--*",
@@ -45,7 +46,7 @@ class PlantUmlPrinter(Printer):
self,
name: str,
type_: NodeType,
- properties: Optional[NodeProperties] = None,
+ properties: NodeProperties | None = None,
) -> None:
"""Create a new node.
@@ -84,7 +85,7 @@ class PlantUmlPrinter(Printer):
from_node: str,
to_node: str,
type_: EdgeType,
- label: Optional[str] = None,
+ label: str | None = None,
) -> None:
"""Create an edge from one node to another to display relationships."""
edge = f"{from_node} {self.ARROWS[type_]} {to_node}"
diff --git a/pylint/pyreverse/printer.py b/pylint/pyreverse/printer.py
index 43cc534e5..f1df4595c 100644
--- a/pylint/pyreverse/printer.py
+++ b/pylint/pyreverse/printer.py
@@ -3,9 +3,12 @@
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
"""Base class defining the interface for a printer."""
+
+from __future__ import annotations
+
from abc import ABC, abstractmethod
from enum import Enum
-from typing import List, NamedTuple, Optional
+from typing import NamedTuple
from astroid import nodes
@@ -34,10 +37,10 @@ class Layout(Enum):
class NodeProperties(NamedTuple):
label: str
- attrs: Optional[List[str]] = None
- methods: Optional[List[nodes.FunctionDef]] = None
- color: Optional[str] = None
- fontcolor: Optional[str] = None
+ attrs: list[str] | None = None
+ methods: list[nodes.FunctionDef] | None = None
+ color: str | None = None
+ fontcolor: str | None = None
class Printer(ABC):
@@ -46,13 +49,13 @@ class Printer(ABC):
def __init__(
self,
title: str,
- layout: Optional[Layout] = None,
- use_automatic_namespace: Optional[bool] = None,
+ layout: Layout | None = None,
+ use_automatic_namespace: bool | None = None,
) -> None:
self.title: str = title
self.layout = layout
self.use_automatic_namespace = use_automatic_namespace
- self.lines: List[str] = []
+ self.lines: list[str] = []
self._indent = ""
self._open_graph()
@@ -68,7 +71,7 @@ class Printer(ABC):
def _open_graph(self) -> None:
"""Emit the header lines, i.e. all boilerplate code that defines things like layout etc."""
- def emit(self, line: str, force_newline: Optional[bool] = True) -> None:
+ def emit(self, line: str, force_newline: bool | None = True) -> None:
if force_newline and not line.endswith("\n"):
line += "\n"
self.lines.append(self._indent + line)
@@ -78,7 +81,7 @@ class Printer(ABC):
self,
name: str,
type_: NodeType,
- properties: Optional[NodeProperties] = None,
+ properties: NodeProperties | None = None,
) -> None:
"""Create a new node.
@@ -91,17 +94,17 @@ class Printer(ABC):
from_node: str,
to_node: str,
type_: EdgeType,
- label: Optional[str] = None,
+ label: str | None = None,
) -> None:
"""Create an edge from one node to another to display relationships."""
@staticmethod
- def _get_method_arguments(method: nodes.FunctionDef) -> List[str]:
+ def _get_method_arguments(method: nodes.FunctionDef) -> list[str]:
if method.args.args is None:
return []
first_arg = 0 if method.type in {"function", "staticmethod"} else 1
- arguments: List[nodes.AssignName] = method.args.args[first_arg:]
+ arguments: list[nodes.AssignName] = method.args.args[first_arg:]
annotations = dict(zip(arguments, method.args.annotations[first_arg:]))
for arg in arguments:
diff --git a/pylint/pyreverse/printer_factory.py b/pylint/pyreverse/printer_factory.py
index 29a406b01..41e8b46c8 100644
--- a/pylint/pyreverse/printer_factory.py
+++ b/pylint/pyreverse/printer_factory.py
@@ -2,7 +2,7 @@
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
-from typing import Dict, Type
+from __future__ import annotations
from pylint.pyreverse.dot_printer import DotPrinter
from pylint.pyreverse.mermaidjs_printer import HTMLMermaidJSPrinter, MermaidJSPrinter
@@ -10,7 +10,7 @@ from pylint.pyreverse.plantuml_printer import PlantUmlPrinter
from pylint.pyreverse.printer import Printer
from pylint.pyreverse.vcg_printer import VCGPrinter
-filetype_to_printer: Dict[str, Type[Printer]] = {
+filetype_to_printer: dict[str, type[Printer]] = {
"vcg": VCGPrinter,
"plantuml": PlantUmlPrinter,
"puml": PlantUmlPrinter,
@@ -20,5 +20,5 @@ filetype_to_printer: Dict[str, Type[Printer]] = {
}
-def get_printer_for_filetype(filetype: str) -> Type[Printer]:
+def get_printer_for_filetype(filetype: str) -> type[Printer]:
return filetype_to_printer.get(filetype, DotPrinter)
diff --git a/pylint/pyreverse/utils.py b/pylint/pyreverse/utils.py
index 8b279281e..2bd8585e0 100644
--- a/pylint/pyreverse/utils.py
+++ b/pylint/pyreverse/utils.py
@@ -3,12 +3,14 @@
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
"""Generic classes/functions for pyreverse core/extensions."""
+
+from __future__ import annotations
+
import os
import re
import shutil
import subprocess
import sys
-from typing import Optional, Union
import astroid
from astroid import nodes
@@ -208,7 +210,7 @@ class LocalsVisitor(ASTWalker):
return None
-def get_annotation_label(ann: Union[nodes.Name, nodes.NodeNG]) -> str:
+def get_annotation_label(ann: nodes.Name | nodes.NodeNG) -> str:
if isinstance(ann, nodes.Name) and ann.name is not None:
return ann.name
if isinstance(ann, nodes.NodeNG):
@@ -217,8 +219,8 @@ def get_annotation_label(ann: Union[nodes.Name, nodes.NodeNG]) -> str:
def get_annotation(
- node: Union[nodes.AssignAttr, nodes.AssignName]
-) -> Optional[Union[nodes.Name, nodes.Subscript]]:
+ node: nodes.AssignAttr | nodes.AssignName,
+) -> nodes.Name | nodes.Subscript | None:
"""Return the annotation for `node`."""
ann = None
if isinstance(node.parent, nodes.AnnAssign):
@@ -251,7 +253,7 @@ def get_annotation(
return ann
-def infer_node(node: Union[nodes.AssignAttr, nodes.AssignName]) -> set:
+def infer_node(node: nodes.AssignAttr | nodes.AssignName) -> set:
"""Return a set containing the node annotation if it exists
otherwise return a set of the inferred types using the NodeNG.infer method
"""
diff --git a/pylint/pyreverse/vcg_printer.py b/pylint/pyreverse/vcg_printer.py
index fa5e90a29..4ca5c254f 100644
--- a/pylint/pyreverse/vcg_printer.py
+++ b/pylint/pyreverse/vcg_printer.py
@@ -10,7 +10,10 @@ Note that vcg exists as a debian package.
See vcg's documentation for explanation about the different values that
maybe used for the functions parameters.
"""
-from typing import Any, Dict, Mapping, Optional
+
+from __future__ import annotations
+
+from typing import Any, Mapping
from pylint.pyreverse.printer import EdgeType, Layout, NodeProperties, NodeType, Printer
@@ -145,12 +148,12 @@ EDGE_ATTRS = {
"anchor": 1,
"horizontal_order": 1,
}
-SHAPES: Dict[NodeType, str] = {
+SHAPES: dict[NodeType, str] = {
NodeType.PACKAGE: "box",
NodeType.CLASS: "box",
NodeType.INTERFACE: "ellipse",
}
-ARROWS: Dict[EdgeType, Dict] = {
+ARROWS: dict[EdgeType, dict] = {
EdgeType.USES: dict(arrowstyle="solid", backarrowstyle="none", backarrowsize=0),
EdgeType.INHERITS: dict(
arrowstyle="solid", backarrowstyle="none", backarrowsize=10
@@ -165,7 +168,7 @@ ARROWS: Dict[EdgeType, Dict] = {
arrowstyle="solid", backarrowstyle="none", textcolor="green"
),
}
-ORIENTATION: Dict[Layout, str] = {
+ORIENTATION: dict[Layout, str] = {
Layout.LEFT_TO_RIGHT: "left_to_right",
Layout.RIGHT_TO_LEFT: "right_to_left",
Layout.TOP_TO_BOTTOM: "top_to_bottom",
@@ -200,7 +203,7 @@ class VCGPrinter(Printer):
self,
name: str,
type_: NodeType,
- properties: Optional[NodeProperties] = None,
+ properties: NodeProperties | None = None,
) -> None:
"""Create a new node.
@@ -245,7 +248,7 @@ class VCGPrinter(Printer):
from_node: str,
to_node: str,
type_: EdgeType,
- label: Optional[str] = None,
+ label: str | None = None,
) -> None:
"""Create an edge from one node to another to display relationships."""
self.emit(