Add type check to CI and fix errors (#266)
This commit is contained in:
parent
c56081f28a
commit
94682d2b23
|
@ -64,6 +64,7 @@ jobs:
|
|||
fail-fast: false
|
||||
matrix:
|
||||
tox_env:
|
||||
- type
|
||||
- dev
|
||||
- readme
|
||||
steps:
|
||||
|
|
|
@ -2,12 +2,6 @@ from __future__ import annotations
|
|||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
try: # noqa: SIM105
|
||||
from pip._internal.operations.freeze import FrozenRequirement # noqa: F401
|
||||
except ImportError:
|
||||
pass # type: ignore[attr-defined, no-redef]
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from pip._vendor.pkg_resources import DistInfoDistribution
|
||||
|
||||
|
|
|
@ -10,9 +10,10 @@ from .text import render_text
|
|||
|
||||
if TYPE_CHECKING:
|
||||
from pipdeptree._cli import Options
|
||||
from pipdeptree._models import PackageDAG
|
||||
|
||||
|
||||
def render(options: Options, tree: PackageDAG) -> None: # noqa: F821
|
||||
def render(options: Options, tree: PackageDAG) -> None:
|
||||
if options.json:
|
||||
print(render_json(tree)) # noqa: T201
|
||||
elif options.json_tree:
|
||||
|
@ -20,6 +21,7 @@ def render(options: Options, tree: PackageDAG) -> None: # noqa: F821
|
|||
elif options.mermaid:
|
||||
print(render_mermaid(tree)) # noqa: T201
|
||||
elif options.output_format:
|
||||
assert options.graph_output is not None # noqa: S101
|
||||
render_graphviz(tree, output_format=options.graph_output, reverse=options.reverse)
|
||||
else:
|
||||
render_text(tree, options.depth, options.encoding_type, options.all, options.freeze)
|
||||
|
|
|
@ -1,23 +1,15 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from textwrap import dedent
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import TYPE_CHECKING, Final
|
||||
|
||||
from pipdeptree._models import ReversedPackageDAG
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from pipdeptree._models import PackageDAG
|
||||
|
||||
|
||||
def render_mermaid(tree: PackageDAG) -> str: # noqa: C901
|
||||
"""
|
||||
Produce a Mermaid flowchart from the dependency graph.
|
||||
|
||||
:param tree: dependency graph
|
||||
"""
|
||||
# List of reserved keywords in Mermaid that cannot be used as node names.
|
||||
# See: https://github.com/mermaid-js/mermaid/issues/4182#issuecomment-1454787806
|
||||
reserved_ids: set[str] = {
|
||||
_RESERVED_IDS: Final[frozenset[str]] = frozenset(
|
||||
[
|
||||
"C4Component",
|
||||
"C4Container",
|
||||
"C4Deployment",
|
||||
|
@ -38,7 +30,19 @@ def render_mermaid(tree: PackageDAG) -> str: # noqa: C901
|
|||
"linkStyle",
|
||||
"style",
|
||||
"subgraph",
|
||||
}
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
def render_mermaid(tree: PackageDAG) -> str: # noqa: C901
|
||||
"""
|
||||
Produce a Mermaid flowchart from the dependency graph.
|
||||
|
||||
:param tree: dependency graph
|
||||
"""
|
||||
# List of reserved keywords in Mermaid that cannot be used as node names.
|
||||
# See: https://github.com/mermaid-js/mermaid/issues/4182#issuecomment-1454787806
|
||||
|
||||
node_ids_map: dict[str, str] = {}
|
||||
|
||||
def mermaid_id(key: str) -> str:
|
||||
|
@ -48,7 +52,7 @@ def render_mermaid(tree: PackageDAG) -> str: # noqa: C901
|
|||
if canonical_id is not None:
|
||||
return canonical_id
|
||||
# If the key is not a reserved keyword, return it as is, and update the map.
|
||||
if key not in reserved_ids:
|
||||
if key not in _RESERVED_IDS:
|
||||
node_ids_map[key] = key
|
||||
return key
|
||||
# If the key is a reserved keyword, append a number to it.
|
||||
|
|
|
@ -33,7 +33,7 @@ def mock_pkgs() -> Callable[[MockGraph], Iterator[Mock]]:
|
|||
|
||||
@pytest.fixture()
|
||||
def example_dag(mock_pkgs: Callable[[MockGraph], Iterator[Mock]]) -> PackageDAG:
|
||||
packages = {
|
||||
packages: MockGraph = {
|
||||
("a", "3.4.0"): [("b", [(">=", "2.0.0")]), ("c", [(">=", "5.7.1")])],
|
||||
("b", "2.3.1"): [("d", [(">=", "2.30"), ("<", "2.42")])],
|
||||
("c", "5.10.0"): [("d", [(">=", "2.30")]), ("e", [(">=", "0.12.1")])],
|
||||
|
|
|
@ -4,7 +4,7 @@ from textwrap import dedent, indent
|
|||
from typing import TYPE_CHECKING, Callable, Iterator
|
||||
|
||||
from pipdeptree._models import PackageDAG
|
||||
from pipdeptree._render import render_mermaid
|
||||
from pipdeptree._render.mermaid import render_mermaid
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from unittest.mock import Mock
|
||||
|
|
|
@ -5,7 +5,7 @@ from typing import TYPE_CHECKING
|
|||
|
||||
import pytest
|
||||
|
||||
from pipdeptree._render import render_text
|
||||
from pipdeptree._render.text import render_text
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from pipdeptree._models import PackageDAG
|
||||
|
|
|
@ -65,7 +65,7 @@ def test_package_dag_filter(example_dag: PackageDAG) -> None:
|
|||
|
||||
@pytest.fixture(scope="session")
|
||||
def t_fnmatch(mock_pkgs: Callable[[MockGraph], Iterator[Mock]]) -> Any:
|
||||
graph = {
|
||||
graph: MockGraph = {
|
||||
("a.a", "1"): [("a.b", []), ("a.c", [])],
|
||||
("a.b", "1"): [("a.c", [])],
|
||||
("b.a", "1"): [("b.b", [])],
|
||||
|
|
Loading…
Reference in New Issue