typing.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  2. # For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
  3. # Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt
  4. from __future__ import annotations
  5. from collections.abc import Callable, Generator
  6. from typing import (
  7. TYPE_CHECKING,
  8. Any,
  9. Generic,
  10. Protocol,
  11. TypedDict,
  12. TypeVar,
  13. Union,
  14. )
  15. if TYPE_CHECKING:
  16. from collections.abc import Iterator
  17. from astroid import bases, exceptions, nodes, transforms, util
  18. from astroid.context import InferenceContext
  19. from astroid.interpreter._import import spec
  20. class InferenceErrorInfo(TypedDict):
  21. """Store additional Inference error information
  22. raised with StopIteration exception.
  23. """
  24. node: nodes.NodeNG
  25. context: InferenceContext | None
  26. class AstroidManagerBrain(TypedDict):
  27. """Dictionary to store relevant information for a AstroidManager class."""
  28. astroid_cache: dict[str, nodes.Module]
  29. _mod_file_cache: dict[
  30. tuple[str, str | None], spec.ModuleSpec | exceptions.AstroidImportError
  31. ]
  32. _failed_import_hooks: list[Callable[[str], nodes.Module]]
  33. always_load_extensions: bool
  34. optimize_ast: bool
  35. max_inferable_values: int
  36. extension_package_whitelist: set[str]
  37. _transform: transforms.TransformVisitor
  38. # pylint: disable=consider-alternative-union-syntax
  39. InferenceResult = Union["nodes.NodeNG", "util.UninferableBase", "bases.Proxy"]
  40. SuccessfulInferenceResult = Union["nodes.NodeNG", "bases.Proxy"]
  41. _SuccessfulInferenceResultT = TypeVar(
  42. "_SuccessfulInferenceResultT", bound=SuccessfulInferenceResult
  43. )
  44. _SuccessfulInferenceResultT_contra = TypeVar(
  45. "_SuccessfulInferenceResultT_contra",
  46. bound=SuccessfulInferenceResult,
  47. contravariant=True,
  48. )
  49. ConstFactoryResult = Union[
  50. "nodes.List",
  51. "nodes.Set",
  52. "nodes.Tuple",
  53. "nodes.Dict",
  54. "nodes.Const",
  55. "nodes.EmptyNode",
  56. ]
  57. InferBinaryOp = Callable[
  58. [
  59. _SuccessfulInferenceResultT,
  60. Union["nodes.AugAssign", "nodes.BinOp"],
  61. str,
  62. InferenceResult,
  63. "InferenceContext",
  64. SuccessfulInferenceResult,
  65. ],
  66. Generator[InferenceResult],
  67. ]
  68. class InferFn(Protocol, Generic[_SuccessfulInferenceResultT_contra]):
  69. def __call__(
  70. self,
  71. node: _SuccessfulInferenceResultT_contra,
  72. context: InferenceContext | None = None,
  73. **kwargs: Any,
  74. ) -> Iterator[InferenceResult]: ... # pragma: no cover
  75. class TransformFn(Protocol, Generic[_SuccessfulInferenceResultT]):
  76. def __call__(
  77. self,
  78. node: _SuccessfulInferenceResultT,
  79. infer_function: InferFn[_SuccessfulInferenceResultT] = ...,
  80. ) -> _SuccessfulInferenceResultT | None: ... # pragma: no cover