Skip to content

Commit a95dca7

Browse files
authored
gh-118761: Improve import time for pstats and zipfile (#128981)
Importing `pstats` or `zipfile` is now roughly 20% faster. This is achieved by removing type annotations depending on `typing`.
1 parent ab353b3 commit a95dca7

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

Lib/pstats.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from enum import StrEnum, _simple_enum
3030
from functools import cmp_to_key
3131
from dataclasses import dataclass
32-
from typing import Dict
3332

3433
__all__ = ["Stats", "SortKey", "FunctionProfile", "StatsProfile"]
3534

@@ -69,7 +68,7 @@ class FunctionProfile:
6968
class StatsProfile:
7069
'''Class for keeping track of an item in inventory.'''
7170
total_tt: float
72-
func_profiles: Dict[str, FunctionProfile]
71+
func_profiles: dict[str, FunctionProfile]
7372

7473
class Stats:
7574
"""This class is used for creating reports from data generated by the

Lib/zipfile/__init__.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import sys
1414
import threading
1515
import time
16-
from typing import Self
1716

1817
try:
1918
import zlib # We may need its compression method
@@ -606,7 +605,7 @@ def from_file(cls, filename, arcname=None, *, strict_timestamps=True):
606605

607606
return zinfo
608607

609-
def _for_archive(self, archive: ZipFile) -> Self:
608+
def _for_archive(self, archive):
610609
"""Resolve suitable defaults from the archive.
611610
612611
Resolve the date_time, compression attributes, and external attributes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Reduce import time of :mod:`pstats` and :mod:`zipfile` by up to 20%, by
2+
removing unnecessary imports to :mod:`typing`. Patch by Bénédikt Tran.

0 commit comments

Comments
 (0)