From e9a96bedd395423f6486967e99f5b4980f50e831 2021-12-11 00:06:06 From: Matthias Bussonnier Date: 2021-12-11 00:06:06 Subject: [PATCH] Merge pull request #13397 from Carreau/enable-black-terminal enable formatting by default --- diff --git a/IPython/core/tests/test_debugger.py b/IPython/core/tests/test_debugger.py index 75ce9fd..dae299e 100644 --- a/IPython/core/tests/test_debugger.py +++ b/IPython/core/tests/test_debugger.py @@ -8,6 +8,7 @@ import bdb import builtins import os import sys +import platform from tempfile import NamedTemporaryFile from textwrap import dedent @@ -16,6 +17,7 @@ from unittest.mock import patch from IPython.core import debugger from IPython.testing import IPYTHON_TESTING_TIMEOUT_SCALE from IPython.testing.decorators import skip_win32 +import pytest #----------------------------------------------------------------------------- # Helper classes, from CPython's Pdb test suite @@ -411,6 +413,7 @@ def test_decorator_skip(): child.close() +@pytest.mark.skip(platform.python_implementation() == "PyPy", reason="issues on PyPy") @skip_win32 def test_decorator_skip_disabled(): """test that decorator frame skipping can be disabled""" @@ -438,6 +441,7 @@ def test_decorator_skip_disabled(): child.close() +@pytest.mark.skip(platform.python_implementation() == "PyPy", reason="issues on PyPy") @skip_win32 def test_decorator_skip_with_breakpoint(): """test that decorator frame skipping can be disabled""" diff --git a/IPython/terminal/interactiveshell.py b/IPython/terminal/interactiveshell.py index 2d1d593..99e3c7e 100644 --- a/IPython/terminal/interactiveshell.py +++ b/IPython/terminal/interactiveshell.py @@ -102,10 +102,16 @@ else: _use_simple_prompt = ('IPY_TEST_SIMPLE_PROMPT' in os.environ) or (not _is_tty) def black_reformat_handler(text_before_cursor): + """ + We do not need to protect against error, + this is taken care at a higher level where any reformat error is ignored. + Indeed we may call reformatting on incomplete code. + """ import black + formatted_text = black.format_str(text_before_cursor, mode=black.FileMode()) - if not text_before_cursor.endswith('\n') and formatted_text.endswith('\n'): - formatted_text = formatted_text[:-1] + if not text_before_cursor.endswith("\n") and formatted_text.endswith("\n"): + formatted_text = formatted_text[:-1] return formatted_text @@ -176,7 +182,8 @@ class TerminalInteractiveShell(InteractiveShell): sequence to complete.""", ).tag(config=True) - autoformatter = Unicode(None, + autoformatter = Unicode( + "black", help="Autoformatter to reformat Terminal code. Can be `'black'` or `None`", allow_none=True ).tag(config=True) @@ -210,9 +217,7 @@ class TerminalInteractiveShell(InteractiveShell): if self.pt_app: self.pt_app.editing_mode = getattr(EditingMode, change.new.upper()) - @observe('autoformatter') - def _autoformatter_changed(self, change): - formatter = change.new + def _set_formatter(self, formatter): if formatter is None: self.reformat_handler = lambda x:x elif formatter == 'black': @@ -220,6 +225,11 @@ class TerminalInteractiveShell(InteractiveShell): else: raise ValueError + @observe("autoformatter") + def _autoformatter_changed(self, change): + formatter = change.new + self._set_formatter(formatter) + @observe('highlighting_style') @observe('colors') def _highlighting_style_changed(self, change): @@ -561,6 +571,7 @@ class TerminalInteractiveShell(InteractiveShell): self.init_prompt_toolkit_cli() self.init_term_title() self.keep_running = True + self._set_formatter(self.autoformatter) def ask_exit(self): diff --git a/setup.cfg b/setup.cfg index 61fa022..f0ba6ce 100644 --- a/setup.cfg +++ b/setup.cfg @@ -31,6 +31,7 @@ zip_safe = False install_requires = setuptools>=18.5 jedi>=0.16 + black decorator pickleshare traitlets>=5