Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡️ Speed up function __getattr__ by 30% #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
63 changes: 13 additions & 50 deletions src/click/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
from __future__ import annotations

from .core import Argument as Argument
from .core import Command as Command
from .core import CommandCollection as CommandCollection
from .core import Context as Context
from .core import Group as Group
from .core import Option as Option
from .core import Parameter as Parameter
from .decorators import argument as argument
Expand Down Expand Up @@ -74,51 +71,17 @@

def __getattr__(name: str) -> object:
import warnings

if name == "BaseCommand":
from .core import _BaseCommand

warnings.warn(
"'BaseCommand' is deprecated and will be removed in Click 9.0. Use"
" 'Command' instead.",
DeprecationWarning,
stacklevel=2,
)
return _BaseCommand

if name == "MultiCommand":
from .core import _MultiCommand

warnings.warn(
"'MultiCommand' is deprecated and will be removed in Click 9.0. Use"
" 'Group' instead.",
DeprecationWarning,
stacklevel=2,
)
return _MultiCommand

if name == "OptionParser":
from .parser import _OptionParser

warnings.warn(
"'OptionParser' is deprecated and will be removed in Click 9.0. The"
" old parser is available in 'optparse'.",
DeprecationWarning,
stacklevel=2,
)
return _OptionParser

if name == "__version__":
import importlib.metadata
import warnings

warnings.warn(
"The '__version__' attribute is deprecated and will be removed in"
" Click 9.1. Use feature detection or"
" 'importlib.metadata.version(\"click\")' instead.",
DeprecationWarning,
stacklevel=2,
)
return importlib.metadata.version("click")

lookup = {
"BaseCommand": ("'BaseCommand' is deprecated and will be removed in Click 9.0. Use 'Command' instead.", ".core", "_BaseCommand"),
"MultiCommand": ("'MultiCommand' is deprecated and will be removed in Click 9.0. Use 'Group' instead.", ".core", "_MultiCommand"),
"OptionParser": ("'OptionParser' is deprecated and will be removed in Click 9.0. The old parser is available in 'optparse'.", ".parser", "_OptionParser"),
"__version__": ("The '__version__' attribute is deprecated and will be removed in Click 9.1. Use feature detection or 'importlib.metadata.version(\"click\")' instead.", "importlib.metadata", "version")
}
if name in lookup:
msg, module, attr = lookup[name]
warnings.warn(msg, DeprecationWarning, stacklevel=2)
mod = __import__(module, fromlist=[attr])
if name == "__version__":
return getattr(mod, attr)("click")
return getattr(mod, attr)
raise AttributeError(name)