4 from _typeshed import Self
5 from collections import OrderedDict
6 from collections.abc import Awaitable, Callable, Generator, Mapping, Sequence
23 if sys.version_info >= (3, 7):
24 from types import ClassMethodDescriptorType, WrapperDescriptorType, MemberDescriptorType, MethodDescriptorType
26 from typing import Any, ClassVar, NamedTuple, Protocol, Tuple, Type, TypeVar, Union
27 from typing_extensions import Literal, TypeGuard
32 class EndOfBlock(Exception): ...
40 decoratorhasargs: bool
42 def tokeneater(self, type: int, token: str, srowcol: tuple[int, int], erowcol: tuple[int, int], line: str) -> None: ...
52 CO_ITERABLE_COROUTINE: int
53 CO_ASYNC_GENERATOR: int
54 TPFLAGS_IS_ABSTRACT: int
56 def getmembers(object: object, predicate: Callable[[Any], bool] | None = ...) -> list[tuple[str, Any]]: ...
57 def getmodulename(path: str) -> str | None: ...
58 def ismodule(object: object) -> TypeGuard[ModuleType]: ...
59 def isclass(object: object) -> TypeGuard[Type[Any]]: ...
60 def ismethod(object: object) -> TypeGuard[MethodType]: ...
61 def isfunction(object: object) -> TypeGuard[FunctionType]: ...
63 if sys.version_info >= (3, 8):
64 def isgeneratorfunction(obj: object) -> bool: ...
65 def iscoroutinefunction(obj: object) -> bool: ...
68 def isgeneratorfunction(object: object) -> bool: ...
69 def iscoroutinefunction(object: object) -> bool: ...
71 def isgenerator(object: object) -> TypeGuard[GeneratorType[Any, Any, Any]]: ...
72 def iscoroutine(object: object) -> TypeGuard[CoroutineType[Any, Any, Any]]: ...
73 def isawaitable(object: object) -> TypeGuard[Awaitable[Any]]: ...
75 if sys.version_info >= (3, 8):
76 def isasyncgenfunction(obj: object) -> bool: ...
79 def isasyncgenfunction(object: object) -> bool: ...
81 _T_cont = TypeVar("_T_cont", contravariant=True)
82 _V_cont = TypeVar("_V_cont", contravariant=True)
84 class _SupportsSet(Protocol[_T_cont, _V_cont]):
85 def __set__(self, __instance: _T_cont, __value: _V_cont) -> None: ...
87 class _SupportsDelete(Protocol[_T_cont]):
88 def __delete__(self, __instance: _T_cont) -> None: ...
90 def isasyncgen(object: object) -> TypeGuard[AsyncGeneratorType[Any, Any]]: ...
91 def istraceback(object: object) -> TypeGuard[TracebackType]: ...
92 def isframe(object: object) -> TypeGuard[FrameType]: ...
93 def iscode(object: object) -> TypeGuard[CodeType]: ...
94 def isbuiltin(object: object) -> TypeGuard[BuiltinFunctionType]: ...
96 if sys.version_info < (3, 7):
99 ) -> TypeGuard[FunctionType | LambdaType | MethodType | BuiltinFunctionType | BuiltinMethodType]: ...
100 def ismethoddescriptor(object: object) -> bool: ...
101 def ismemberdescriptor(object: object) -> bool: ...
110 | BuiltinFunctionType
112 | WrapperDescriptorType
113 | MethodDescriptorType
114 | ClassMethodDescriptorType
116 def ismethoddescriptor(object: object) -> TypeGuard[MethodDescriptorType]: ...
117 def ismemberdescriptor(object: object) -> TypeGuard[MemberDescriptorType]: ...
119 def isabstract(object: object) -> bool: ...
120 def isgetsetdescriptor(object: object) -> TypeGuard[GetSetDescriptorType]: ...
121 def isdatadescriptor(object: object) -> TypeGuard[_SupportsSet[Any, Any] | _SupportsDelete[Any]]: ...
124 # Retrieving source code
126 _SourceObjectType = Union[ModuleType, Type[Any], MethodType, FunctionType, TracebackType, FrameType, CodeType, Callable[..., Any]]
128 def findsource(object: _SourceObjectType) -> tuple[list[str], int]: ...
129 def getabsfile(object: _SourceObjectType, _filename: str | None = ...) -> str: ...
130 def getblock(lines: Sequence[str]) -> Sequence[str]: ...
131 def getdoc(object: object) -> str | None: ...
132 def getcomments(object: object) -> str | None: ...
133 def getfile(object: _SourceObjectType) -> str: ...
134 def getmodule(object: object, _filename: str | None = ...) -> ModuleType | None: ...
135 def getsourcefile(object: _SourceObjectType) -> str | None: ...
136 def getsourcelines(object: _SourceObjectType) -> tuple[list[str], int]: ...
137 def getsource(object: _SourceObjectType) -> str: ...
138 def cleandoc(doc: str) -> str: ...
139 def indentsize(line: str) -> int: ...
142 # Introspecting callables with the Signature object
144 if sys.version_info >= (3, 10):
146 obj: Callable[..., Any],
148 follow_wrapped: bool = ...,
149 globals: Mapping[str, Any] | None = ...,
150 locals: Mapping[str, Any] | None = ...,
151 eval_str: bool = ...,
155 def signature(obj: Callable[..., Any], *, follow_wrapped: bool = ...) -> Signature: ...
162 self, parameters: Sequence[Parameter] | None = ..., *, return_annotation: Any = ..., __validate_parameters__: bool = ...
166 def parameters(self) -> types.MappingProxyType[str, Parameter]: ...
167 # TODO: can we be more specific here?
169 def return_annotation(self) -> Any: ...
170 def bind(self, *args: Any, **kwargs: Any) -> BoundArguments: ...
171 def bind_partial(self, *args: Any, **kwargs: Any) -> BoundArguments: ...
173 self: Self, *, parameters: Sequence[Parameter] | Type[_void] | None = ..., return_annotation: Any = ...
175 if sys.version_info >= (3, 10):
179 obj: Callable[..., Any],
181 follow_wrapped: bool = ...,
182 globals: Mapping[str, Any] | None = ...,
183 locals: Mapping[str, Any] | None = ...,
184 eval_str: bool = ...,
188 def from_callable(cls, obj: Callable[..., Any], *, follow_wrapped: bool = ...) -> Signature: ...
190 if sys.version_info >= (3, 10):
192 obj: Callable[..., Any] | Type[Any] | ModuleType,
194 globals: Mapping[str, Any] | None = ...,
195 locals: Mapping[str, Any] | None = ...,
196 eval_str: bool = ...,
197 ) -> dict[str, Any]: ...
199 # The name is the same as the enum's name in CPython
200 class _ParameterKind(enum.IntEnum):
202 POSITIONAL_OR_KEYWORD: int
207 if sys.version_info >= (3, 8):
211 def __init__(self, name: str, kind: _ParameterKind, *, default: Any = ..., annotation: Any = ...) -> None: ...
218 POSITIONAL_ONLY: ClassVar[Literal[_ParameterKind.POSITIONAL_ONLY]]
219 POSITIONAL_OR_KEYWORD: ClassVar[Literal[_ParameterKind.POSITIONAL_OR_KEYWORD]]
220 VAR_POSITIONAL: ClassVar[Literal[_ParameterKind.VAR_POSITIONAL]]
221 KEYWORD_ONLY: ClassVar[Literal[_ParameterKind.KEYWORD_ONLY]]
222 VAR_KEYWORD: ClassVar[Literal[_ParameterKind.VAR_KEYWORD]]
226 name: str | Type[_void] = ...,
227 kind: _ParameterKind | Type[_void] = ...,
229 annotation: Any = ...,
232 class BoundArguments:
233 arguments: OrderedDict[str, Any]
234 args: Tuple[Any, ...]
235 kwargs: dict[str, Any]
237 def __init__(self, signature: Signature, arguments: OrderedDict[str, Any]) -> None: ...
238 def apply_defaults(self) -> None: ...
241 # Classes and functions
244 # TODO: The actual return type should be list[_ClassTreeItem] but mypy doesn't
245 # seem to be supporting this at the moment:
246 # _ClassTreeItem = list[_ClassTreeItem] | Tuple[type, Tuple[type, ...]]
247 def getclasstree(classes: list[type], unique: bool = ...) -> list[Any]: ...
248 def walktree(classes: list[type], children: dict[Type[Any], list[type]], parent: Type[Any] | None) -> list[Any]: ...
250 class ArgSpec(NamedTuple):
254 defaults: Tuple[Any, ...]
256 class Arguments(NamedTuple):
261 def getargs(co: CodeType) -> Arguments: ...
262 def getargspec(func: object) -> ArgSpec: ...
264 class FullArgSpec(NamedTuple):
268 defaults: Tuple[Any, ...] | None
269 kwonlyargs: list[str]
270 kwonlydefaults: dict[str, Any] | None
271 annotations: dict[str, Any]
273 def getfullargspec(func: object) -> FullArgSpec: ...
275 class ArgInfo(NamedTuple):
279 locals: dict[str, Any]
281 def getargvalues(frame: FrameType) -> ArgInfo: ...
282 def formatannotation(annotation: object, base_module: str | None = ...) -> str: ...
283 def formatannotationrelativeto(object: object) -> Callable[[object], str]: ...
286 varargs: str | None = ...,
287 varkw: str | None = ...,
288 defaults: Tuple[Any, ...] | None = ...,
289 kwonlyargs: Sequence[str] | None = ...,
290 kwonlydefaults: dict[str, Any] | None = ...,
291 annotations: dict[str, Any] = ...,
292 formatarg: Callable[[str], str] = ...,
293 formatvarargs: Callable[[str], str] = ...,
294 formatvarkw: Callable[[str], str] = ...,
295 formatvalue: Callable[[Any], str] = ...,
296 formatreturns: Callable[[Any], str] = ...,
297 formatannotation: Callable[[Any], str] = ...,
303 locals: dict[str, Any] | None,
304 formatarg: Callable[[str], str] | None = ...,
305 formatvarargs: Callable[[str], str] | None = ...,
306 formatvarkw: Callable[[str], str] | None = ...,
307 formatvalue: Callable[[Any], str] | None = ...,
309 def getmro(cls: type) -> Tuple[type, ...]: ...
310 def getcallargs(__func: Callable[..., Any], *args: Any, **kwds: Any) -> dict[str, Any]: ...
312 class ClosureVars(NamedTuple):
313 nonlocals: Mapping[str, Any]
314 globals: Mapping[str, Any]
315 builtins: Mapping[str, Any]
318 def getclosurevars(func: Callable[..., Any]) -> ClosureVars: ...
319 def unwrap(func: Callable[..., Any], *, stop: Callable[[Any], Any] | None = ...) -> Any: ...
322 # The interpreter stack
325 class Traceback(NamedTuple):
329 code_context: list[str] | None
330 index: int | None # type: ignore
332 class FrameInfo(NamedTuple):
337 code_context: list[str] | None
338 index: int | None # type: ignore
340 def getframeinfo(frame: FrameType | TracebackType, context: int = ...) -> Traceback: ...
341 def getouterframes(frame: Any, context: int = ...) -> list[FrameInfo]: ...
342 def getinnerframes(tb: TracebackType, context: int = ...) -> list[FrameInfo]: ...
343 def getlineno(frame: FrameType) -> int: ...
344 def currentframe() -> FrameType | None: ...
345 def stack(context: int = ...) -> list[FrameInfo]: ...
346 def trace(context: int = ...) -> list[FrameInfo]: ...
349 # Fetching attributes statically
352 def getattr_static(obj: object, attr: str, default: Any | None = ...) -> Any: ...
355 # Current State of Generators and Coroutines
358 # TODO In the next two blocks of code, can we be more specific regarding the
359 # type of the "enums"?
366 def getgeneratorstate(generator: Generator[Any, Any, Any]) -> str: ...
372 # TODO can we be more specific than "object"?
373 def getcoroutinestate(coroutine: object) -> str: ...
374 def getgeneratorlocals(generator: Generator[Any, Any, Any]) -> dict[str, Any]: ...
376 # TODO can we be more specific than "object"?
377 def getcoroutinelocals(coroutine: object) -> dict[str, Any]: ...
379 # Create private type alias to avoid conflict with symbol of same
380 # name created in Attribute class.
383 class Attribute(NamedTuple):
389 def classify_class_attrs(cls: type) -> list[Attribute]: ...
391 if sys.version_info >= (3, 9):
392 class ClassFoundException(Exception): ...