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

Improved termui.pause() Function and Simplified Imports #2774

Closed
wants to merge 6 commits into from

Conversation

rly0nheart
Copy link

This pull request includes two main changes:

  1. Pause Function Update:

    • The termui.pause() function has been improved by adding the default message ("Press any key to continue...") directly in the info param. This change improves readability and eliminates unnecessary checks for the info param
    • Additionally, I removed the if info checks since info will now have a default string value.
  2. Simplified Import Statements:

    • Multiple imports from the same module have been combined into single lines to reduce clutter and make the codebase more maintainable.
    • Specifically, imports from _compat, exceptions, types, and other modules have been simplified.

Quick Question:
I noticed these imports here:

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
from .decorators import command as command
from .decorators import confirmation_option as confirmation_option
from .decorators import group as group
from .decorators import help_option as help_option
from .decorators import make_pass_decorator as make_pass_decorator
from .decorators import option as option
from .decorators import pass_context as pass_context
from .decorators import pass_obj as pass_obj
from .decorators import password_option as password_option
from .decorators import version_option as version_option
from .exceptions import Abort as Abort
from .exceptions import BadArgumentUsage as BadArgumentUsage
from .exceptions import BadOptionUsage as BadOptionUsage
from .exceptions import BadParameter as BadParameter
from .exceptions import ClickException as ClickException
from .exceptions import FileError as FileError
from .exceptions import MissingParameter as MissingParameter
from .exceptions import NoSuchOption as NoSuchOption
from .exceptions import UsageError as UsageError
from .formatting import HelpFormatter as HelpFormatter
from .formatting import wrap_text as wrap_text
from .globals import get_current_context as get_current_context
from .termui import clear as clear
from .termui import confirm as confirm
from .termui import echo_via_pager as echo_via_pager
from .termui import edit as edit
from .termui import getchar as getchar
from .termui import launch as launch
from .termui import pause as pause
from .termui import progressbar as progressbar
from .termui import prompt as prompt
from .termui import secho as secho
from .termui import style as style
from .termui import unstyle as unstyle
from .types import BOOL as BOOL
from .types import Choice as Choice
from .types import DateTime as DateTime
from .types import File as File
from .types import FLOAT as FLOAT
from .types import FloatRange as FloatRange
from .types import INT as INT
from .types import IntRange as IntRange
from .types import ParamType as ParamType
from .types import Path as Path
from .types import STRING as STRING
from .types import Tuple as Tuple
from .types import UNPROCESSED as UNPROCESSED
from .types import UUID as UUID
from .utils import echo as echo
from .utils import format_filename as format_filename
from .utils import get_app_dir as get_app_dir
from .utils import get_binary_stream as get_binary_stream
from .utils import get_text_stream as get_text_stream
from .utils import open_file as open_file

Is there a reason for using as when the names are the same?

I’m curious if there’s a specific reason for doing it this way, or if it’s something that could be simplified like so:

from .core import (
    Argument,
    Command,
    CommandCollection,
    Context,
    Group,
    Option,
    Parameter,
)

from .decorators import (
    argument,
    command,
    confirmation_option,
    group,
    help_option,
    make_pass_decorator,
    option,
    pass_context,
    pass_obj,
    password_option,
    version_option,
)

from .exceptions import (
    Abort,
    BadArgumentUsage,
    BadOptionUsage,
    BadParameter,
    ClickException,
    FileError,
    MissingParameter,
    NoSuchOption,
    UsageError,
)

from .formatting import HelpFormatter, wrap_text
from .globals import get_current_context
from .termui import (
    clear,
    confirm,
    echo_via_pager,
    edit,
    getchar,
    launch,
    pause,
    progressbar,
    prompt,
    secho,
    style,
    unstyle,
)

from .types import (
    BOOL,
    Choice,
    DateTime,
    File,
    FLOAT,
    FloatRange,
    INT,
    IntRange,
    ParamType,
    Path,
    STRING,
    Tuple,
    UNPROCESSED,
    UUID,
)

from .utils import (
    echo,
    format_filename,
    get_app_dir,
    get_binary_stream,
    get_text_stream,
    open_file,
)

Just want to understand the thought process behind it!

rly0nheart and others added 6 commits September 5, 2024 23:36
1. Set a default message ("Press any key to continue...") for the info parameter in the pause() function. This simplifies the function's use by providing a default without requiring explicit checks for a None value

2. Refactor the (pause()) function to remove redundant checks and simplify the control flow, improving readability while maintaining the existing functionality.

3. Simplify imports to avoid clutter.
Simplfied imports to improved readability and avoid clutter.
@@ -771,16 +773,11 @@ def pause(info: str | None = None, err: bool = False) -> None:
if not isatty(sys.stdin) or not isatty(sys.stdout):
return

if info is None:
info = _("Press any key to continue...")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is here for translation, which must happen dynamically, not in the default.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOW IT MAKES SENSE!

This makes me feel terrible about it😅

@@ -227,6 +225,10 @@ def confirm(
"y/n" if default is None else ("Y/n" if default else "y/N"),
)

# Initialise rv to avoid a
# `variable might be referenced before assignment` warning
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't happen.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh?, I added that because I was seeing a warning in Pycharm

@@ -338,7 +338,7 @@ def __init__(
max_content_width = parent.max_content_width

#: The maximum width of formatted content (None implies a sensible
#: default which is 80 for most things).
#: default whih is 80 for most things).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo

@@ -17,6 +17,7 @@
from io import StringIO
from types import TracebackType

# Imports can be simplified to improved readability and avoid clutter.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This style is deliberate and enforced by our formatting configuration.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! Okay, thanks :)

@davidism
Copy link
Member

davidism commented Sep 6, 2024

Thanks for your interest. However, we generally don't accept general refactors or style changes from outside contributors.

Is there a reason for using as when the names are the same?

They indicate that the names are re-exported for type checkers and formatters. And the style used is deliberate, as enforced by our formatter.

@davidism davidism closed this Sep 6, 2024
@rly0nheart rly0nheart deleted the dev branch September 6, 2024 05:45
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants