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

Inspect continuation prompt signature and pass only viable arguments. #14274

Merged
merged 3 commits into from Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 20 additions & 3 deletions IPython/terminal/interactiveshell.py
Expand Up @@ -3,6 +3,7 @@
import asyncio
import os
import sys
import inspect
from warnings import warn
from typing import Union as UnionType, Optional

Expand Down Expand Up @@ -66,8 +67,8 @@
PTK3 = ptk_version.startswith('3.')


class _NoStyle(Style): pass

class _NoStyle(Style):
pass


_style_overrides_light_bg = {
Expand All @@ -84,6 +85,20 @@ class _NoStyle(Style): pass
Token.OutPromptNum: '#ansired bold',
}


def _backward_compat_continuation_prompt_tokens(method, width: int, *, lineno: int):
"""
Sagemath use custom prompt and we broke them in 8.19.
"""
sig = inspect.signature(method)
if "lineno" in inspect.signature(method).parameters or any(
[p.kind == p.VAR_KEYWORD for p in sig.parameters.values()]
):
return method(width, lineno=lineno)
else:
return method(width)


def get_default_editor():
try:
return os.environ['EDITOR']
Expand Down Expand Up @@ -764,7 +779,9 @@ def get_message():
"message": get_message,
"prompt_continuation": (
lambda width, lineno, is_soft_wrap: PygmentsTokens(
self.prompts.continuation_prompt_tokens(width, lineno=lineno)
_backward_compat_continuation_prompt_tokens(
self.prompts.continuation_prompt_tokens, width, lineno=lineno
)
)
),
"multiline": True,
Expand Down
8 changes: 3 additions & 5 deletions docs/source/config/details.rst
Expand Up @@ -33,8 +33,8 @@ which defines the defaults. The required interface is like this:
Prompt style definition. *shell* is a reference to the
:class:`~.TerminalInteractiveShell` instance.

.. method:: in_prompt_tokens(cli=None)
continuation_prompt_tokens(self, cli=None, width=None)
.. method:: in_prompt_tokens()
continuation_prompt_tokens(self, width=None)

Choose a reason for hiding this comment

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

While you are at it, maybe remove cli=None from the previous method, and the two lines 46-47 below.

rewrite_prompt_tokens()
out_prompt_tokens()

Expand All @@ -43,8 +43,6 @@ which defines the defaults. The required interface is like this:
For continuation prompts, *width* is an integer representing the width of
the prompt area in terminal columns.

*cli*, where used, is the prompt_toolkit ``CommandLineInterface`` instance.
This is mainly for compatibility with the API prompt_toolkit expects.

Here is an example Prompt class that will show the current working directory
in the input prompt:
Expand All @@ -55,7 +53,7 @@ in the input prompt:
import os

class MyPrompt(Prompts):
def in_prompt_tokens(self, cli=None):
def in_prompt_tokens(self):
return [(Token, os.getcwd()),
(Token.Prompt, ' >>>')]

Expand Down
8 changes: 4 additions & 4 deletions examples/Embedding/embed_class_long.py
Expand Up @@ -18,26 +18,26 @@
# %run example-embed.py)

from IPython.terminal.prompts import Prompts, Token
from traitlets.config.loader import Config

class CustomPrompt(Prompts):

def in_prompt_tokens(self, cli=None):
def in_prompt_tokens(self):

return [
return [
(Token.Prompt, 'In <'),
(Token.PromptNum, str(self.shell.execution_count)),
(Token.Prompt, '>: '),
]

def out_prompt_tokens(self):
return [
return [
(Token.OutPrompt, 'Out<'),
(Token.OutPromptNum, str(self.shell.execution_count)),
(Token.OutPrompt, '>: '),
]


from traitlets.config.loader import Config
try:
get_ipython
except NameError:
Expand Down
13 changes: 6 additions & 7 deletions examples/utils/cwd_prompt.py
Expand Up @@ -6,9 +6,12 @@

class MyPrompt(Prompts):

def in_prompt_tokens(self, cli=None):
return [(Token, os.getcwd()),
(Token.Prompt, '>>>')]
def in_prompt_tokens(self):
return [
(Token, os.getcwd()),
(Token.Prompt, ">>>")
]


def load_ipython_extension(shell):
new_prompts = MyPrompt(shell)
Expand All @@ -20,7 +23,3 @@ def unload_ipython_extension(shell):
print("cannot unload")
else:
shell.prompts = shell.prompts.old_prompts