Skip to content

Commit

Permalink
Don't add a UTF-8 header - this is the default encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Feb 9, 2022
1 parent f2c0245 commit 2d06889
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
3 changes: 1 addition & 2 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ Jupytext ChangeLog

**Added**
- Added Haskell as supported language ([#909](https://github.com/mwouts/jupytext/issues/909)) - thanks to [codeweber](https://github.com/codeweber) for this contribution
- We added an example that document how one needs to reload the notebook to update the metadata (e.g. the encoding) when the text version has changed (#907)

**Changed**
- We have updated the pre-commit hooks and in particular we switched to the first stable version of `black==22.1.0`.
- We require `pandoc==2.16.2` for testing. The representation for code cells changed from ` ``` {.python}` to ` ``` python` in that version of Pandoc ([#906](https://github.com/mwouts/jupytext/issues/906)). We don't use `pandoc>=2.17` in tests at the moment because of the introduction of cell ids that cannot be filtered.

- Jupytext will not add anymore a UTF-8 encoding on Python scripts when the notebook contains non-ascii characters ([#907](https://github.com/mwouts/jupytext/issues/907))

1.13.6 (2022-01-11)
-------------------
Expand Down
8 changes: 6 additions & 2 deletions jupytext/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
from nbformat.v4.nbbase import new_raw_cell
from yaml.representer import SafeRepresenter

from .languages import _SCRIPT_EXTENSIONS, comment_lines
from .languages import (
_SCRIPT_EXTENSIONS,
comment_lines,
default_language_from_metadata_and_ext,
)
from .metadata_filter import _DEFAULT_NOTEBOOK_METADATA, filter_metadata
from .pep8 import pep8_lines_between_cells
from .version import __version__
Expand Down Expand Up @@ -58,7 +62,7 @@ def encoding_and_executable(notebook, metadata, ext):
if comment is not None:
if "encoding" in jupytext_metadata:
lines.append(jupytext_metadata.pop("encoding"))
else:
elif default_language_from_metadata_and_ext(metadata, ext) != "python":
for cell in notebook.cells:
try:
cell.source.encode("ascii")
Expand Down
33 changes: 32 additions & 1 deletion tests/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,47 @@ def test_write_non_ascii(tmpdir):
jupytext.write(nb, str(tmpdir.join("notebook.ipynb")))


def test_no_encoding_in_python_scripts(no_jupytext_version_number):
"""No UTF encoding should not be added to Python scripts"""
nb = new_notebook(
cells=[new_markdown_cell("α")],
metadata={
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3",
},
},
)

# Saving to and reading from py creates an encoding
py_light = jupytext.writes(nb, "py:light")
compare(
py_light,
"""# ---
# jupyter:
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# α
""",
)


def test_encoding_in_scripts_only(no_jupytext_version_number):
"""UTF encoding should not be added to markdown files"""
nb = new_notebook(
cells=[new_markdown_cell("α")],
metadata={
"encoding": "# -*- coding: utf-8 -*-",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3",
}
},
},
)

Expand Down

0 comments on commit 2d06889

Please sign in to comment.