Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions line_profiler/autoprofile/line_profiler_utils.pyi
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
from types import ModuleType
from typing import overload, Any, Literal, TYPE_CHECKING
from functools import partial, partialmethod, cached_property
from types import FunctionType, MethodType, ModuleType
from typing import overload, Any, Literal, TypeVar, TYPE_CHECKING

if TYPE_CHECKING: # Stub-only annotations
from ..line_profiler import CallableLike
from ..profiler_mixin import CLevelCallable
from ..profiler_mixin import CLevelCallable, CythonCallable
from ..scoping_policy import ScopingPolicy, ScopingPolicyDict




@overload
def add_imported_function_or_module(
self, item: CLevelCallable | Any,
scoping_policy: (
ScopingPolicy | str | ScopingPolicyDict | None) = None,
scoping_policy: ScopingPolicy | str | ScopingPolicyDict | None = None,
wrap: bool = False) -> Literal[0]:
...


@overload
def add_imported_function_or_module(
self, item: CallableLike | ModuleType,
scoping_policy: (
ScopingPolicy | str | ScopingPolicyDict | None) = None,
self,
item: (FunctionType | CythonCallable
| type | partial | property | cached_property
| MethodType | staticmethod | classmethod | partialmethod
| ModuleType),
scoping_policy: ScopingPolicy | str | ScopingPolicyDict | None = None,
wrap: bool = False) -> Literal[0, 1]:
...
6 changes: 3 additions & 3 deletions line_profiler/cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def boolean(value, *, fallback=None, invert=False):
Arguments:
value (str)
Value to be parsed into a boolean (case insensitive)
fallback (Union[bool, None])
fallback (bool | None)
Optional value to fall back to in case ``value`` doesn't
match any of the specified
invert (bool)
Expand Down Expand Up @@ -278,12 +278,12 @@ def boolean(value, *, fallback=None, invert=False):
def short_string_path(path):
"""
Arguments:
path (Union[str, PurePath]):
path (str | os.PathLike[str]):
Path-like

Returns:
str: short_path
The shortest formatted ``path`` among the provided path, the
The shortest formatted path among the provided ``path``, the
corresponding absolute path, and its relative path to the
current directory.
"""
Expand Down
16 changes: 9 additions & 7 deletions line_profiler/cli_utils.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ Shared utilities between the :command:`python -m line_profiler` and
"""
import argparse
import pathlib
from typing import Protocol, Sequence, Tuple, TypeVar, Union
from os import PathLike
from typing import Protocol, Sequence, Tuple, TypeVar

from line_profiler.toml_config import ConfigSource


P_con = TypeVar('P_con', bound='ParserLike', contravariant=True)
A_co = TypeVar('A_co', bound='ActionLike', covariant=True)


class ActionLike(Protocol):
def __call__(self, parser: 'ParserLike',
class ActionLike(Protocol[P_con]):
def __call__(self, parser: P_con,
namespace: argparse.Namespace,
values: Sequence,
option_string: Union[str, None] = None) -> None:
values: str | Sequence | None,
option_string: str | None = None) -> None:
...

def format_usage(self) -> str:
Expand Down Expand Up @@ -50,9 +52,9 @@ def positive_float(value: str) -> float:


def boolean(value: str, *,
fallback: Union[bool, None] = None, invert: bool = False) -> bool:
fallback: bool | None = None, invert: bool = False) -> bool:
...


def short_string_path(path: Union[str, pathlib.PurePath]) -> str:
def short_string_path(path: str | PathLike[str]) -> str:
...
65 changes: 50 additions & 15 deletions line_profiler/line_profiler.pyi
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import io
import pathlib
from functools import cached_property, partial, partialmethod
from types import FunctionType, MethodType, ModuleType
from typing import (overload,
Callable, List, Literal, Mapping, Tuple,
TypeVar, Union)
from os import PathLike
from types import FunctionType, ModuleType
from typing import TYPE_CHECKING, overload, Callable, Literal, Mapping, TypeVar
try:
from typing import ( # type: ignore[attr-defined] # noqa: F401
ParamSpec)
except ImportError:
from typing_extensions import ParamSpec # noqa: F401
from _typeshed import Incomplete
from ._line_profiler import LineProfiler as CLineProfiler
from .profiler_mixin import ByCountProfilerMixin, CLevelCallable
from .scoping_policy import ScopingPolicy, ScopingPolicyDict

if TYPE_CHECKING:
from .profiler_mixin import UnparametrizedCallableLike

CallableLike = TypeVar('CallableLike',
FunctionType, partial, property, cached_property,
MethodType, staticmethod, classmethod, partialmethod,
type)

T = TypeVar('T')
T_co = TypeVar('T_co', covariant=True)
PS = ParamSpec('PS')


def get_column_widths(
config: Union[bool, str, pathlib.PurePath, None] = False) -> Mapping[
config: bool | str | PathLike[str] | None = False) -> Mapping[
Literal['line', 'hits', 'time', 'perhit', 'percent'], int]:
...

Expand All @@ -33,9 +39,39 @@ class LineProfiler(CLineProfiler, ByCountProfilerMixin):
func: CLevelCallable) -> CLevelCallable:
...

@overload
def __call__( # type: ignore[overload-overlap]
self, func: UnparametrizedCallableLike,
) -> UnparametrizedCallableLike:
...

@overload
def __call__(self, # type: ignore[overload-overlap]
func: CallableLike) -> CallableLike:
func: type[T]) -> type[T]:
...

@overload
def __call__(self, # type: ignore[overload-overlap]
func: partial[T]) -> partial[T]:
...

@overload
def __call__(self, func: partialmethod[T]) -> partialmethod[T]:
...

@overload
def __call__(self, func: cached_property[T_co]) -> cached_property[T_co]:
...

@overload
def __call__(self, # type: ignore[overload-overlap]
func: staticmethod[PS, T_co]) -> staticmethod[PS, T_co]:
...

@overload
def __call__(
self, func: classmethod[type[T], PS, T_co],
) -> classmethod[type[T], PS, T_co]:
...

# Fallback: just wrap the `.__call__()` of a generic callable
Expand All @@ -62,8 +98,7 @@ class LineProfiler(CLineProfiler, ByCountProfilerMixin):
sort: bool = ...,
rich: bool = ...,
*,
config: Union[str, pathlib.PurePath,
bool, None] = None) -> None:
config: str | PathLike[str] | bool | None = None) -> None:
...

def add_module(
Expand All @@ -88,14 +123,14 @@ def is_generated_code(filename):
def show_func(filename: str,
start_lineno: int,
func_name: str,
timings: List[Tuple[int, int, float]],
timings: list[tuple[int, int, float]],
unit: float,
output_unit: float | None = None,
stream: io.TextIOBase | None = None,
stripzeros: bool = False,
rich: bool = False,
*,
config: Union[str, pathlib.PurePath, bool, None] = None) -> None:
config: str | PathLike[str] | bool | None = None) -> None:
...


Expand All @@ -109,7 +144,7 @@ def show_text(stats,
sort: bool = ...,
rich: bool = ...,
*,
config: Union[str, pathlib.PurePath, bool, None] = None) -> None:
config: str | PathLike[str] | bool | None = None) -> None:
...


Expand Down
93 changes: 71 additions & 22 deletions line_profiler/profiler_mixin.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ from types import (CodeType, FunctionType, MethodType,
BuiltinFunctionType, BuiltinMethodType,
ClassMethodDescriptorType, MethodDescriptorType,
MethodWrapperType, WrapperDescriptorType)
from typing import (TYPE_CHECKING,
Any, Callable, Dict, List, Mapping, Protocol, TypeVar)
from typing import (TYPE_CHECKING, overload,
Any, Callable, Mapping, Protocol, TypeVar)
try:
from typing import ( # type: ignore[attr-defined] # noqa: F401
ParamSpec)
Expand All @@ -23,9 +23,10 @@ except ImportError: # Python < 3.13
from ._line_profiler import label


T = TypeVar('T', bound=type)
UnparametrizedCallableLike = TypeVar('UnparametrizedCallableLike',
FunctionType, property, MethodType)
T = TypeVar('T')
T_co = TypeVar('T_co', covariant=True)
R = TypeVar('R')
PS = ParamSpec('PS')

if TYPE_CHECKING:
Expand Down Expand Up @@ -66,31 +67,31 @@ if TYPE_CHECKING:
...

@property
def __globals__(self) -> Dict[str, Any]:
def __globals__(self) -> dict[str, Any]:
...

@property
def func_globals(self) -> Dict[str, Any]:
def func_globals(self) -> dict[str, Any]:
...

@property
def __dict__(self) -> Dict[str, Any]:
def __dict__(self) -> dict[str, Any]:
...

@__dict__.setter
def __dict__(self, dict: Dict[str, Any]) -> None:
def __dict__(self, dict: dict[str, Any]) -> None:
...

@property
def func_dict(self) -> Dict[str, Any]:
def func_dict(self) -> dict[str, Any]:
...

@property
def __annotations__(self) -> Dict[str, Any]:
def __annotations__(self) -> dict[str, Any]:
...

@__annotations__.setter
def __annotations__(self, annotations: Dict[str, Any]) -> None:
def __annotations__(self, annotations: dict[str, Any]) -> None:
...

@property
Expand Down Expand Up @@ -160,31 +161,79 @@ def is_cached_property(f: Any) -> TypeIs[cached_property]:


class ByCountProfilerMixin:
def get_underlying_functions(self, func) -> List[FunctionType]:
def get_underlying_functions(self, func) -> list[FunctionType]:
...

def wrap_callable(self, func):
@overload
def wrap_callable(self, # type: ignore[overload-overlap]
func: CLevelCallable) -> CLevelCallable:
...

def wrap_classmethod(self, func: classmethod) -> classmethod:
@overload
def wrap_callable( # type: ignore[overload-overlap]
self, func: UnparametrizedCallableLike,
) -> UnparametrizedCallableLike:
...

def wrap_staticmethod(self, func: staticmethod) -> staticmethod:
@overload
def wrap_callable(self, # type: ignore[overload-overlap]
func: type[T]) -> type[T]:
...

@overload
def wrap_callable(self, # type: ignore[overload-overlap]
func: partial[T]) -> partial[T]:
...

@overload
def wrap_callable(self, func: partialmethod[T]) -> partialmethod[T]:
...

@overload
def wrap_callable(self,
func: cached_property[T_co]) -> cached_property[T_co]:
...

@overload
def wrap_callable(self, # type: ignore[overload-overlap]
func: staticmethod[PS, T_co]) -> staticmethod[PS, T_co]:
...

@overload
def wrap_callable(
self, func: classmethod[type[T], PS, T_co],
) -> classmethod[type[T], PS, T_co]:
...

# Fallback: just return a wrapper function around a generic callable

@overload
def wrap_callable(self, func: Callable) -> FunctionType:
...

def wrap_classmethod(
self, func: classmethod[type[T], PS, T_co],
) -> classmethod[type[T], PS, T_co]:
...

def wrap_staticmethod(
self, func: staticmethod[PS, T_co]) -> staticmethod[PS, T_co]:
...

def wrap_boundmethod(self, func: MethodType) -> MethodType:
...

def wrap_partialmethod(self, func: partialmethod) -> partialmethod:
def wrap_partialmethod(self, func: partialmethod[T]) -> partialmethod[T]:
...

def wrap_partial(self, func: partial) -> partial:
def wrap_partial(self, func: partial[T]) -> partial[T]:
...

def wrap_property(self, func: property) -> property:
...

def wrap_cached_property(self, func: cached_property) -> cached_property:
def wrap_cached_property(
self, func: cached_property[T_co]) -> cached_property[T_co]:
...

def wrap_async_generator(self, func: FunctionType) -> FunctionType:
Expand All @@ -199,20 +248,20 @@ class ByCountProfilerMixin:
def wrap_function(self, func: Callable) -> FunctionType:
...

def wrap_class(self, func: T) -> T:
def wrap_class(self, func: type[T]) -> type[T]:
...

def run(self, cmd: str) -> Self:
...

def runctx(self,
cmd: str,
globals: Dict[str, Any] | None,
globals: dict[str, Any] | None,
locals: Mapping[str, Any] | None) -> Self:
...

def runcall(self, func: Callable[PS, R], /,
*args: PS.args, **kw: PS.kwargs) -> R:
def runcall(self, func: Callable[PS, T], /,
*args: PS.args, **kw: PS.kwargs) -> T:
...

def __enter__(self) -> Self:
Expand Down
Loading
Loading