Skip to content

Commit

Permalink
Avoid importing typing to improve startup time 35% (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
hamdanal authored Oct 7, 2023
1 parent f38bd65 commit e3c4dac
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
- Add `HelpPreviewAction` to generate a preview of the help output in SVG, HTML, or TXT formats.
* Issue [#91](https://github.com/hamdanal/rich-argparse/issues/91),
PR [#93](https://github.com/hamdanal/rich-argparse/pull/93)
- Avoid importing `typing` to improve startup time by about 35%.
* PR [#97](https://github.com/hamdanal/rich-argparse/pull/97)

## 1.3.0 - 2023-08-19

Expand Down
4 changes: 3 additions & 1 deletion rich_argparse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
import argparse
import re
import sys
from typing import TYPE_CHECKING, Any, ClassVar

import rich_argparse._lazy_rich as r
from rich_argparse._common import _HIGHLIGHTS, _fix_legacy_win_text, _rich_fill, _rich_wrap

TYPE_CHECKING = False
if TYPE_CHECKING:
from argparse import Action, ArgumentParser, Namespace, _MutuallyExclusiveGroup
from collections.abc import Callable, Iterable, Iterator, MutableMapping, Sequence
from typing import Any, ClassVar
from typing_extensions import Self
del TYPE_CHECKING

__all__ = [
"RichHelpFormatter",
Expand Down
35 changes: 18 additions & 17 deletions rich_argparse/_lazy_rich.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
# for internal use only
from __future__ import annotations

from typing import TYPE_CHECKING, Any
TYPE_CHECKING = False
if TYPE_CHECKING:
from typing import Any

from rich.ansi import re_ansi as re_ansi
from rich.console import Console as Console
from rich.console import ConsoleOptions as ConsoleOptions
from rich.console import RenderableType as RenderableType
from rich.console import RenderResult as RenderResult
from rich.containers import Lines as Lines
from rich.control import strip_control_codes as strip_control_codes
from rich.markup import escape as escape
from rich.padding import Padding as Padding
from rich.segment import Segment as Segment
from rich.style import StyleType as StyleType
from rich.text import Span as Span
from rich.text import Text as Text
from rich.theme import Theme as Theme

__all__ = [
"re_ansi",
Expand All @@ -20,22 +37,6 @@
"Theme",
]

if TYPE_CHECKING:
from rich.ansi import re_ansi as re_ansi
from rich.console import Console as Console
from rich.console import ConsoleOptions as ConsoleOptions
from rich.console import RenderableType as RenderableType
from rich.console import RenderResult as RenderResult
from rich.containers import Lines as Lines
from rich.control import strip_control_codes as strip_control_codes
from rich.markup import escape as escape
from rich.padding import Padding as Padding
from rich.segment import Segment as Segment
from rich.style import StyleType as StyleType
from rich.text import Span as Span
from rich.text import Text as Text
from rich.theme import Theme as Theme


def __getattr__(name: str) -> Any:
if name not in __all__:
Expand Down

0 comments on commit e3c4dac

Please sign in to comment.