From 2e985c25f996c6c6093101af41c31acb6afa81d0 Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Fri, 3 Nov 2023 14:47:23 +0000 Subject: [PATCH] Use ruff for linting and formatting. - Several fixes because of Ruff. - Updated GitHub workflow to use ruff instead of isort and black. - Added Python 3.10 to GitHub workflow. - Ruff formatting. - Removed unnecessary trailing comma. --- .github/workflows/test.yaml | 15 +- .travis.yml | 35 --- docs/conf.py | 1 - examples/full-screen/calculator.py | 4 +- examples/prompts/asyncio-prompt.py | 7 +- examples/prompts/custom-lexer.py | 2 +- examples/ssh/asyncssh-server.py | 1 - examples/telnet/hello-world.py | 2 +- examples/telnet/toolbar.py | 2 +- pyproject.toml | 51 +++-- src/prompt_toolkit/application/application.py | 2 +- src/prompt_toolkit/application/current.py | 3 - src/prompt_toolkit/auto_suggest.py | 6 +- src/prompt_toolkit/buffer.py | 5 +- src/prompt_toolkit/cache.py | 6 +- src/prompt_toolkit/clipboard/in_memory.py | 3 +- src/prompt_toolkit/completion/filesystem.py | 2 +- .../contrib/regular_languages/compiler.py | 8 +- src/prompt_toolkit/contrib/telnet/server.py | 2 +- src/prompt_toolkit/formatted_text/base.py | 2 +- .../key_binding/bindings/mouse.py | 215 +++++++++--------- .../key_binding/key_processor.py | 4 +- src/prompt_toolkit/layout/containers.py | 2 + src/prompt_toolkit/layout/controls.py | 4 +- src/prompt_toolkit/layout/layout.py | 3 +- src/prompt_toolkit/layout/menus.py | 4 +- src/prompt_toolkit/layout/mouse_handlers.py | 6 +- src/prompt_toolkit/layout/screen.py | 6 +- src/prompt_toolkit/lexers/pygments.py | 2 +- src/prompt_toolkit/output/win32.py | 15 +- src/prompt_toolkit/renderer.py | 5 +- src/prompt_toolkit/utils.py | 3 +- src/prompt_toolkit/widgets/base.py | 4 +- tests/test_filter.py | 2 - tests/test_shortcuts.py | 1 - 35 files changed, 203 insertions(+), 232 deletions(-) delete mode 100644 .travis.yml diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 5469afd1e3..511c14bac5 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -10,7 +10,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [3.7, 3.8, 3.9, "3.10"] + python-version: [3.7, 3.8, 3.9, "3.10", "3.11"] steps: - uses: actions/checkout@v2 @@ -22,21 +22,22 @@ jobs: run: | sudo apt remove python3-pip python -m pip install --upgrade pip - python -m pip install . black coverage codecov flake8 isort==5.6.4 mypy pytest readme_renderer types-contextvars asyncssh + python -m pip install . ruff coverage codecov mypy pytest readme_renderer types-contextvars asyncssh pip list - - name: Run Tests + - name: Ruff + run: | + ruff . + ruff format --check . + - name: Tests run: | - flake8 . coverage run -m pytest - - name: Type Checker + - name: Mypy # Check wheather the imports were sorted correctly. # When this fails, please run ./tools/sort-imports.sh run: | mypy --strict src/prompt_toolkit --platform win32 mypy --strict src/prompt_toolkit --platform linux mypy --strict src/prompt_toolkit --platform darwin - isort -c --profile black src examples tests setup.py - black --check src examples tests setup.py - name: Validate README.md # Ensure that the README renders correctly (required for uploading to PyPI). run: | diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 0091119f15..0000000000 --- a/.travis.yml +++ /dev/null @@ -1,35 +0,0 @@ -os: linux -dist: focal -cache: pip -language: python - -jobs: - include: - - python: 3.8 - - python: 3.7 - - python: 3.6 - -install: - - pip install . black coverage codecov flake8 isort mypy pytest readme_renderer - - pip list - -script: - - echo "$TRAVIS_PYTHON_VERSION" - - flake8 prompt_toolkit - - coverage run -m pytest - - # Run type checker. - - mypy prompt_toolkit - - # Check wheather the imports were sorted correctly. - # When this fails, please run ./tools/sort-imports.sh - - isort -c -rc --profile black prompt_toolkit examples tests setup.py - - - black --check prompt_toolkit examples tests setup.py - - # Ensure that the README renders correctly (required for uploading to PyPI). - - python -m readme_renderer README.rst > /dev/null - - -after_success: - - codecov diff --git a/docs/conf.py b/docs/conf.py index 550d097f5f..0b015ac46a 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -12,7 +12,6 @@ # serve to show the default. import os -import sys # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the diff --git a/examples/full-screen/calculator.py b/examples/full-screen/calculator.py index 4d54511552..f567801061 100755 --- a/examples/full-screen/calculator.py +++ b/examples/full-screen/calculator.py @@ -49,8 +49,8 @@ def main(): def accept(buff): # Evaluate "calculator" expression. try: - output = "\n\nIn: {}\nOut: {}".format( - input_field.text, eval(input_field.text) + output = ( + f"\n\nIn: {input_field.text}\nOut: {eval(input_field.text)}" ) # Don't do 'eval' in real code! except BaseException as e: output = f"\n\n{e}" diff --git a/examples/prompts/asyncio-prompt.py b/examples/prompts/asyncio-prompt.py index 6f94ed9513..32a1481d99 100755 --- a/examples/prompts/asyncio-prompt.py +++ b/examples/prompts/asyncio-prompt.py @@ -60,9 +60,4 @@ async def main(): if __name__ == "__main__": - try: - from asyncio import run - except ImportError: - asyncio.run_until_complete(main()) - else: - asyncio.run(main()) + asyncio.run(main()) diff --git a/examples/prompts/custom-lexer.py b/examples/prompts/custom-lexer.py index 1255d9f3f9..c4c9fbed2f 100755 --- a/examples/prompts/custom-lexer.py +++ b/examples/prompts/custom-lexer.py @@ -9,7 +9,7 @@ class RainbowLexer(Lexer): def lex_document(self, document): - colors = list(sorted(NAMED_COLORS, key=NAMED_COLORS.get)) + colors = sorted(NAMED_COLORS, key=NAMED_COLORS.get) def get_line(lineno): return [ diff --git a/examples/ssh/asyncssh-server.py b/examples/ssh/asyncssh-server.py index 3951528463..27d0dd22cc 100755 --- a/examples/ssh/asyncssh-server.py +++ b/examples/ssh/asyncssh-server.py @@ -4,7 +4,6 @@ """ import asyncio import logging -from asyncio import run import asyncssh from pygments.lexers.html import HtmlLexer diff --git a/examples/telnet/hello-world.py b/examples/telnet/hello-world.py index 68b6918afa..c19c60ca9a 100755 --- a/examples/telnet/hello-world.py +++ b/examples/telnet/hello-world.py @@ -7,7 +7,7 @@ That is probably the preferred way if you only need Python 3 support. """ import logging -from asyncio import Future, run +from asyncio import run from prompt_toolkit.contrib.telnet.server import TelnetServer from prompt_toolkit.shortcuts import PromptSession, clear diff --git a/examples/telnet/toolbar.py b/examples/telnet/toolbar.py index d73a4db116..d6ae88656a 100755 --- a/examples/telnet/toolbar.py +++ b/examples/telnet/toolbar.py @@ -4,7 +4,7 @@ in the prompt. """ import logging -from asyncio import Future, run +from asyncio import run from prompt_toolkit.completion import WordCompleter from prompt_toolkit.contrib.telnet.server import TelnetServer diff --git a/pyproject.toml b/pyproject.toml index c15b97511c..21de1915f3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,13 +1,38 @@ -[tool.black] -target-version = ['py36'] - - -[tool.isort] -# isort configuration that is compatible with Black. -multi_line_output = 3 -include_trailing_comma = true -known_first_party = "prompt_toolkit" -known_third_party = "asyncssh,pygments" -force_grid_wrap = 0 -use_parentheses = true -line_length = 88 +[tool.ruff] +target-version = "py37" +select = [ + "E", # pycodestyle errors + "W", # pycodestyle warnings + "F", # pyflakes + "C", # flake8-comprehensions + "T", # Print. + "I", # isort + # "B", # flake8-bugbear + "UP", # pyupgrade + "RUF100", # unused-noqa + "Q", # quotes +] +ignore = [ + "E501", # Line too long, handled by black + "C901", # Too complex + "E731", # Assign lambda. + "E402", # Module level import not at the top. + "E741", # Ambiguous variable name. +] + + +[tool.ruff.per-file-ignores] +"examples/*" = ["T201"] # Print allowed in examples. +"src/prompt_toolkit/application/application.py" = ["T100", "T201", "F821"] # pdb and print allowed. +"src/prompt_toolkit/contrib/telnet/server.py" = ["T201"] # Print allowed. +"src/prompt_toolkit/key_binding/bindings/named_commands.py" = ["T201"] # Print allowed. +"src/prompt_toolkit/shortcuts/progress_bar/base.py" = ["T201"] # Print allowed. +"tools/*" = ["T201"] # Print allowed. +"src/prompt_toolkit/filters/__init__.py" = ["F403", "F405"] # Possibly undefined due to star import. +"src/prompt_toolkit/filters/cli.py" = ["F403", "F405"] # Possibly undefined due to star import. +"src/prompt_toolkit/shortcuts/progress_bar/formatters.py" = ["UP031"] # %-style formatting. + + +[tool.ruff.isort] +known-first-party = ["prompt_toolkit"] +known-third-party = ["pygments", "asyncssh"] diff --git a/src/prompt_toolkit/application/application.py b/src/prompt_toolkit/application/application.py index bcc819095c..be799c0d21 100644 --- a/src/prompt_toolkit/application/application.py +++ b/src/prompt_toolkit/application/application.py @@ -655,7 +655,7 @@ async def run_async( # See: https://github.com/prompt-toolkit/python-prompt-toolkit/issues/1553 handle_sigint = False - async def _run_async(f: "asyncio.Future[_AppResult]") -> _AppResult: + async def _run_async(f: asyncio.Future[_AppResult]) -> _AppResult: context = contextvars.copy_context() self.context = context diff --git a/src/prompt_toolkit/application/current.py b/src/prompt_toolkit/application/current.py index 33828024f4..736cb064f4 100644 --- a/src/prompt_toolkit/application/current.py +++ b/src/prompt_toolkit/application/current.py @@ -1,6 +1,5 @@ from __future__ import annotations -import sys from contextlib import contextmanager from contextvars import ContextVar from typing import TYPE_CHECKING, Any, Generator @@ -148,8 +147,6 @@ def create_app_session( Like in the case of an Telnet/SSH server. This functionality uses contextvars and requires at least Python 3.7. """ - if sys.version_info <= (3, 6): - raise RuntimeError("Application sessions require Python 3.7.") # If no input/output is specified, fall back to the current input/output, # whatever that is. diff --git a/src/prompt_toolkit/auto_suggest.py b/src/prompt_toolkit/auto_suggest.py index 1049d1a451..98cb4ddd92 100644 --- a/src/prompt_toolkit/auto_suggest.py +++ b/src/prompt_toolkit/auto_suggest.py @@ -72,7 +72,7 @@ def get_suggestion(self, buffer: Buffer, document: Document) -> Suggestion | Non """ async def get_suggestion_async( - self, buff: "Buffer", document: Document + self, buff: Buffer, document: Document ) -> Suggestion | None: """ Return a :class:`.Future` which is set when the suggestions are ready. @@ -96,7 +96,7 @@ def get_suggestion(self, buff: Buffer, document: Document) -> Suggestion | None: return self.auto_suggest.get_suggestion(buff, document) async def get_suggestion_async( - self, buff: "Buffer", document: Document + self, buff: Buffer, document: Document ) -> Suggestion | None: """ Run the `get_suggestion` function in a thread. @@ -170,7 +170,7 @@ def get_suggestion(self, buff: Buffer, document: Document) -> Suggestion | None: return auto_suggest.get_suggestion(buff, document) async def get_suggestion_async( - self, buff: "Buffer", document: Document + self, buff: Buffer, document: Document ) -> Suggestion | None: auto_suggest = self.get_auto_suggest() or DummyAutoSuggest() return await auto_suggest.get_suggestion_async(buff, document) diff --git a/src/prompt_toolkit/buffer.py b/src/prompt_toolkit/buffer.py index 8edc01d1ef..444f6c1116 100644 --- a/src/prompt_toolkit/buffer.py +++ b/src/prompt_toolkit/buffer.py @@ -15,7 +15,7 @@ from collections import deque from enum import Enum from functools import wraps -from typing import Any, Awaitable, Callable, Coroutine, Deque, Iterable, TypeVar, cast +from typing import Any, Awaitable, Callable, Coroutine, Iterable, TypeVar, cast from .application.current import get_app from .application.run_in_terminal import run_in_terminal @@ -56,6 +56,7 @@ class EditReadOnlyBuffer(Exception): class ValidationState(Enum): "The validation state of a buffer. This is set after the validation." + VALID = "VALID" INVALID = "INVALID" UNKNOWN = "UNKNOWN" @@ -366,7 +367,7 @@ def reset( #: Ctrl-C should reset this, and copy the whole history back in here. #: Enter should process the current command and append to the real #: history. - self._working_lines: Deque[str] = deque([document.text]) + self._working_lines: deque[str] = deque([document.text]) self.__working_index = 0 def load_history_if_not_yet_loaded(self) -> None: diff --git a/src/prompt_toolkit/cache.py b/src/prompt_toolkit/cache.py index ada8d9856a..01dd1f79d6 100644 --- a/src/prompt_toolkit/cache.py +++ b/src/prompt_toolkit/cache.py @@ -2,7 +2,7 @@ from collections import deque from functools import wraps -from typing import Any, Callable, Deque, Dict, Generic, Hashable, Tuple, TypeVar, cast +from typing import Any, Callable, Dict, Generic, Hashable, Tuple, TypeVar, cast __all__ = [ "SimpleCache", @@ -26,7 +26,7 @@ def __init__(self, maxsize: int = 8) -> None: assert maxsize > 0 self._data: dict[_T, _U] = {} - self._keys: Deque[_T] = deque() + self._keys: deque[_T] = deque() self.maxsize: int = maxsize def get(self, key: _T, getter_func: Callable[[], _U]) -> _U: @@ -86,7 +86,7 @@ class FastDictCache(Dict[_K, _V]): def __init__(self, get_value: Callable[..., _V], size: int = 1000000) -> None: assert size > 0 - self._keys: Deque[_K] = deque() + self._keys: deque[_K] = deque() self.get_value = get_value self.size = size diff --git a/src/prompt_toolkit/clipboard/in_memory.py b/src/prompt_toolkit/clipboard/in_memory.py index d6e010e232..d9ae0817b7 100644 --- a/src/prompt_toolkit/clipboard/in_memory.py +++ b/src/prompt_toolkit/clipboard/in_memory.py @@ -1,7 +1,6 @@ from __future__ import annotations from collections import deque -from typing import Deque from .base import Clipboard, ClipboardData @@ -22,7 +21,7 @@ def __init__(self, data: ClipboardData | None = None, max_size: int = 60) -> Non assert max_size >= 1 self.max_size = max_size - self._ring: Deque[ClipboardData] = deque() + self._ring: deque[ClipboardData] = deque() if data is not None: self.set_data(data) diff --git a/src/prompt_toolkit/completion/filesystem.py b/src/prompt_toolkit/completion/filesystem.py index 10e2f7ca69..8e7f87e00b 100644 --- a/src/prompt_toolkit/completion/filesystem.py +++ b/src/prompt_toolkit/completion/filesystem.py @@ -115,4 +115,4 @@ def __init__(self) -> None: get_paths=lambda: os.environ.get("PATH", "").split(os.pathsep), file_filter=lambda name: os.access(name, os.X_OK), expanduser=True, - ), + ) diff --git a/src/prompt_toolkit/contrib/regular_languages/compiler.py b/src/prompt_toolkit/contrib/regular_languages/compiler.py index a56bc698fb..474f6cfd10 100644 --- a/src/prompt_toolkit/contrib/regular_languages/compiler.py +++ b/src/prompt_toolkit/contrib/regular_languages/compiler.py @@ -41,9 +41,8 @@ from __future__ import annotations import re -from typing import Callable, Dict, Iterable, Iterator +from typing import Callable, Dict, Iterable, Iterator, Pattern from typing import Match as RegexMatch -from typing import Pattern from .regex_parser import ( AnyNode, @@ -170,10 +169,7 @@ def transform(node: Node) -> str: # A `Variable` wraps the children into a named group. elif isinstance(node, Variable): - return "(?P<{}>{})".format( - create_group_func(node), - transform(node.childnode), - ) + return f"(?P<{create_group_func(node)}>{transform(node.childnode)})" # `Repeat`. elif isinstance(node, Repeat): diff --git a/src/prompt_toolkit/contrib/telnet/server.py b/src/prompt_toolkit/contrib/telnet/server.py index 5730a3b0bb..81c19f6c88 100644 --- a/src/prompt_toolkit/contrib/telnet/server.py +++ b/src/prompt_toolkit/contrib/telnet/server.py @@ -186,7 +186,7 @@ def handle_incoming_data() -> None: self.feed(data) else: # Connection closed by client. - logger.info("Connection closed by client. %r %r" % self.addr) + logger.info("Connection closed by client. {!r} {!r}".format(*self.addr)) self.close() # Add reader. diff --git a/src/prompt_toolkit/formatted_text/base.py b/src/prompt_toolkit/formatted_text/base.py index cec6debcfb..92de353577 100644 --- a/src/prompt_toolkit/formatted_text/base.py +++ b/src/prompt_toolkit/formatted_text/base.py @@ -86,7 +86,7 @@ def to_formatted_text( else: raise ValueError( "No formatted text. Expecting a unicode object, " - "HTML, ANSI or a FormattedText instance. Got %r" % (value,) + f"HTML, ANSI or a FormattedText instance. Got {value!r}" ) # Apply extra style. diff --git a/src/prompt_toolkit/key_binding/bindings/mouse.py b/src/prompt_toolkit/key_binding/bindings/mouse.py index af589aba77..cb426ce7bc 100644 --- a/src/prompt_toolkit/key_binding/bindings/mouse.py +++ b/src/prompt_toolkit/key_binding/bindings/mouse.py @@ -25,7 +25,6 @@ E = KeyPressEvent # fmt: off -# flake8: noqa E201 SCROLL_UP = MouseEventType.SCROLL_UP SCROLL_DOWN = MouseEventType.SCROLL_DOWN MOUSE_DOWN = MouseEventType.MOUSE_DOWN @@ -49,113 +48,113 @@ UNKNOWN_BUTTON = MouseButton.UNKNOWN xterm_sgr_mouse_events = { - ( 0, 'm') : (LEFT, MOUSE_UP, NO_MODIFIER), # left_up 0+ + + =0 - ( 4, 'm') : (LEFT, MOUSE_UP, SHIFT), # left_up Shift 0+4+ + =4 - ( 8, 'm') : (LEFT, MOUSE_UP, ALT), # left_up Alt 0+ +8+ =8 - (12, 'm') : (LEFT, MOUSE_UP, SHIFT_ALT), # left_up Shift Alt 0+4+8+ =12 - (16, 'm') : (LEFT, MOUSE_UP, CONTROL), # left_up Control 0+ + +16=16 - (20, 'm') : (LEFT, MOUSE_UP, SHIFT_CONTROL), # left_up Shift Control 0+4+ +16=20 - (24, 'm') : (LEFT, MOUSE_UP, ALT_CONTROL), # left_up Alt Control 0+ +8+16=24 - (28, 'm') : (LEFT, MOUSE_UP, SHIFT_ALT_CONTROL), # left_up Shift Alt Control 0+4+8+16=28 - - ( 1, 'm') : (MIDDLE, MOUSE_UP, NO_MODIFIER), # middle_up 1+ + + =1 - ( 5, 'm') : (MIDDLE, MOUSE_UP, SHIFT), # middle_up Shift 1+4+ + =5 - ( 9, 'm') : (MIDDLE, MOUSE_UP, ALT), # middle_up Alt 1+ +8+ =9 - (13, 'm') : (MIDDLE, MOUSE_UP, SHIFT_ALT), # middle_up Shift Alt 1+4+8+ =13 - (17, 'm') : (MIDDLE, MOUSE_UP, CONTROL), # middle_up Control 1+ + +16=17 - (21, 'm') : (MIDDLE, MOUSE_UP, SHIFT_CONTROL), # middle_up Shift Control 1+4+ +16=21 - (25, 'm') : (MIDDLE, MOUSE_UP, ALT_CONTROL), # middle_up Alt Control 1+ +8+16=25 - (29, 'm') : (MIDDLE, MOUSE_UP, SHIFT_ALT_CONTROL), # middle_up Shift Alt Control 1+4+8+16=29 - - ( 2, 'm') : (RIGHT, MOUSE_UP, NO_MODIFIER), # right_up 2+ + + =2 - ( 6, 'm') : (RIGHT, MOUSE_UP, SHIFT), # right_up Shift 2+4+ + =6 - (10, 'm') : (RIGHT, MOUSE_UP, ALT), # right_up Alt 2+ +8+ =10 - (14, 'm') : (RIGHT, MOUSE_UP, SHIFT_ALT), # right_up Shift Alt 2+4+8+ =14 - (18, 'm') : (RIGHT, MOUSE_UP, CONTROL), # right_up Control 2+ + +16=18 - (22, 'm') : (RIGHT, MOUSE_UP, SHIFT_CONTROL), # right_up Shift Control 2+4+ +16=22 - (26, 'm') : (RIGHT, MOUSE_UP, ALT_CONTROL), # right_up Alt Control 2+ +8+16=26 - (30, 'm') : (RIGHT, MOUSE_UP, SHIFT_ALT_CONTROL), # right_up Shift Alt Control 2+4+8+16=30 - - ( 0, 'M') : (LEFT, MOUSE_DOWN, NO_MODIFIER), # left_down 0+ + + =0 - ( 4, 'M') : (LEFT, MOUSE_DOWN, SHIFT), # left_down Shift 0+4+ + =4 - ( 8, 'M') : (LEFT, MOUSE_DOWN, ALT), # left_down Alt 0+ +8+ =8 - (12, 'M') : (LEFT, MOUSE_DOWN, SHIFT_ALT), # left_down Shift Alt 0+4+8+ =12 - (16, 'M') : (LEFT, MOUSE_DOWN, CONTROL), # left_down Control 0+ + +16=16 - (20, 'M') : (LEFT, MOUSE_DOWN, SHIFT_CONTROL), # left_down Shift Control 0+4+ +16=20 - (24, 'M') : (LEFT, MOUSE_DOWN, ALT_CONTROL), # left_down Alt Control 0+ +8+16=24 - (28, 'M') : (LEFT, MOUSE_DOWN, SHIFT_ALT_CONTROL), # left_down Shift Alt Control 0+4+8+16=28 - - ( 1, 'M') : (MIDDLE, MOUSE_DOWN, NO_MODIFIER), # middle_down 1+ + + =1 - ( 5, 'M') : (MIDDLE, MOUSE_DOWN, SHIFT), # middle_down Shift 1+4+ + =5 - ( 9, 'M') : (MIDDLE, MOUSE_DOWN, ALT), # middle_down Alt 1+ +8+ =9 - (13, 'M') : (MIDDLE, MOUSE_DOWN, SHIFT_ALT), # middle_down Shift Alt 1+4+8+ =13 - (17, 'M') : (MIDDLE, MOUSE_DOWN, CONTROL), # middle_down Control 1+ + +16=17 - (21, 'M') : (MIDDLE, MOUSE_DOWN, SHIFT_CONTROL), # middle_down Shift Control 1+4+ +16=21 - (25, 'M') : (MIDDLE, MOUSE_DOWN, ALT_CONTROL), # middle_down Alt Control 1+ +8+16=25 - (29, 'M') : (MIDDLE, MOUSE_DOWN, SHIFT_ALT_CONTROL), # middle_down Shift Alt Control 1+4+8+16=29 - - ( 2, 'M') : (RIGHT, MOUSE_DOWN, NO_MODIFIER), # right_down 2+ + + =2 - ( 6, 'M') : (RIGHT, MOUSE_DOWN, SHIFT), # right_down Shift 2+4+ + =6 - (10, 'M') : (RIGHT, MOUSE_DOWN, ALT), # right_down Alt 2+ +8+ =10 - (14, 'M') : (RIGHT, MOUSE_DOWN, SHIFT_ALT), # right_down Shift Alt 2+4+8+ =14 - (18, 'M') : (RIGHT, MOUSE_DOWN, CONTROL), # right_down Control 2+ + +16=18 - (22, 'M') : (RIGHT, MOUSE_DOWN, SHIFT_CONTROL), # right_down Shift Control 2+4+ +16=22 - (26, 'M') : (RIGHT, MOUSE_DOWN, ALT_CONTROL), # right_down Alt Control 2+ +8+16=26 - (30, 'M') : (RIGHT, MOUSE_DOWN, SHIFT_ALT_CONTROL), # right_down Shift Alt Control 2+4+8+16=30 - - (32, 'M') : (LEFT, MOUSE_MOVE, NO_MODIFIER), # left_drag 32+ + + =32 - (36, 'M') : (LEFT, MOUSE_MOVE, SHIFT), # left_drag Shift 32+4+ + =36 - (40, 'M') : (LEFT, MOUSE_MOVE, ALT), # left_drag Alt 32+ +8+ =40 - (44, 'M') : (LEFT, MOUSE_MOVE, SHIFT_ALT), # left_drag Shift Alt 32+4+8+ =44 - (48, 'M') : (LEFT, MOUSE_MOVE, CONTROL), # left_drag Control 32+ + +16=48 - (52, 'M') : (LEFT, MOUSE_MOVE, SHIFT_CONTROL), # left_drag Shift Control 32+4+ +16=52 - (56, 'M') : (LEFT, MOUSE_MOVE, ALT_CONTROL), # left_drag Alt Control 32+ +8+16=56 - (60, 'M') : (LEFT, MOUSE_MOVE, SHIFT_ALT_CONTROL), # left_drag Shift Alt Control 32+4+8+16=60 - - (33, 'M') : (MIDDLE, MOUSE_MOVE, NO_MODIFIER), # middle_drag 33+ + + =33 - (37, 'M') : (MIDDLE, MOUSE_MOVE, SHIFT), # middle_drag Shift 33+4+ + =37 - (41, 'M') : (MIDDLE, MOUSE_MOVE, ALT), # middle_drag Alt 33+ +8+ =41 - (45, 'M') : (MIDDLE, MOUSE_MOVE, SHIFT_ALT), # middle_drag Shift Alt 33+4+8+ =45 - (49, 'M') : (MIDDLE, MOUSE_MOVE, CONTROL), # middle_drag Control 33+ + +16=49 - (53, 'M') : (MIDDLE, MOUSE_MOVE, SHIFT_CONTROL), # middle_drag Shift Control 33+4+ +16=53 - (57, 'M') : (MIDDLE, MOUSE_MOVE, ALT_CONTROL), # middle_drag Alt Control 33+ +8+16=57 - (61, 'M') : (MIDDLE, MOUSE_MOVE, SHIFT_ALT_CONTROL), # middle_drag Shift Alt Control 33+4+8+16=61 - - (34, 'M') : (RIGHT, MOUSE_MOVE, NO_MODIFIER), # right_drag 34+ + + =34 - (38, 'M') : (RIGHT, MOUSE_MOVE, SHIFT), # right_drag Shift 34+4+ + =38 - (42, 'M') : (RIGHT, MOUSE_MOVE, ALT), # right_drag Alt 34+ +8+ =42 - (46, 'M') : (RIGHT, MOUSE_MOVE, SHIFT_ALT), # right_drag Shift Alt 34+4+8+ =46 - (50, 'M') : (RIGHT, MOUSE_MOVE, CONTROL), # right_drag Control 34+ + +16=50 - (54, 'M') : (RIGHT, MOUSE_MOVE, SHIFT_CONTROL), # right_drag Shift Control 34+4+ +16=54 - (58, 'M') : (RIGHT, MOUSE_MOVE, ALT_CONTROL), # right_drag Alt Control 34+ +8+16=58 - (62, 'M') : (RIGHT, MOUSE_MOVE, SHIFT_ALT_CONTROL), # right_drag Shift Alt Control 34+4+8+16=62 - - (35, 'M') : (NO_BUTTON, MOUSE_MOVE, NO_MODIFIER), # none_drag 35+ + + =35 - (39, 'M') : (NO_BUTTON, MOUSE_MOVE, SHIFT), # none_drag Shift 35+4+ + =39 - (43, 'M') : (NO_BUTTON, MOUSE_MOVE, ALT), # none_drag Alt 35+ +8+ =43 - (47, 'M') : (NO_BUTTON, MOUSE_MOVE, SHIFT_ALT), # none_drag Shift Alt 35+4+8+ =47 - (51, 'M') : (NO_BUTTON, MOUSE_MOVE, CONTROL), # none_drag Control 35+ + +16=51 - (55, 'M') : (NO_BUTTON, MOUSE_MOVE, SHIFT_CONTROL), # none_drag Shift Control 35+4+ +16=55 - (59, 'M') : (NO_BUTTON, MOUSE_MOVE, ALT_CONTROL), # none_drag Alt Control 35+ +8+16=59 - (63, 'M') : (NO_BUTTON, MOUSE_MOVE, SHIFT_ALT_CONTROL), # none_drag Shift Alt Control 35+4+8+16=63 - - (64, 'M') : (NO_BUTTON, SCROLL_UP, NO_MODIFIER), # scroll_up 64+ + + =64 - (68, 'M') : (NO_BUTTON, SCROLL_UP, SHIFT), # scroll_up Shift 64+4+ + =68 - (72, 'M') : (NO_BUTTON, SCROLL_UP, ALT), # scroll_up Alt 64+ +8+ =72 - (76, 'M') : (NO_BUTTON, SCROLL_UP, SHIFT_ALT), # scroll_up Shift Alt 64+4+8+ =76 - (80, 'M') : (NO_BUTTON, SCROLL_UP, CONTROL), # scroll_up Control 64+ + +16=80 - (84, 'M') : (NO_BUTTON, SCROLL_UP, SHIFT_CONTROL), # scroll_up Shift Control 64+4+ +16=84 - (88, 'M') : (NO_BUTTON, SCROLL_UP, ALT_CONTROL), # scroll_up Alt Control 64+ +8+16=88 - (92, 'M') : (NO_BUTTON, SCROLL_UP, SHIFT_ALT_CONTROL), # scroll_up Shift Alt Control 64+4+8+16=92 - - (65, 'M') : (NO_BUTTON, SCROLL_DOWN, NO_MODIFIER), # scroll_down 64+ + + =65 - (69, 'M') : (NO_BUTTON, SCROLL_DOWN, SHIFT), # scroll_down Shift 64+4+ + =69 - (73, 'M') : (NO_BUTTON, SCROLL_DOWN, ALT), # scroll_down Alt 64+ +8+ =73 - (77, 'M') : (NO_BUTTON, SCROLL_DOWN, SHIFT_ALT), # scroll_down Shift Alt 64+4+8+ =77 - (81, 'M') : (NO_BUTTON, SCROLL_DOWN, CONTROL), # scroll_down Control 64+ + +16=81 - (85, 'M') : (NO_BUTTON, SCROLL_DOWN, SHIFT_CONTROL), # scroll_down Shift Control 64+4+ +16=85 - (89, 'M') : (NO_BUTTON, SCROLL_DOWN, ALT_CONTROL), # scroll_down Alt Control 64+ +8+16=89 - (93, 'M') : (NO_BUTTON, SCROLL_DOWN, SHIFT_ALT_CONTROL), # scroll_down Shift Alt Control 64+4+8+16=93 + ( 0, "m") : (LEFT, MOUSE_UP, NO_MODIFIER), # left_up 0+ + + =0 + ( 4, "m") : (LEFT, MOUSE_UP, SHIFT), # left_up Shift 0+4+ + =4 + ( 8, "m") : (LEFT, MOUSE_UP, ALT), # left_up Alt 0+ +8+ =8 + (12, "m") : (LEFT, MOUSE_UP, SHIFT_ALT), # left_up Shift Alt 0+4+8+ =12 + (16, "m") : (LEFT, MOUSE_UP, CONTROL), # left_up Control 0+ + +16=16 + (20, "m") : (LEFT, MOUSE_UP, SHIFT_CONTROL), # left_up Shift Control 0+4+ +16=20 + (24, "m") : (LEFT, MOUSE_UP, ALT_CONTROL), # left_up Alt Control 0+ +8+16=24 + (28, "m") : (LEFT, MOUSE_UP, SHIFT_ALT_CONTROL), # left_up Shift Alt Control 0+4+8+16=28 + + ( 1, "m") : (MIDDLE, MOUSE_UP, NO_MODIFIER), # middle_up 1+ + + =1 + ( 5, "m") : (MIDDLE, MOUSE_UP, SHIFT), # middle_up Shift 1+4+ + =5 + ( 9, "m") : (MIDDLE, MOUSE_UP, ALT), # middle_up Alt 1+ +8+ =9 + (13, "m") : (MIDDLE, MOUSE_UP, SHIFT_ALT), # middle_up Shift Alt 1+4+8+ =13 + (17, "m") : (MIDDLE, MOUSE_UP, CONTROL), # middle_up Control 1+ + +16=17 + (21, "m") : (MIDDLE, MOUSE_UP, SHIFT_CONTROL), # middle_up Shift Control 1+4+ +16=21 + (25, "m") : (MIDDLE, MOUSE_UP, ALT_CONTROL), # middle_up Alt Control 1+ +8+16=25 + (29, "m") : (MIDDLE, MOUSE_UP, SHIFT_ALT_CONTROL), # middle_up Shift Alt Control 1+4+8+16=29 + + ( 2, "m") : (RIGHT, MOUSE_UP, NO_MODIFIER), # right_up 2+ + + =2 + ( 6, "m") : (RIGHT, MOUSE_UP, SHIFT), # right_up Shift 2+4+ + =6 + (10, "m") : (RIGHT, MOUSE_UP, ALT), # right_up Alt 2+ +8+ =10 + (14, "m") : (RIGHT, MOUSE_UP, SHIFT_ALT), # right_up Shift Alt 2+4+8+ =14 + (18, "m") : (RIGHT, MOUSE_UP, CONTROL), # right_up Control 2+ + +16=18 + (22, "m") : (RIGHT, MOUSE_UP, SHIFT_CONTROL), # right_up Shift Control 2+4+ +16=22 + (26, "m") : (RIGHT, MOUSE_UP, ALT_CONTROL), # right_up Alt Control 2+ +8+16=26 + (30, "m") : (RIGHT, MOUSE_UP, SHIFT_ALT_CONTROL), # right_up Shift Alt Control 2+4+8+16=30 + + ( 0, "M") : (LEFT, MOUSE_DOWN, NO_MODIFIER), # left_down 0+ + + =0 + ( 4, "M") : (LEFT, MOUSE_DOWN, SHIFT), # left_down Shift 0+4+ + =4 + ( 8, "M") : (LEFT, MOUSE_DOWN, ALT), # left_down Alt 0+ +8+ =8 + (12, "M") : (LEFT, MOUSE_DOWN, SHIFT_ALT), # left_down Shift Alt 0+4+8+ =12 + (16, "M") : (LEFT, MOUSE_DOWN, CONTROL), # left_down Control 0+ + +16=16 + (20, "M") : (LEFT, MOUSE_DOWN, SHIFT_CONTROL), # left_down Shift Control 0+4+ +16=20 + (24, "M") : (LEFT, MOUSE_DOWN, ALT_CONTROL), # left_down Alt Control 0+ +8+16=24 + (28, "M") : (LEFT, MOUSE_DOWN, SHIFT_ALT_CONTROL), # left_down Shift Alt Control 0+4+8+16=28 + + ( 1, "M") : (MIDDLE, MOUSE_DOWN, NO_MODIFIER), # middle_down 1+ + + =1 + ( 5, "M") : (MIDDLE, MOUSE_DOWN, SHIFT), # middle_down Shift 1+4+ + =5 + ( 9, "M") : (MIDDLE, MOUSE_DOWN, ALT), # middle_down Alt 1+ +8+ =9 + (13, "M") : (MIDDLE, MOUSE_DOWN, SHIFT_ALT), # middle_down Shift Alt 1+4+8+ =13 + (17, "M") : (MIDDLE, MOUSE_DOWN, CONTROL), # middle_down Control 1+ + +16=17 + (21, "M") : (MIDDLE, MOUSE_DOWN, SHIFT_CONTROL), # middle_down Shift Control 1+4+ +16=21 + (25, "M") : (MIDDLE, MOUSE_DOWN, ALT_CONTROL), # middle_down Alt Control 1+ +8+16=25 + (29, "M") : (MIDDLE, MOUSE_DOWN, SHIFT_ALT_CONTROL), # middle_down Shift Alt Control 1+4+8+16=29 + + ( 2, "M") : (RIGHT, MOUSE_DOWN, NO_MODIFIER), # right_down 2+ + + =2 + ( 6, "M") : (RIGHT, MOUSE_DOWN, SHIFT), # right_down Shift 2+4+ + =6 + (10, "M") : (RIGHT, MOUSE_DOWN, ALT), # right_down Alt 2+ +8+ =10 + (14, "M") : (RIGHT, MOUSE_DOWN, SHIFT_ALT), # right_down Shift Alt 2+4+8+ =14 + (18, "M") : (RIGHT, MOUSE_DOWN, CONTROL), # right_down Control 2+ + +16=18 + (22, "M") : (RIGHT, MOUSE_DOWN, SHIFT_CONTROL), # right_down Shift Control 2+4+ +16=22 + (26, "M") : (RIGHT, MOUSE_DOWN, ALT_CONTROL), # right_down Alt Control 2+ +8+16=26 + (30, "M") : (RIGHT, MOUSE_DOWN, SHIFT_ALT_CONTROL), # right_down Shift Alt Control 2+4+8+16=30 + + (32, "M") : (LEFT, MOUSE_MOVE, NO_MODIFIER), # left_drag 32+ + + =32 + (36, "M") : (LEFT, MOUSE_MOVE, SHIFT), # left_drag Shift 32+4+ + =36 + (40, "M") : (LEFT, MOUSE_MOVE, ALT), # left_drag Alt 32+ +8+ =40 + (44, "M") : (LEFT, MOUSE_MOVE, SHIFT_ALT), # left_drag Shift Alt 32+4+8+ =44 + (48, "M") : (LEFT, MOUSE_MOVE, CONTROL), # left_drag Control 32+ + +16=48 + (52, "M") : (LEFT, MOUSE_MOVE, SHIFT_CONTROL), # left_drag Shift Control 32+4+ +16=52 + (56, "M") : (LEFT, MOUSE_MOVE, ALT_CONTROL), # left_drag Alt Control 32+ +8+16=56 + (60, "M") : (LEFT, MOUSE_MOVE, SHIFT_ALT_CONTROL), # left_drag Shift Alt Control 32+4+8+16=60 + + (33, "M") : (MIDDLE, MOUSE_MOVE, NO_MODIFIER), # middle_drag 33+ + + =33 + (37, "M") : (MIDDLE, MOUSE_MOVE, SHIFT), # middle_drag Shift 33+4+ + =37 + (41, "M") : (MIDDLE, MOUSE_MOVE, ALT), # middle_drag Alt 33+ +8+ =41 + (45, "M") : (MIDDLE, MOUSE_MOVE, SHIFT_ALT), # middle_drag Shift Alt 33+4+8+ =45 + (49, "M") : (MIDDLE, MOUSE_MOVE, CONTROL), # middle_drag Control 33+ + +16=49 + (53, "M") : (MIDDLE, MOUSE_MOVE, SHIFT_CONTROL), # middle_drag Shift Control 33+4+ +16=53 + (57, "M") : (MIDDLE, MOUSE_MOVE, ALT_CONTROL), # middle_drag Alt Control 33+ +8+16=57 + (61, "M") : (MIDDLE, MOUSE_MOVE, SHIFT_ALT_CONTROL), # middle_drag Shift Alt Control 33+4+8+16=61 + + (34, "M") : (RIGHT, MOUSE_MOVE, NO_MODIFIER), # right_drag 34+ + + =34 + (38, "M") : (RIGHT, MOUSE_MOVE, SHIFT), # right_drag Shift 34+4+ + =38 + (42, "M") : (RIGHT, MOUSE_MOVE, ALT), # right_drag Alt 34+ +8+ =42 + (46, "M") : (RIGHT, MOUSE_MOVE, SHIFT_ALT), # right_drag Shift Alt 34+4+8+ =46 + (50, "M") : (RIGHT, MOUSE_MOVE, CONTROL), # right_drag Control 34+ + +16=50 + (54, "M") : (RIGHT, MOUSE_MOVE, SHIFT_CONTROL), # right_drag Shift Control 34+4+ +16=54 + (58, "M") : (RIGHT, MOUSE_MOVE, ALT_CONTROL), # right_drag Alt Control 34+ +8+16=58 + (62, "M") : (RIGHT, MOUSE_MOVE, SHIFT_ALT_CONTROL), # right_drag Shift Alt Control 34+4+8+16=62 + + (35, "M") : (NO_BUTTON, MOUSE_MOVE, NO_MODIFIER), # none_drag 35+ + + =35 + (39, "M") : (NO_BUTTON, MOUSE_MOVE, SHIFT), # none_drag Shift 35+4+ + =39 + (43, "M") : (NO_BUTTON, MOUSE_MOVE, ALT), # none_drag Alt 35+ +8+ =43 + (47, "M") : (NO_BUTTON, MOUSE_MOVE, SHIFT_ALT), # none_drag Shift Alt 35+4+8+ =47 + (51, "M") : (NO_BUTTON, MOUSE_MOVE, CONTROL), # none_drag Control 35+ + +16=51 + (55, "M") : (NO_BUTTON, MOUSE_MOVE, SHIFT_CONTROL), # none_drag Shift Control 35+4+ +16=55 + (59, "M") : (NO_BUTTON, MOUSE_MOVE, ALT_CONTROL), # none_drag Alt Control 35+ +8+16=59 + (63, "M") : (NO_BUTTON, MOUSE_MOVE, SHIFT_ALT_CONTROL), # none_drag Shift Alt Control 35+4+8+16=63 + + (64, "M") : (NO_BUTTON, SCROLL_UP, NO_MODIFIER), # scroll_up 64+ + + =64 + (68, "M") : (NO_BUTTON, SCROLL_UP, SHIFT), # scroll_up Shift 64+4+ + =68 + (72, "M") : (NO_BUTTON, SCROLL_UP, ALT), # scroll_up Alt 64+ +8+ =72 + (76, "M") : (NO_BUTTON, SCROLL_UP, SHIFT_ALT), # scroll_up Shift Alt 64+4+8+ =76 + (80, "M") : (NO_BUTTON, SCROLL_UP, CONTROL), # scroll_up Control 64+ + +16=80 + (84, "M") : (NO_BUTTON, SCROLL_UP, SHIFT_CONTROL), # scroll_up Shift Control 64+4+ +16=84 + (88, "M") : (NO_BUTTON, SCROLL_UP, ALT_CONTROL), # scroll_up Alt Control 64+ +8+16=88 + (92, "M") : (NO_BUTTON, SCROLL_UP, SHIFT_ALT_CONTROL), # scroll_up Shift Alt Control 64+4+8+16=92 + + (65, "M") : (NO_BUTTON, SCROLL_DOWN, NO_MODIFIER), # scroll_down 64+ + + =65 + (69, "M") : (NO_BUTTON, SCROLL_DOWN, SHIFT), # scroll_down Shift 64+4+ + =69 + (73, "M") : (NO_BUTTON, SCROLL_DOWN, ALT), # scroll_down Alt 64+ +8+ =73 + (77, "M") : (NO_BUTTON, SCROLL_DOWN, SHIFT_ALT), # scroll_down Shift Alt 64+4+8+ =77 + (81, "M") : (NO_BUTTON, SCROLL_DOWN, CONTROL), # scroll_down Control 64+ + +16=81 + (85, "M") : (NO_BUTTON, SCROLL_DOWN, SHIFT_CONTROL), # scroll_down Shift Control 64+4+ +16=85 + (89, "M") : (NO_BUTTON, SCROLL_DOWN, ALT_CONTROL), # scroll_down Alt Control 64+ +8+16=89 + (93, "M") : (NO_BUTTON, SCROLL_DOWN, SHIFT_ALT_CONTROL), # scroll_down Shift Alt Control 64+4+8+16=93 } typical_mouse_events = { diff --git a/src/prompt_toolkit/key_binding/key_processor.py b/src/prompt_toolkit/key_binding/key_processor.py index edf27573ee..4c4f0d155d 100644 --- a/src/prompt_toolkit/key_binding/key_processor.py +++ b/src/prompt_toolkit/key_binding/key_processor.py @@ -10,7 +10,7 @@ import weakref from asyncio import Task, sleep from collections import deque -from typing import TYPE_CHECKING, Any, Deque, Generator +from typing import TYPE_CHECKING, Any, Generator from prompt_toolkit.application.current import get_app from prompt_toolkit.enums import EditingMode @@ -103,7 +103,7 @@ def reset(self) -> None: self._previous_handler: Binding | None = None # The queue of keys not yet send to our _process generator/state machine. - self.input_queue: Deque[KeyPress] = deque() + self.input_queue: deque[KeyPress] = deque() # The key buffer that is matched in the generator state machine. # (This is at at most the amount of keys that make up for one key binding.) diff --git a/src/prompt_toolkit/layout/containers.py b/src/prompt_toolkit/layout/containers.py index bcbf35d056..5de87b08a7 100644 --- a/src/prompt_toolkit/layout/containers.py +++ b/src/prompt_toolkit/layout/containers.py @@ -172,6 +172,7 @@ def _window_too_small() -> Window: class VerticalAlign(Enum): "Alignment for `HSplit`." + TOP = "TOP" CENTER = "CENTER" BOTTOM = "BOTTOM" @@ -180,6 +181,7 @@ class VerticalAlign(Enum): class HorizontalAlign(Enum): "Alignment for `VSplit`." + LEFT = "LEFT" CENTER = "CENTER" RIGHT = "RIGHT" diff --git a/src/prompt_toolkit/layout/controls.py b/src/prompt_toolkit/layout/controls.py index cb036cc842..647df3d63c 100644 --- a/src/prompt_toolkit/layout/controls.py +++ b/src/prompt_toolkit/layout/controls.py @@ -477,9 +477,7 @@ def create_content(self, width: int, height: int) -> UIContent: def get_line(i: int) -> StyleAndTextTuples: return [] - return UIContent( - get_line=get_line, line_count=100**100 - ) # Something very big. + return UIContent(get_line=get_line, line_count=100**100) # Something very big. def is_focusable(self) -> bool: return False diff --git a/src/prompt_toolkit/layout/layout.py b/src/prompt_toolkit/layout/layout.py index 093e69ed18..a5e7a80e75 100644 --- a/src/prompt_toolkit/layout/layout.py +++ b/src/prompt_toolkit/layout/layout.py @@ -133,8 +133,7 @@ def focus(self, value: FocusableElement) -> None: # This is a `Window`: focus that. if value not in self.find_all_windows(): raise ValueError( - "Invalid value. Window does not appear in the layout: %r" - % (value,) + f"Invalid value. Window does not appear in the layout: {value!r}" ) self.current_window = value diff --git a/src/prompt_toolkit/layout/menus.py b/src/prompt_toolkit/layout/menus.py index 7879e3abc6..1a21237a84 100644 --- a/src/prompt_toolkit/layout/menus.py +++ b/src/prompt_toolkit/layout/menus.py @@ -2,7 +2,7 @@ import math from itertools import zip_longest -from typing import TYPE_CHECKING, Callable, Iterable, Sequence, Tuple, TypeVar, cast +from typing import TYPE_CHECKING, Callable, Iterable, Sequence, TypeVar, cast from weakref import WeakKeyDictionary from prompt_toolkit.application.current import get_app @@ -332,7 +332,7 @@ def __init__(self, min_rows: int = 3, suggested_max_column_width: int = 30) -> N # the count, because a completer can add new completions to the # `CompletionState` while loading.) self._column_width_for_completion_state: WeakKeyDictionary[ - CompletionState, Tuple[int, int] + CompletionState, tuple[int, int] ] = WeakKeyDictionary() # Info of last rendering. diff --git a/src/prompt_toolkit/layout/mouse_handlers.py b/src/prompt_toolkit/layout/mouse_handlers.py index 2faf99e980..56a4eddd9d 100644 --- a/src/prompt_toolkit/layout/mouse_handlers.py +++ b/src/prompt_toolkit/layout/mouse_handlers.py @@ -1,7 +1,7 @@ from __future__ import annotations from collections import defaultdict -from typing import TYPE_CHECKING, Callable, DefaultDict +from typing import TYPE_CHECKING, Callable from prompt_toolkit.mouse_events import MouseEvent @@ -34,8 +34,8 @@ def dummy_callback(mouse_event: MouseEvent) -> NotImplementedOrNone: # over the mouse handlers of the visible region in the scrollable pane. # Map y (row) to x (column) to handlers. - self.mouse_handlers: DefaultDict[ - int, DefaultDict[int, MouseHandler] + self.mouse_handlers: defaultdict[ + int, defaultdict[int, MouseHandler] ] = defaultdict(lambda: defaultdict(lambda: dummy_callback)) def set_mouse_handler_for_range( diff --git a/src/prompt_toolkit/layout/screen.py b/src/prompt_toolkit/layout/screen.py index 17330283d5..49aebbd626 100644 --- a/src/prompt_toolkit/layout/screen.py +++ b/src/prompt_toolkit/layout/screen.py @@ -1,7 +1,7 @@ from __future__ import annotations from collections import defaultdict -from typing import TYPE_CHECKING, Callable, DefaultDict +from typing import TYPE_CHECKING, Callable from prompt_toolkit.cache import FastDictCache from prompt_toolkit.data_structures import Point @@ -163,12 +163,12 @@ def __init__( else: default_char2 = default_char - self.data_buffer: DefaultDict[int, DefaultDict[int, Char]] = defaultdict( + self.data_buffer: defaultdict[int, defaultdict[int, Char]] = defaultdict( lambda: defaultdict(lambda: default_char2) ) #: Escape sequences to be injected. - self.zero_width_escapes: DefaultDict[int, DefaultDict[int, str]] = defaultdict( + self.zero_width_escapes: defaultdict[int, defaultdict[int, str]] = defaultdict( lambda: defaultdict(lambda: "") ) diff --git a/src/prompt_toolkit/lexers/pygments.py b/src/prompt_toolkit/lexers/pygments.py index 72630cecac..16ed29a287 100644 --- a/src/prompt_toolkit/lexers/pygments.py +++ b/src/prompt_toolkit/lexers/pygments.py @@ -114,7 +114,7 @@ def from_pygments_lexer_cls(cls, lexer_cls: PygmentsLexerCls) -> RegexSync: # For HTML, start at any open/close tag definition. "HTML": r"<[/a-zA-Z]", # For javascript, start at a function. - "JavaScript": r"\bfunction\b" + "JavaScript": r"\bfunction\b", # TODO: Add definitions for other languages. # By default, we start at every possible line. } diff --git a/src/prompt_toolkit/output/win32.py b/src/prompt_toolkit/output/win32.py index c93ec7de3c..edeca0968d 100644 --- a/src/prompt_toolkit/output/win32.py +++ b/src/prompt_toolkit/output/win32.py @@ -6,14 +6,6 @@ import os from ctypes import ArgumentError, byref, c_char, c_long, c_uint, c_ulong, pointer - -from ..utils import SPHINX_AUTODOC_RUNNING - -# Do not import win32-specific stuff when generating documentation. -# Otherwise RTD would be unable to generate docs for this module. -if not SPHINX_AUTODOC_RUNNING: - from ctypes import windll - from ctypes.wintypes import DWORD, HANDLE from typing import Callable, TextIO, TypeVar @@ -29,9 +21,16 @@ STD_OUTPUT_HANDLE, ) +from ..utils import SPHINX_AUTODOC_RUNNING from .base import Output from .color_depth import ColorDepth +# Do not import win32-specific stuff when generating documentation. +# Otherwise RTD would be unable to generate docs for this module. +if not SPHINX_AUTODOC_RUNNING: + from ctypes import windll + + __all__ = [ "Win32Output", ] diff --git a/src/prompt_toolkit/renderer.py b/src/prompt_toolkit/renderer.py index 99018f1685..7a3b88a608 100644 --- a/src/prompt_toolkit/renderer.py +++ b/src/prompt_toolkit/renderer.py @@ -7,7 +7,7 @@ from asyncio import FIRST_COMPLETED, Future, ensure_future, sleep, wait from collections import deque from enum import Enum -from typing import TYPE_CHECKING, Any, Callable, Deque, Dict, Hashable +from typing import TYPE_CHECKING, Any, Callable, Dict, Hashable from prompt_toolkit.application.current import get_app from prompt_toolkit.cursor_shapes import CursorShape @@ -319,6 +319,7 @@ def __missing__(self, style_str: str) -> bool: class CPR_Support(Enum): "Enum: whether or not CPR is supported." + SUPPORTED = "SUPPORTED" NOT_SUPPORTED = "NOT_SUPPORTED" UNKNOWN = "UNKNOWN" @@ -357,7 +358,7 @@ def __init__( self._cursor_key_mode_reset = False # Future set when we are waiting for a CPR flag. - self._waiting_for_cpr_futures: Deque[Future[None]] = deque() + self._waiting_for_cpr_futures: deque[Future[None]] = deque() self.cpr_support = CPR_Support.UNKNOWN if not output.responds_to_cpr: diff --git a/src/prompt_toolkit/utils.py b/src/prompt_toolkit/utils.py index 8cd9fae100..1a99a2868a 100644 --- a/src/prompt_toolkit/utils.py +++ b/src/prompt_toolkit/utils.py @@ -8,7 +8,6 @@ from typing import ( Callable, ContextManager, - Deque, Dict, Generator, Generic, @@ -136,7 +135,7 @@ class _CharSizesCache(Dict[str, int]): def __init__(self) -> None: super().__init__() # Keep track of the "long" strings in this cache. - self._long_strings: Deque[str] = deque() + self._long_strings: deque[str] = deque() def __missing__(self, string: str) -> int: # Note: We use the `max(0, ...` because some non printable control diff --git a/src/prompt_toolkit/widgets/base.py b/src/prompt_toolkit/widgets/base.py index cddb91596d..f36a545755 100644 --- a/src/prompt_toolkit/widgets/base.py +++ b/src/prompt_toolkit/widgets/base.py @@ -58,9 +58,8 @@ FormattedTextControl, GetLinePrefixCallable, ) -from prompt_toolkit.layout.dimension import AnyDimension +from prompt_toolkit.layout.dimension import AnyDimension, to_dimension from prompt_toolkit.layout.dimension import Dimension as D -from prompt_toolkit.layout.dimension import to_dimension from prompt_toolkit.layout.margins import ( ConditionalMargin, NumberedMargin, @@ -100,6 +99,7 @@ class Border: "Box drawing characters. (Thin)" + HORIZONTAL = "\u2500" VERTICAL = "\u2502" TOP_LEFT = "\u250c" diff --git a/tests/test_filter.py b/tests/test_filter.py index 9f133969e8..f7184c286f 100644 --- a/tests/test_filter.py +++ b/tests/test_filter.py @@ -1,7 +1,5 @@ from __future__ import annotations -import gc - import pytest from prompt_toolkit.filters import Always, Condition, Filter, Never, to_filter diff --git a/tests/test_shortcuts.py b/tests/test_shortcuts.py index 2c0a84b67a..287c6d33a6 100644 --- a/tests/test_shortcuts.py +++ b/tests/test_shortcuts.py @@ -2,7 +2,6 @@ from prompt_toolkit.shortcuts import print_container from prompt_toolkit.shortcuts.prompt import _split_multiline_prompt -from prompt_toolkit.shortcuts.utils import print_container from prompt_toolkit.widgets import Frame, TextArea