-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add type annotations for instance attributes in exceptions
#3453
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
Open
jorenham
wants to merge
3
commits into
pallets:stable
Choose a base branch
from
jorenham:typing/exceptions/coverage
base: stable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+47
−14
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| .. currentmodule:: click | ||
|
|
||
| Version 8.4.dev | ||
| Version 8.4.1 | ||
| ------------------ | ||
|
|
||
| Unreleased | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,13 +36,16 @@ class ClickException(Exception): | |
| """An exception that Click can handle and show to the user.""" | ||
|
|
||
| #: The exit code for this exception. | ||
| exit_code = 1 | ||
| exit_code: t.ClassVar[int] = 1 | ||
|
|
||
| show_color: t.Final[bool | None] | ||
| message: t.Final[str] | ||
|
|
||
| def __init__(self, message: str) -> None: | ||
| super().__init__(message) | ||
| # The context will be removed by the time we print the message, so cache | ||
| # the color settings here to be used later on (in `show`) | ||
| self.show_color: bool | None = resolve_color_default() | ||
| self.show_color = resolve_color_default() | ||
| self.message = message | ||
|
|
||
| def format_message(self) -> str: | ||
|
|
@@ -71,12 +74,15 @@ class UsageError(ClickException): | |
| fill in the context automatically in some situations. | ||
| """ | ||
|
|
||
| exit_code = 2 | ||
| exit_code: t.ClassVar[int] = 2 | ||
|
|
||
| ctx: Context | None | ||
| cmd: t.Final[Command | None] | ||
|
|
||
| def __init__(self, message: str, ctx: Context | None = None) -> None: | ||
| super().__init__(message) | ||
| self.ctx = ctx | ||
| self.cmd: Command | None = self.ctx.command if self.ctx else None | ||
| self.cmd = self.ctx.command if self.ctx else None | ||
|
|
||
| def show(self, file: t.IO[t.Any] | None = None) -> None: | ||
| if file is None: | ||
|
|
@@ -123,6 +129,9 @@ class BadParameter(UsageError): | |
| each item is quoted and separated. | ||
| """ | ||
|
|
||
| param: Parameter | None | ||
| param_hint: cabc.Sequence[str] | str | None | ||
|
|
||
| def __init__( | ||
| self, | ||
| message: str, | ||
|
|
@@ -159,6 +168,8 @@ class MissingParameter(BadParameter): | |
| ``'option'`` or ``'argument'``. | ||
| """ | ||
|
|
||
| param_type: t.Final[str | None] | ||
|
|
||
| def __init__( | ||
| self, | ||
| message: str | None = None, | ||
|
|
@@ -224,6 +235,9 @@ class NoSuchOption(UsageError): | |
| .. versionadded:: 4.0 | ||
| """ | ||
|
|
||
| option_name: t.Final[str] | ||
| possibilities: t.Final[list[str] | None] | ||
|
|
||
| def __init__( | ||
| self, | ||
| option_name: str, | ||
|
|
@@ -236,11 +250,15 @@ def __init__( | |
|
|
||
| super().__init__(message, ctx) | ||
| self.option_name = option_name | ||
| self.possibilities: list[str] | None = None | ||
|
|
||
| # this temp variable avoids mypy complaining self.possibilities "reassignment" | ||
| if possibilities: | ||
| from difflib import get_close_matches | ||
|
|
||
| self.possibilities = get_close_matches(option_name, possibilities) | ||
| possibilities_ = get_close_matches(option_name, possibilities) | ||
| else: | ||
| possibilities_ = None | ||
| self.possibilities = possibilities_ | ||
|
|
||
| def format_message(self) -> str: | ||
| if not self.possibilities: | ||
|
|
@@ -251,6 +269,9 @@ def format_message(self) -> str: | |
| class NoSuchCommand(UsageError): | ||
| """Raised if Click attempted to handle a command that does not exist.""" | ||
|
|
||
| command_name: t.Final[str] | ||
| possibilities: t.Final[list[str] | None] | ||
|
|
||
| def __init__( | ||
| self, | ||
| command_name: str, | ||
|
|
@@ -263,11 +284,15 @@ def __init__( | |
|
|
||
| super().__init__(message, ctx) | ||
| self.command_name = command_name | ||
| self.possibilities: list[str] | None = None | ||
|
|
||
| # this temp variable avoids mypy complaining self.possibilities "reassignment" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as other comment. |
||
| if possibilities: | ||
| from difflib import get_close_matches | ||
|
|
||
| self.possibilities = get_close_matches(command_name, possibilities) | ||
| possibilities_ = get_close_matches(command_name, possibilities) | ||
| else: | ||
| possibilities_ = None | ||
| self.possibilities = possibilities_ | ||
|
|
||
| def format_message(self) -> str: | ||
| if not self.possibilities: | ||
|
|
@@ -285,6 +310,8 @@ class BadOptionUsage(UsageError): | |
| :param option_name: the name of the option being used incorrectly. | ||
| """ | ||
|
|
||
| option_name: t.Final[str] | ||
|
|
||
| def __init__( | ||
| self, option_name: str, message: str, ctx: Context | None = None | ||
| ) -> None: | ||
|
|
@@ -302,8 +329,9 @@ class BadArgumentUsage(UsageError): | |
|
|
||
|
|
||
| class NoArgsIsHelpError(UsageError): | ||
| ctx: Context | ||
|
|
||
| def __init__(self, ctx: Context) -> None: | ||
| self.ctx: Context | ||
| super().__init__(ctx.get_help(), ctx=ctx) | ||
|
|
||
| def show(self, file: t.IO[t.Any] | None = None) -> None: | ||
|
|
@@ -313,12 +341,15 @@ def show(self, file: t.IO[t.Any] | None = None) -> None: | |
| class FileError(ClickException): | ||
| """Raised if a file cannot be opened.""" | ||
|
|
||
| ui_filename: t.Final[str] | ||
| filename: t.Final[str] | ||
|
|
||
| def __init__(self, filename: str, hint: str | None = None) -> None: | ||
| if hint is None: | ||
| hint = _("unknown error") | ||
|
|
||
| super().__init__(hint) | ||
| self.ui_filename: str = format_filename(filename) | ||
| self.ui_filename = format_filename(filename) | ||
| self.filename = filename | ||
|
|
||
| def format_message(self) -> str: | ||
|
|
@@ -340,5 +371,7 @@ class Exit(RuntimeError): | |
|
|
||
| __slots__ = ("exit_code",) | ||
|
|
||
| exit_code: t.Final[int] | ||
|
|
||
| def __init__(self, code: int = 0) -> None: | ||
| self.exit_code: int = code | ||
| self.exit_code = code | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fine to remove this.