typing.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
  3. # Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt
  4. """A collection of typing utilities."""
  5. from __future__ import annotations
  6. import argparse
  7. from collections.abc import Callable, Iterable, Sequence
  8. from pathlib import Path
  9. from re import Pattern
  10. from typing import (
  11. TYPE_CHECKING,
  12. Any,
  13. Literal,
  14. NamedTuple,
  15. Protocol,
  16. TypedDict,
  17. )
  18. if TYPE_CHECKING:
  19. from pylint.config.callback_actions import _CallbackAction
  20. from pylint.pyreverse.diadefslib import DiaDefGenerator
  21. from pylint.pyreverse.inspector import Project
  22. from pylint.reporters.ureports.nodes import Section
  23. from pylint.testutils.pyreverse import PyreverseConfig
  24. from pylint.utils import LinterStats
  25. class FileItem(NamedTuple):
  26. """Represents data about a file handled by pylint.
  27. Each file item has:
  28. - name: full name of the module
  29. - filepath: path of the file
  30. - modname: module name
  31. """
  32. name: str
  33. filepath: str
  34. modpath: str
  35. class ModuleDescriptionDict(TypedDict):
  36. """Represents data about a checked module."""
  37. path: str
  38. name: str
  39. isarg: bool
  40. basepath: str
  41. basename: str
  42. isignored: bool
  43. class ErrorDescriptionDict(TypedDict):
  44. """Represents data about errors collected during checking of a module."""
  45. key: Literal["fatal"]
  46. mod: str
  47. ex: ImportError | SyntaxError
  48. class MessageLocationTuple(NamedTuple):
  49. """Tuple with information about the location of a to-be-displayed message."""
  50. abspath: str
  51. path: str
  52. module: str
  53. obj: str
  54. line: int
  55. column: int
  56. end_line: int | None = None
  57. end_column: int | None = None
  58. class ManagedMessage(NamedTuple):
  59. """Tuple with information about a managed message of the linter."""
  60. name: str | None
  61. msgid: str
  62. symbol: str
  63. line: int | None
  64. is_disabled: bool
  65. MessageTypesFullName = Literal[
  66. "convention", "error", "fatal", "info", "refactor", "statement", "warning"
  67. ]
  68. """All possible message categories."""
  69. OptionDict = dict[
  70. str,
  71. None
  72. | str
  73. | bool
  74. | int
  75. | Pattern[str]
  76. | Iterable[str | int | Pattern[str]]
  77. | type["_CallbackAction"]
  78. | Callable[[Any], Any]
  79. | Callable[[Any, Any, Any, Any], Any],
  80. ]
  81. Options = tuple[tuple[str, OptionDict], ...]
  82. ReportsCallable = Callable[["Section", "LinterStats", "LinterStats | None"], None]
  83. """Callable to create a report."""
  84. class ExtraMessageOptions(TypedDict, total=False):
  85. """All allowed keys in the extra options for message definitions."""
  86. scope: str
  87. old_names: list[tuple[str, str]]
  88. maxversion: tuple[int, int]
  89. minversion: tuple[int, int]
  90. shared: bool
  91. default_enabled: bool
  92. MessageDefinitionTuple = (
  93. tuple[str, str, str] | tuple[str, str, str, ExtraMessageOptions]
  94. )
  95. DirectoryNamespaceDict = dict[Path, tuple[argparse.Namespace, "DirectoryNamespaceDict"]]
  96. class GetProjectCallable(Protocol):
  97. def __call__(
  98. self, module: str, name: str | None = "No Name"
  99. ) -> Project: ... # pragma: no cover
  100. class GeneratorFactory(Protocol):
  101. def __call__(
  102. self, config: PyreverseConfig | None = None, args: Sequence[str] | None = None
  103. ) -> DiaDefGenerator: ...