Repository: Textualize/rich
Pull request #2538 (2022-09-22 06:56:50)

fix default value of ensure_ascii to match comments
## Type of changes

- [x] Bug fix
- [ ] New feature
- [ ] Documentation / docstrings
- [ ] Tests
- [ ] Other

## Checklist

- [x] I've run the latest [black](https://github.com/psf/black) with default args on new code.
- [x] I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate.
- [x] I've added tests for new code.
- [x] I accept that @willmcgugan may be pedantic in the code review.

## Description

Make the behaviour of `ensure_ascii` parameter match the comments and the documents.

The default value of `ensure_ascii` is supposed to be **False**, but it is **True** in the master branch, according to the comments or the documents, like:

https://github.com/Textualize/rich/blob/bfea226da7baddd3c5e31c9f0c0d2f2ca45e4c7c/rich/__init__.py#L84

and

https://github.com/Textualize/rich/blob/bfea226da7baddd3c5e31c9f0c0d2f2ca45e4c7c/rich/__init__.py#L98

---

I am not sure of the original preference, but I think the escaping result may be confusing since it looks like an additional unexpected process.

Thus I change the default value to **False**. 


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a4fb679e..eb2c1a1c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -22,8 +22,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - Fix NO_COLOR support on legacy Windows https://github.com/Textualize/rich/pull/2458
 - Fix pretty printer handling of cyclic references https://github.com/Textualize/rich/pull/2524
 - Fix missing `mode` property on file wrapper breaking uploads via `requests` https://github.com/Textualize/rich/pull/2495
+- Fix mismatching default value of parameter `ensure_ascii` https://github.com/Textualize/rich/pull/2538
 - Remove unused height parameter in `Layout` class https://github.com/Textualize/rich/pull/2540
 
+
 ### Changed
 
 - Removed border from code blocks in Markdown
diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md
index 1972229a..155fb61b 100644
--- a/CONTRIBUTORS.md
+++ b/CONTRIBUTORS.md
@@ -55,3 +55,4 @@ The following people have contributed to the development of Rich:
 - [Motahhar Mokfi](https://github.com/motahhar)
 - [Tomer Shalev](https://github.com/tomers)
 - [Serkan UYSAL](https://github.com/uysalserkan)
+- [Zhe Huang](https://github.com/onlyacat)
\ No newline at end of file
diff --git a/rich/__init__.py b/rich/__init__.py
index 6658707f..b631d544 100644
--- a/rich/__init__.py
+++ b/rich/__init__.py
@@ -81,7 +81,7 @@ def print_json(
     indent: Union[None, int, str] = 2,
     highlight: bool = True,
     skip_keys: bool = False,
-    ensure_ascii: bool = True,
+    ensure_ascii: bool = False,
     check_circular: bool = True,
     allow_nan: bool = True,
     default: Optional[Callable[[Any], Any]] = None,
diff --git a/rich/console.py b/rich/console.py
index 974a4e31..90034913 100644
--- a/rich/console.py
+++ b/rich/console.py
@@ -1722,7 +1722,7 @@ class Console:
         indent: Union[None, int, str] = 2,
         highlight: bool = True,
         skip_keys: bool = False,
-        ensure_ascii: bool = True,
+        ensure_ascii: bool = False,
         check_circular: bool = True,
         allow_nan: bool = True,
         default: Optional[Callable[[Any], Any]] = None,
diff --git a/rich/json.py b/rich/json.py
index 1fc115a9..ed9ca1ea 100644
--- a/rich/json.py
+++ b/rich/json.py
@@ -27,7 +27,7 @@ class JSON:
         indent: Union[None, int, str] = 2,
         highlight: bool = True,
         skip_keys: bool = False,
-        ensure_ascii: bool = True,
+        ensure_ascii: bool = False,
         check_circular: bool = True,
         allow_nan: bool = True,
         default: Optional[Callable[[Any], Any]] = None,
@@ -56,7 +56,7 @@ class JSON:
         indent: Union[None, int, str] = 2,
         highlight: bool = True,
         skip_keys: bool = False,
-        ensure_ascii: bool = True,
+        ensure_ascii: bool = False,
         check_circular: bool = True,
         allow_nan: bool = True,
         default: Optional[Callable[[Any], Any]] = None,


Test patch:
diff --git a/tests/test_console.py b/tests/test_console.py
index 369f0a67..4509f6b2 100644
--- a/tests/test_console.py
+++ b/tests/test_console.py
@@ -240,6 +240,15 @@ def test_print_json_ensure_ascii():
     assert result == expected
 
 
+def test_print_json_with_default_ensure_ascii():
+    console = Console(file=io.StringIO(), color_system="truecolor")
+    console.print_json(data={"foo": "💩"})
+    result = console.file.getvalue()
+    print(repr(result))
+    expected = '\x1b[1m{\x1b[0m\n  \x1b[1;34m"foo"\x1b[0m: \x1b[32m"💩"\x1b[0m\n\x1b[1m}\x1b[0m\n'
+    assert result == expected
+
+
 def test_print_json_indent_none():
     console = Console(file=io.StringIO(), color_system="truecolor")
     data = {"name": "apple", "count": 1}