typing.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. import sys
  8. from pathlib import Path
  9. from typing import (
  10. TYPE_CHECKING,
  11. Any,
  12. Callable,
  13. Dict,
  14. Iterable,
  15. NamedTuple,
  16. Optional,
  17. Pattern,
  18. Tuple,
  19. Type,
  20. Union,
  21. )
  22. if sys.version_info >= (3, 8):
  23. from typing import Literal, Protocol, TypedDict
  24. else:
  25. from typing_extensions import Literal, Protocol, TypedDict
  26. if TYPE_CHECKING:
  27. from pylint.config.callback_actions import _CallbackAction
  28. from pylint.pyreverse.inspector import Project
  29. from pylint.reporters.ureports.nodes import Section
  30. from pylint.utils import LinterStats
  31. class FileItem(NamedTuple):
  32. """Represents data about a file handled by pylint.
  33. Each file item has:
  34. - name: full name of the module
  35. - filepath: path of the file
  36. - modname: module name
  37. """
  38. name: str
  39. filepath: str
  40. modpath: str
  41. class ModuleDescriptionDict(TypedDict):
  42. """Represents data about a checked module."""
  43. path: str
  44. name: str
  45. isarg: bool
  46. basepath: str
  47. basename: str
  48. class ErrorDescriptionDict(TypedDict):
  49. """Represents data about errors collected during checking of a module."""
  50. key: Literal["fatal"]
  51. mod: str
  52. ex: ImportError | SyntaxError
  53. class MessageLocationTuple(NamedTuple):
  54. """Tuple with information about the location of a to-be-displayed message."""
  55. abspath: str
  56. path: str
  57. module: str
  58. obj: str
  59. line: int
  60. column: int
  61. end_line: int | None = None
  62. end_column: int | None = None
  63. class ManagedMessage(NamedTuple):
  64. """Tuple with information about a managed message of the linter."""
  65. name: str | None
  66. msgid: str
  67. symbol: str
  68. line: int | None
  69. is_disabled: bool
  70. MessageTypesFullName = Literal[
  71. "convention", "error", "fatal", "info", "refactor", "statement", "warning"
  72. ]
  73. """All possible message categories."""
  74. OptionDict = Dict[
  75. str,
  76. Union[
  77. None,
  78. str,
  79. bool,
  80. int,
  81. Pattern[str],
  82. Iterable[Union[str, int, Pattern[str]]],
  83. Type["_CallbackAction"],
  84. Callable[[Any], Any],
  85. Callable[[Any, Any, Any, Any], Any],
  86. ],
  87. ]
  88. Options = Tuple[Tuple[str, OptionDict], ...]
  89. ReportsCallable = Callable[["Section", "LinterStats", Optional["LinterStats"]], None]
  90. """Callable to create a report."""
  91. class ExtraMessageOptions(TypedDict, total=False):
  92. """All allowed keys in the extra options for message definitions."""
  93. scope: str
  94. old_names: list[tuple[str, str]]
  95. maxversion: tuple[int, int]
  96. minversion: tuple[int, int]
  97. shared: bool
  98. default_enabled: bool
  99. MessageDefinitionTuple = Union[
  100. Tuple[str, str, str],
  101. Tuple[str, str, str, ExtraMessageOptions],
  102. ]
  103. DirectoryNamespaceDict = Dict[Path, Tuple[argparse.Namespace, "DirectoryNamespaceDict"]]
  104. class GetProjectCallable(Protocol):
  105. def __call__(self, module: str, name: str | None = "No Name") -> Project:
  106. ... # pragma: no cover