Skip to content

Commit

Permalink
Use original text to compute pep8 lines #181
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Feb 18, 2019
1 parent d262e33 commit b78d3db
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
9 changes: 5 additions & 4 deletions jupytext/cell_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def __init__(self, fmt=None, default_language=None):
self.default_language = default_language or _SCRIPT_EXTENSIONS.get(self.ext, {}).get('language', 'python')
self.comment_magics = fmt['comment_magics'] if 'comment_magics' in fmt else self.default_comment_magics
self.metadata = None
self.org_content = []
self.content = []
self.explicit_eoc = None
self.cell_type = None
Expand Down Expand Up @@ -124,11 +125,8 @@ def read(self, lines):
if not self.metadata:
self.metadata = {}

org_lines = self.content
if self.ext == '.py' and self.cell_type != 'code' and self.content:
org_lines = ['#'] # cell was originally commented
if self.ext == '.py' and not self.explicit_eoc:
expected_blank_lines = pep8_lines_between_cells(org_lines or [''], lines[pos_next_cell:], self.ext)
expected_blank_lines = pep8_lines_between_cells(self.org_content or [''], lines[pos_next_cell:], self.ext)
else:
expected_blank_lines = 1

Expand Down Expand Up @@ -215,6 +213,7 @@ def find_cell_content(self, lines):

# Cell content
source = lines[cell_start:cell_end_marker]
self.org_content = [line for line in source]

# Exactly two empty lines at the end of cell (caused by PEP8)?
if self.ext == '.py' and self.explicit_eoc:
Expand Down Expand Up @@ -467,6 +466,7 @@ def find_cell_content(self, lines):

# Cell content
source = lines[cell_start:cell_end_marker]
self.org_content = [line for line in source]

if self.cell_type != 'code' or (self.metadata and not is_active('py', self.metadata)) \
or (self.language is not None and self.language != self.default_language):
Expand Down Expand Up @@ -623,6 +623,7 @@ def find_cell_content(self, lines):

# Cell content
source = lines[cell_start:cell_end_marker]
self.org_content = [line for line in source]

if self.cell_type == 'code' and self.comment_magics:
uncomment_magic(source, self.language or self.default_language)
Expand Down
2 changes: 2 additions & 0 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ def test_apply_black_on_python_notebooks(nb_file, tmpdir):

nb1 = readf(nb_file)
nb2 = readf(tmp_ipynb)
nb3 = readf(tmp_py)

assert len(nb1.cells) == len(nb2.cells)
assert len(nb1.cells) == len(nb3.cells)
for c1, c2 in zip(nb1.cells, nb2.cells):
# same content (almost)
assert black_invariant(c1.source) == black_invariant(c2.source)
Expand Down
16 changes: 16 additions & 0 deletions tests/test_read_simple_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,22 @@ def test_notebook_with_magic_and_bash_cells(script="""# This is a test for issue
compare(script, script2)


def test_notebook_no_line_to_next_cell(nb=new_notebook(
cells=[new_markdown_cell('Markdown cell #1'),
new_code_cell('%load_ext line_profiler'),
new_markdown_cell('Markdown cell #2'),
new_code_cell('%lprun -f ...'),
new_markdown_cell('Markdown cell #3'),
new_code_cell('# And a function!\n'
'def f(x):\n'
' return 5')])):
script = jupytext.writes(nb, 'py')
nb2 = jupytext.reads(script, 'py')
nb2.metadata.pop('jupytext')

compare(nb, nb2)


def test_notebook_one_blank_line_before_first_markdown_cell(script="""
# This is a markdown cell
Expand Down

0 comments on commit b78d3db

Please sign in to comment.