xref: /petsc/lib/petsc/bin/maint/petsclinter/petsclinter/_typing.py (revision 4c7cc9c8e01791debde927bc0816c9a347055c8f)
1#!/usr/bin/env python3
2"""
3# Created: Thu Jul 27 13:53:56 2023 (-0400)
4# @author: Jacob Faibussowitsch
5"""
6from __future__ import annotations
7
8import sys
9
10from typing import (
11  TYPE_CHECKING,
12  Union, Optional, TypedDict, TypeVar, NamedTuple, Generic, Protocol,
13  overload, cast as TYPE_CAST
14)
15
16from .__version__ import py_version_lt
17
18py_version_lt(3, 9)
19# Dummy function call conditional to make sure you remember to remove the following line
20from typing import List, Dict, Tuple, Type
21
22py_version_lt(3, 10)
23# Also TypeAlias below
24if sys.version_info >= (3, 10):
25  # novermin
26  from typing import ParamSpec
27else:
28  from typing_extensions import ParamSpec
29
30if TYPE_CHECKING:
31  import re
32  import pathlib
33  import weakref
34  import clang.cindex as clx # type: ignore[import]
35
36  from typing import Any, NoReturn, ClassVar, SupportsInt
37
38  if sys.version_info >= (3, 10):
39    # novermin
40    from typing import TypeAlias
41  else:
42    from typing_extensions import TypeAlias
43
44  from collections.abc import (
45    Iterator, Iterable, Generator, Callable, Collection, Sequence, Container, Mapping
46  )
47
48  ##
49  # CLASSES
50  ##
51
52  from .classes._cursor     import Cursor
53  from .classes._linter     import Linter, WeakList
54  from .classes._patch      import Patch
55  from .classes._path       import Path
56  from .classes._pool       import WorkerPoolBase, ParallelPool, SerialPool
57  from .classes._src_pos    import SourceLocation, SourceRange
58  from .classes._attr_cache import AttributeCache
59  from .classes._scope      import Scope
60  from .classes._weak_list  import WeakList
61  from .classes._add_line   import Addline
62  from .classes._diag       import (
63    DiagnosticMapProxy, DiagnosticMap, DiagnosticsManagerCls, DiagnosticKind, Diagnostic
64  )
65
66  PathLike: TypeAlias           = Union[pathlib.Path, Path]
67  StrPathLike: TypeAlias        = Union[PathLike, str]
68  CursorLike: TypeAlias         = Union[clx.Cursor, Cursor]
69  SourceLocationLike: TypeAlias = Union[clx.SourceLocation, SourceLocation]
70  SourceRangeLike: TypeAlias    = Union[clx.SourceRange, SourceRange]
71  PoolImpl                      = TypeVar('PoolImpl', bound=WorkerPoolBase)
72  PathDiffPair: TypeAlias       = Tuple[Path, str]
73  CondensedDiags: TypeAlias     = Dict[Path, List[str]]
74  WeakListType: TypeAlias       = WeakList[Tuple[str, bool, int]]
75  WeakListRef: TypeAlias        = weakref.ReferenceType[WeakListType]
76
77  class Formattable(Protocol):
78    # Want to do the following,
79    #
80    # def formatted(self, num_before_context: int = 0, num_after_context: int = 0, num_context: int = 0, view: bool = False, highlight: bool = True, trim: bool = True) -> str:
81    #   ...
82    #
83    # def raw(self, num_before_context: int = 0, num_after_context: int = 0, num_context: int = 0, trim: bool = True, tight: bool = False) -> str:
84    #   ...
85    #
86    # but then mypy complains
87    #
88    # error: Argument "crange" to "make_diagnostic_message" of
89    # "PetscDocString" has incompatible type "Cursor"; expected "Optional[Formattable]"  [arg-type]
90    #               'Parameter list defined here', crange=docstring.cursor
91    #                                                     ^~~~~~~~~~~~~~~~
92    # note: Following member(s) of "Cursor" have conflicts:
93    # note:     Expected:
94    # note:         def formatted(self, num_before_context: int = ..., num_after_context: int = ..., num_context: int = ..., view: bool = ..., highlight: bool = ..., trim: bool = ...) -> str
95    # note:     Got:
96    # note:         def formatted(self, **kwargs: Any) -> str
97    # note:     Expected:
98    # note:         def raw(self, num_before_context: int = ..., num_after_context: int = ..., num_context: int = ..., trim: bool = ..., tight: bool = ...) -> str
99    # note:     Got:
100    # note:         def raw(self, **kwargs: Any) -> str
101    #
102    # So instead we find ourselves reduced to this...
103    def formatted(self, **kwargs: Any) -> str:
104      ...
105
106    def raw(self, **kwargs: Any) -> str:
107      ...
108
109  ##
110  # DOCS
111  ##
112
113  from .classes.docs._doc_str          import Verdict, PetscDocString
114  from .classes.docs._doc_section_base import (
115    DescribableItem, SectionBase, Synopsis, ParameterList, Prose, VerbatimBlock, InlineList,
116  )
117  from .classes.docs._doc_section      import (
118    FunctionSynopsis, EnumSynopsis, FunctionParameterList, OptionDatabaseKeys, Notes, DeveloperNotes,
119    References, FortranNotes, SourceCode, Level, SeeAlso
120  )
121
122  SectionImpl        = TypeVar('SectionImpl', bound=SectionBase)
123  PetscDocStringImpl = TypeVar('PetscDocStringImpl', bound=PetscDocString)
124  SynopsisImpl       = TypeVar('SynopsisImpl', bound=Synopsis)
125
126  ##
127  # UTIL
128  ##
129
130  from .util._clang   import CXTranslationUnit, ClangFunction
131  from .util._color   import Color
132  from .util._utility import PrecompiledHeader
133
134  ExceptionKind = TypeVar('ExceptionKind', bound=Exception)
135
136  ##
137  # CHECKS
138  ##
139
140  FunctionChecker: TypeAlias = Callable[[Linter, Cursor, Cursor], None]
141  DocChecker: TypeAlias      = Callable[[Linter, Cursor], None]
142
143del py_version_lt
144