|
50 | 50 | from collections.abc import Callable, Iterable, Mapping |
51 | 51 | from contextlib import ( |
52 | 52 | redirect_stdout, |
| 53 | + suppress, |
53 | 54 | ) |
54 | 55 | from types import ( |
55 | 56 | FrameType, |
@@ -689,13 +690,13 @@ def register_command_set(self, cmdset: CommandSet) -> None: |
689 | 690 | if self.always_prefix_settables: |
690 | 691 | if not cmdset.settable_prefix.strip(): |
691 | 692 | raise CommandSetRegistrationError('CommandSet settable prefix must not be empty') |
692 | | - for key in cmdset.settables.keys(): |
| 693 | + for key in cmdset.settables: |
693 | 694 | prefixed_name = f'{cmdset.settable_prefix}.{key}' |
694 | 695 | if prefixed_name in all_settables: |
695 | 696 | raise CommandSetRegistrationError(f'Duplicate settable: {key}') |
696 | 697 |
|
697 | 698 | else: |
698 | | - for key in cmdset.settables.keys(): |
| 699 | + for key in cmdset.settables: |
699 | 700 | if key in all_settables: |
700 | 701 | raise CommandSetRegistrationError(f'Duplicate settable {key} is already registered') |
701 | 702 |
|
@@ -1098,7 +1099,7 @@ def add_settable(self, settable: Settable) -> None: |
1098 | 1099 | :param settable: Settable object being added |
1099 | 1100 | """ |
1100 | 1101 | if not self.always_prefix_settables: |
1101 | | - if settable.name in self.settables.keys() and settable.name not in self._settables.keys(): |
| 1102 | + if settable.name in self.settables and settable.name not in self._settables: |
1102 | 1103 | raise KeyError(f'Duplicate settable: {settable.name}') |
1103 | 1104 | self._settables[settable.name] = settable |
1104 | 1105 |
|
@@ -1594,10 +1595,7 @@ def index_based_complete( |
1594 | 1595 |
|
1595 | 1596 | # Check if token is at an index in the dictionary |
1596 | 1597 | match_against: Optional[Union[Iterable[str], CompleterFunc]] |
1597 | | - if index in index_dict: |
1598 | | - match_against = index_dict[index] |
1599 | | - else: |
1600 | | - match_against = all_else |
| 1598 | + match_against = index_dict.get(index, all_else) |
1601 | 1599 |
|
1602 | 1600 | # Perform tab completion using a Iterable |
1603 | 1601 | if isinstance(match_against, Iterable): |
@@ -1739,10 +1737,7 @@ def complete_users() -> list[str]: |
1739 | 1737 |
|
1740 | 1738 | # Remove cwd if it was added to match the text readline expects |
1741 | 1739 | if cwd_added: |
1742 | | - if cwd == os.path.sep: |
1743 | | - to_replace = cwd |
1744 | | - else: |
1745 | | - to_replace = cwd + os.path.sep |
| 1740 | + to_replace = cwd if cwd == os.path.sep else cwd + os.path.sep |
1746 | 1741 | matches = [cur_path.replace(to_replace, '', 1) for cur_path in matches] |
1747 | 1742 |
|
1748 | 1743 | # Restore the tilde string if we expanded one to match the text readline expects |
@@ -1962,10 +1957,7 @@ def _display_matches_pyreadline(self, matches: list[str]) -> None: # pragma: no |
1962 | 1957 | # Otherwise use pyreadline3's formatter |
1963 | 1958 | else: |
1964 | 1959 | # Check if we should show display_matches |
1965 | | - if self.display_matches: |
1966 | | - matches_to_display = self.display_matches |
1967 | | - else: |
1968 | | - matches_to_display = matches |
| 1960 | + matches_to_display = self.display_matches if self.display_matches else matches |
1969 | 1961 |
|
1970 | 1962 | # Add padding for visual appeal |
1971 | 1963 | matches_to_display, _ = self._pad_matches_to_display(matches_to_display) |
@@ -2154,10 +2146,7 @@ def _perform_completion( |
2154 | 2146 |
|
2155 | 2147 | if add_quote: |
2156 | 2148 | # Figure out what kind of quote to add and save it as the unclosed_quote |
2157 | | - if any('"' in match for match in self.completion_matches): |
2158 | | - completion_token_quote = "'" |
2159 | | - else: |
2160 | | - completion_token_quote = '"' |
| 2149 | + completion_token_quote = "'" if any('"' in match for match in self.completion_matches) else '"' |
2161 | 2150 |
|
2162 | 2151 | self.completion_matches = [completion_token_quote + match for match in self.completion_matches] |
2163 | 2152 |
|
@@ -2762,7 +2751,7 @@ def _input_line_to_statement(self, line: str, *, orig_rl_history_length: Optiona |
2762 | 2751 | orig_rl_history_length = None |
2763 | 2752 |
|
2764 | 2753 | # Check if this command matches a macro and wasn't already processed to avoid an infinite loop |
2765 | | - if statement.command in self.macros.keys() and statement.command not in used_macros: |
| 2754 | + if statement.command in self.macros and statement.command not in used_macros: |
2766 | 2755 | used_macros.append(statement.command) |
2767 | 2756 | resolve_result = self._resolve_macro(statement) |
2768 | 2757 | if resolve_result is None: |
@@ -2795,7 +2784,7 @@ def _resolve_macro(self, statement: Statement) -> Optional[str]: |
2795 | 2784 | :param statement: the parsed statement from the command line |
2796 | 2785 | :return: the resolved macro or None on error |
2797 | 2786 | """ |
2798 | | - if statement.command not in self.macros.keys(): |
| 2787 | + if statement.command not in self.macros: |
2799 | 2788 | raise KeyError(f"{statement.command} is not a macro") |
2800 | 2789 |
|
2801 | 2790 | macro = self.macros[statement.command] |
@@ -2886,10 +2875,8 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState: |
2886 | 2875 | # like: !ls -l | grep user | wc -l > out.txt. But this makes it difficult to know if the pipe process |
2887 | 2876 | # started OK, since the shell itself always starts. Therefore, we will wait a short time and check |
2888 | 2877 | # if the pipe process is still running. |
2889 | | - try: |
| 2878 | + with suppress(subprocess.TimeoutExpired): |
2890 | 2879 | proc.wait(0.2) |
2891 | | - except subprocess.TimeoutExpired: |
2892 | | - pass |
2893 | 2880 |
|
2894 | 2881 | # Check if the pipe process already exited |
2895 | 2882 | if proc.returncode is not None: |
@@ -3462,10 +3449,7 @@ def _alias_list(self, args: argparse.Namespace) -> None: |
3462 | 3449 | tokens_to_quote = constants.REDIRECTION_TOKENS |
3463 | 3450 | tokens_to_quote.extend(self.statement_parser.terminators) |
3464 | 3451 |
|
3465 | | - if args.names: |
3466 | | - to_list = utils.remove_duplicates(args.names) |
3467 | | - else: |
3468 | | - to_list = sorted(self.aliases, key=self.default_sort_key) |
| 3452 | + to_list = utils.remove_duplicates(args.names) if args.names else sorted(self.aliases, key=self.default_sort_key) |
3469 | 3453 |
|
3470 | 3454 | not_found: list[str] = [] |
3471 | 3455 | for name in to_list: |
@@ -3697,10 +3681,7 @@ def _macro_list(self, args: argparse.Namespace) -> None: |
3697 | 3681 | tokens_to_quote = constants.REDIRECTION_TOKENS |
3698 | 3682 | tokens_to_quote.extend(self.statement_parser.terminators) |
3699 | 3683 |
|
3700 | | - if args.names: |
3701 | | - to_list = utils.remove_duplicates(args.names) |
3702 | | - else: |
3703 | | - to_list = sorted(self.macros, key=self.default_sort_key) |
| 3684 | + to_list = utils.remove_duplicates(args.names) if args.names else sorted(self.macros, key=self.default_sort_key) |
3704 | 3685 |
|
3705 | 3686 | not_found: list[str] = [] |
3706 | 3687 | for name in to_list: |
@@ -3865,10 +3846,7 @@ def columnize(self, str_list: Optional[list[str]], display_width: int = 80) -> N |
3865 | 3846 | texts = [] |
3866 | 3847 | for col in range(ncols): |
3867 | 3848 | i = row + nrows * col |
3868 | | - if i >= size: |
3869 | | - x = "" |
3870 | | - else: |
3871 | | - x = str_list[i] |
| 3849 | + x = "" if i >= size else str_list[i] |
3872 | 3850 | texts.append(x) |
3873 | 3851 | while texts and not texts[-1]: |
3874 | 3852 | del texts[-1] |
@@ -4267,10 +4245,8 @@ def _reset_py_display() -> None: |
4267 | 4245 | # Delete any prompts that have been set |
4268 | 4246 | attributes = ['ps1', 'ps2', 'ps3'] |
4269 | 4247 | for cur_attr in attributes: |
4270 | | - try: |
| 4248 | + with suppress(KeyError): |
4271 | 4249 | del sys.__dict__[cur_attr] |
4272 | | - except KeyError: |
4273 | | - pass |
4274 | 4250 |
|
4275 | 4251 | # Reset functions |
4276 | 4252 | sys.displayhook = sys.__displayhook__ |
@@ -4982,10 +4958,7 @@ def _generate_transcript( |
4982 | 4958 | self.perror(f"Error saving transcript file '{transcript_path}': {ex}") |
4983 | 4959 | else: |
4984 | 4960 | # and let the user know what we did |
4985 | | - if commands_run == 1: |
4986 | | - plural = 'command and its output' |
4987 | | - else: |
4988 | | - plural = 'commands and their outputs' |
| 4961 | + plural = 'command and its output' if commands_run == 1 else 'commands and their outputs' |
4989 | 4962 | self.pfeedback(f"{commands_run} {plural} saved to transcript file '{transcript_path}'") |
4990 | 4963 | self.last_result = True |
4991 | 4964 |
|
|
0 commit comments