From c933ce5da967625bf5c8f1b4e61105a259adae0d 2020-05-04 23:50:54 From: Matthias Bussonnier Date: 2020-05-04 23:50:54 Subject: [PATCH] Fix a couple of edge cases, and update tests Mostly when the match is super-short. Ass type annotations, run mypy on ptutils now. --- diff --git a/.travis.yml b/.travis.yml index 62615ed..20a966a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -38,7 +38,7 @@ install: - pip install setuptools --upgrade - pip install -e file://$PWD#egg=ipython[test] --upgrade - pip install trio curio --upgrade --upgrade-strategy eager - - pip install pytest 'matplotlib !=3.2.0' + - pip install pytest 'matplotlib !=3.2.0' mypy - pip install codecov check-manifest --upgrade script: @@ -50,6 +50,7 @@ script: fi - cd /tmp && iptest --coverage xml && cd - - pytest IPython + - mypy --ignore-missing-imports -m IPython.terminal.ptutils # On the latest Python (on Linux) only, make sure that the docs build. - | if [[ "$TRAVIS_PYTHON_VERSION" == "3.7" ]] && [[ "$TRAVIS_OS_NAME" == "linux" ]]; then diff --git a/IPython/terminal/ptutils.py b/IPython/terminal/ptutils.py index 7c19e95..ed7ad45 100644 --- a/IPython/terminal/ptutils.py +++ b/IPython/terminal/ptutils.py @@ -23,7 +23,7 @@ import os _completion_sentinel = object() -def _elide_point(string, *, min_elide=30): +def _elide_point(string:str, *, min_elide=30)->str: """ If a string is long enough, and has at least 3 dots, replace the middle part with ellipses. @@ -53,7 +53,7 @@ def _elide_point(string, *, min_elide=30): return string -def _elide_typed(string, typed, *, min_elide=30): +def _elide_typed(string:str, typed:str, *, min_elide:int=30)->str: """ Elide the middle of a long string if the beginning has already been typed. """ @@ -61,11 +61,13 @@ def _elide_typed(string, typed, *, min_elide=30): if len(string) < min_elide: return string cut_how_much = len(typed)-3 + if cut_how_much < 7: + return string if string.startswith(typed) and len(string)> len(typed): return f"{string[:3]}\N{HORIZONTAL ELLIPSIS}{string[cut_how_much:]}" return string -def _elide(string, typed, min_elide=30): +def _elide(string:str, typed:str, min_elide=30)->str: return _elide_typed( _elide_point(string, min_elide=min_elide), typed, min_elide=min_elide) diff --git a/IPython/terminal/tests/test_interactivshell.py b/IPython/terminal/tests/test_interactivshell.py index 6bacc8e..640e5d4 100644 --- a/IPython/terminal/tests/test_interactivshell.py +++ b/IPython/terminal/tests/test_interactivshell.py @@ -17,14 +17,32 @@ import nose.tools as nt class TestElide(unittest.TestCase): def test_elide(self): - _elide('concatenate((a1, a2, ...), axis') # do not raise - _elide('concatenate((a1, a2, ..), . axis') # do not raise - nt.assert_equal(_elide('aaaa.bbbb.ccccc.dddddd.eeeee.fffff.gggggg.hhhhhh'), 'aaaa.b…g.hhhhhh') - + _elide('concatenate((a1, a2, ...), axis', '') # do not raise + _elide('concatenate((a1, a2, ..), . axis', '') # do not raise + nt.assert_equal(_elide('aaaa.bbbb.ccccc.dddddd.eeeee.fffff.gggggg.hhhhhh',''), 'aaaa.b…g.hhhhhh') + test_string = os.sep.join(['', 10*'a', 10*'b', 10*'c', '']) expect_stirng = os.sep + 'a' + '\N{HORIZONTAL ELLIPSIS}' + 'b' + os.sep + 10*'c' - nt.assert_equal(_elide(test_string), expect_stirng) - + nt.assert_equal(_elide(test_string, ''), expect_stirng) + + def test_elide_typed_normal(self): + nt.assert_equal(_elide('the quick brown fox jumped over the lazy dog', 'the quick brown fox', min_elide=10), 'the…fox jumped over the lazy dog') + + + def test_elide_typed_short_match(self): + """ + if the match is too short we don't elide. + avoid the "the...the" + """ + nt.assert_equal(_elide('the quick brown fox jumped over the lazy dog', 'the', min_elide=10), 'the quick brown fox jumped over the lazy dog') + + def test_elide_typed_no_match(self): + """ + if the match is too short we don't elide. + avoid the "the...the" + """ + # here we typed red instead of brown + nt.assert_equal(_elide('the quick brown fox jumped over the lazy dog', 'the quick red fox', min_elide=10), 'the quick brown fox jumped over the lazy dog') class TestContextAwareCompletion(unittest.TestCase):