From 51b070b27c1095e6595d9f25a9ab1929328320db Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Tue, 12 Dec 2023 19:26:30 -0800 Subject: [PATCH 1/6] add _lsprof module related to https://github.com/python/typeshed/issues/3968 --- stdlib/VERSIONS | 1 + stdlib/_lsprof.pyi | 33 +++++++++++++++++++++++++++++++++ stdlib/cProfile.pyi | 9 +++------ 3 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 stdlib/_lsprof.pyi diff --git a/stdlib/VERSIONS b/stdlib/VERSIONS index f5e30c4602b4..fc86a75d3dea 100644 --- a/stdlib/VERSIONS +++ b/stdlib/VERSIONS @@ -36,6 +36,7 @@ _heapq: 2.7- _imp: 3.0- _json: 2.7- _locale: 2.7- +_lsprof: 2.7- _markupbase: 2.7- _msi: 2.7- _operator: 3.4- diff --git a/stdlib/_lsprof.pyi b/stdlib/_lsprof.pyi new file mode 100644 index 000000000000..f00deaadfba6 --- /dev/null +++ b/stdlib/_lsprof.pyi @@ -0,0 +1,33 @@ +from _typeshed import structseq +from collections.abc import Callable +from types import CodeType +from typing import Any +from typing_extensions import Final, final + +class Profiler: + def __init__( + self, timer: Callable[[], float] | None = None, timeunit: float = 0.0, subcalls: bool = True, builtins: bool = True + ) -> None: ... + def getstats(self) -> list[profiler_entry]: ... + def enable(self, subcalls: bool = True, builtins: bool = True) -> None: ... + def disable(self) -> None: ... + def clear(self) -> None: ... + +@final +class profiler_entry(structseq[Any], tuple[CodeType | str, int, int, float, float, list[profiler_subentry]]): + __match_args__: Final = ("code", "callcount", "reccallcount", "totaltime", "inlinetime", "calls") + code: CodeType | str + callcount: int + reccallcount: int + totaltime: float + inlinetime: float + calls: list[profiler_subentry] + +@final +class profiler_subentry(structseq[Any], tuple[CodeType | str, int, int, float, float]): + __match_args__: Final = ("code", "callcount", "reccallcount", "totaltime", "inlinetime") + code: CodeType | str + callcount: int + reccallcount: int + totaltime: float + inlinetime: float diff --git a/stdlib/cProfile.pyi b/stdlib/cProfile.pyi index 8945b21427ab..23a3c401c955 100644 --- a/stdlib/cProfile.pyi +++ b/stdlib/cProfile.pyi @@ -5,6 +5,8 @@ from types import CodeType from typing import Any, TypeVar from typing_extensions import ParamSpec, Self, TypeAlias +import _lsprof + __all__ = ["run", "runctx", "Profile"] def run(statement: str, filename: str | None = None, sort: str | int = -1) -> None: ... @@ -16,13 +18,8 @@ _T = TypeVar("_T") _P = ParamSpec("_P") _Label: TypeAlias = tuple[str, int, str] -class Profile: +class Profile(_lsprof.Profiler): stats: dict[_Label, tuple[int, int, int, int, dict[_Label, tuple[int, int, int, int]]]] # undocumented - def __init__( - self, timer: Callable[[], float] = ..., timeunit: float = ..., subcalls: bool = ..., builtins: bool = ... - ) -> None: ... - def enable(self) -> None: ... - def disable(self) -> None: ... def print_stats(self, sort: str | int = -1) -> None: ... def dump_stats(self, file: StrOrBytesPath) -> None: ... def create_stats(self) -> None: ... From e2a8dd8b762c7a58471b08fb237b43fa88704e4f Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Wed, 13 Dec 2023 01:40:14 -0800 Subject: [PATCH 2/6] Apply suggestions from code review Co-authored-by: Alex Waygood --- stdlib/_lsprof.pyi | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/stdlib/_lsprof.pyi b/stdlib/_lsprof.pyi index f00deaadfba6..ec815ea2f618 100644 --- a/stdlib/_lsprof.pyi +++ b/stdlib/_lsprof.pyi @@ -15,7 +15,8 @@ class Profiler: @final class profiler_entry(structseq[Any], tuple[CodeType | str, int, int, float, float, list[profiler_subentry]]): - __match_args__: Final = ("code", "callcount", "reccallcount", "totaltime", "inlinetime", "calls") + if sys.version_info >= (3, 10): + __match_args__: Final = ("code", "callcount", "reccallcount", "totaltime", "inlinetime", "calls") code: CodeType | str callcount: int reccallcount: int @@ -25,7 +26,8 @@ class profiler_entry(structseq[Any], tuple[CodeType | str, int, int, float, floa @final class profiler_subentry(structseq[Any], tuple[CodeType | str, int, int, float, float]): - __match_args__: Final = ("code", "callcount", "reccallcount", "totaltime", "inlinetime") + if sys.version_info >= (3, 10): + __match_args__: Final = ("code", "callcount", "reccallcount", "totaltime", "inlinetime") code: CodeType | str callcount: int reccallcount: int From fd7632a5a8eadcd4b1b345d9713c48df150da684 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Wed, 13 Dec 2023 09:43:51 +0000 Subject: [PATCH 3/6] Update stdlib/_lsprof.pyi --- stdlib/_lsprof.pyi | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/_lsprof.pyi b/stdlib/_lsprof.pyi index ec815ea2f618..14f20a90c1d3 100644 --- a/stdlib/_lsprof.pyi +++ b/stdlib/_lsprof.pyi @@ -1,3 +1,4 @@ +import sys from _typeshed import structseq from collections.abc import Callable from types import CodeType From 5baa3ac5d35d46430f18e00babb44e66ec210e63 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Wed, 13 Dec 2023 10:56:55 -0800 Subject: [PATCH 4/6] add _lsprof to ruff's isort config --- pyproject.toml | 1 + stdlib/cProfile.pyi | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 9efc93d93cab..02cbee56bc59 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -68,6 +68,7 @@ extra-standard-library = [ "_imp", "_json", "_locale", + "_lsprof", "_markupbase", "_msi", "_operator", diff --git a/stdlib/cProfile.pyi b/stdlib/cProfile.pyi index 23a3c401c955..93b38436fccf 100644 --- a/stdlib/cProfile.pyi +++ b/stdlib/cProfile.pyi @@ -1,3 +1,4 @@ +import _lsprof import sys from _typeshed import StrOrBytesPath, Unused from collections.abc import Callable @@ -5,8 +6,6 @@ from types import CodeType from typing import Any, TypeVar from typing_extensions import ParamSpec, Self, TypeAlias -import _lsprof - __all__ = ["run", "runctx", "Profile"] def run(statement: str, filename: str | None = None, sort: str | int = -1) -> None: ... From d504ca8ad5745dc2870821db446ee243357ecd69 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 16 Feb 2024 14:55:01 +0000 Subject: [PATCH 5/6] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/_lsprof.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/_lsprof.pyi b/stdlib/_lsprof.pyi index 14f20a90c1d3..b4b3ac9bd57f 100644 --- a/stdlib/_lsprof.pyi +++ b/stdlib/_lsprof.pyi @@ -2,8 +2,8 @@ import sys from _typeshed import structseq from collections.abc import Callable from types import CodeType -from typing import Any -from typing_extensions import Final, final +from typing import Any, Final +from typing_extensions import final class Profiler: def __init__( From 2f7d91e175c34641910efecf8bda36e270ead5d8 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 16 Feb 2024 06:58:52 -0800 Subject: [PATCH 6/6] final typing --- stdlib/_lsprof.pyi | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stdlib/_lsprof.pyi b/stdlib/_lsprof.pyi index b4b3ac9bd57f..8a6934162c92 100644 --- a/stdlib/_lsprof.pyi +++ b/stdlib/_lsprof.pyi @@ -2,8 +2,7 @@ import sys from _typeshed import structseq from collections.abc import Callable from types import CodeType -from typing import Any, Final -from typing_extensions import final +from typing import Any, Final, final class Profiler: def __init__(