| 1 |
import collections |
| 2 |
import io |
| 3 |
import sys |
| 4 |
from array import array |
| 5 |
from collections import UserDict, defaultdict, deque |
| 6 |
from dataclasses import dataclass, field |
| 7 |
from typing import Any, List, NamedTuple |
| 8 |
|
| 9 |
import attr |
| 10 |
import pytest |
| 11 |
|
| 12 |
from rich.console import Console |
| 13 |
from rich.measure import Measurement |
| 14 |
from rich.pretty import Node, Pretty, _ipy_display_hook, install, pprint, pretty_repr |
| 15 |
from rich.text import Text |
| 16 |
|
| 17 |
skip_py38 = pytest.mark.skipif( |
| 18 |
sys.version_info.minor == 8 and sys.version_info.major == 3, |
| 19 |
reason="rendered differently on py3.8", |
| 20 |
) |
| 21 |
skip_py39 = pytest.mark.skipif( |
| 22 |
sys.version_info.minor == 9 and sys.version_info.major == 3, |
| 23 |
reason="rendered differently on py3.9", |
| 24 |
) |
| 25 |
skip_py310 = pytest.mark.skipif( |
| 26 |
sys.version_info.minor == 10 and sys.version_info.major == 3, |
| 27 |
reason="rendered differently on py3.10", |
| 28 |
) |
| 29 |
skip_py311 = pytest.mark.skipif( |
| 30 |
sys.version_info.minor == 11 and sys.version_info.major == 3, |
| 31 |
reason="rendered differently on py3.11", |
| 32 |
) |
| 33 |
skip_py312 = pytest.mark.skipif( |
| 34 |
sys.version_info.minor == 12 and sys.version_info.major == 3, |
| 35 |
reason="rendered differently on py3.12", |
| 36 |
) |
| 37 |
skip_py313 = pytest.mark.skipif( |
| 38 |
sys.version_info.minor == 13 and sys.version_info.major == 3, |
| 39 |
reason="rendered differently on py3.13", |
| 40 |
) |
| 41 |
skip_py314 = pytest.mark.skipif( |
| 42 |
sys.version_info.minor == 14 and sys.version_info.major == 3, |
| 43 |
reason="rendered differently on py3.14", |
| 44 |
) |
| •45 |
skip_py315 = pytest.mark.skipif( |
| 46 |
sys.version_info.minor == 15 and sys.version_info.major == 3, |
| 47 |
reason="rendered differently on py3.15", |
| 48 |
) |
| 49 |
|
| 50 |
|
| 51 |
def test_install() -> None: |
| 52 |
console = Console(file=io.StringIO()) |
| 53 |
dh = sys.displayhook |
| 54 |
install(console) |
| 55 |
sys.displayhook("foo") |
| 56 |
assert console.file.getvalue() == "'foo'\n" |
| 57 |
assert sys.displayhook is not dh |
| 58 |
|
| 59 |
|
| 60 |
def test_install_max_depth() -> None: |
| 61 |
console = Console(file=io.StringIO()) |
| 62 |
dh = sys.displayhook |
| 63 |
install(console, max_depth=1) |
| 64 |
sys.displayhook({"foo": {"bar": True}}) |
| 65 |
assert console.file.getvalue() == "{'foo': {...}}\n" |
| 66 |
assert sys.displayhook is not dh |
| 67 |
|
| 68 |
|
| 69 |
def test_ipy_display_hook__repr_html() -> None: |
| 70 |
console = Console(file=io.StringIO(), force_jupyter=True) |
| 71 |
|
| 72 |
class Thing: |
| 73 |
def _repr_html_(self): |
| 74 |
return "hello" |
| 75 |
|
| 76 |
console.begin_capture() |
| 77 |
_ipy_display_hook(Thing(), console=console) |
| 78 |
|
| 79 |
# Rendering delegated to notebook because _repr_html_ method exists |
| 80 |
assert console.end_capture() == "" |
| 81 |
|
| 82 |
|
| 83 |
def test_ipy_display_hook__multiple_special_reprs() -> None: |
| 84 |
""" |
| 85 |
The case where there are multiple IPython special _repr_*_ |
| 86 |
methods on the object, and one of them returns None but another |
| 87 |
one does not. |
| 88 |
""" |
| 89 |
console = Console(file=io.StringIO(), force_jupyter=True) |
| 90 |
|
| 91 |
class Thing: |
| 92 |
def __repr__(self): |
| 93 |
return "A Thing" |
| 94 |
|
| 95 |
def _repr_latex_(self): |
| 96 |
return None |
| 97 |
|
| 98 |
def _repr_html_(self): |
| 99 |
return "hello" |
| 100 |
|
| 101 |
result = _ipy_display_hook(Thing(), console=console) |
| 102 |
assert result == "A Thing" |
| 103 |
|
| 104 |
|
| 105 |
def test_ipy_display_hook__no_special_repr_methods() -> None: |
| 106 |
console = Console(file=io.StringIO(), force_jupyter=True) |
| 107 |
|
| 108 |
class Thing: |
| 109 |
def __repr__(self) -> str: |
| 110 |
return "hello" |
| 111 |
|
| 112 |
result = _ipy_display_hook(Thing(), console=console) |
| 113 |
# should be repr as-is |
| 114 |
assert result == "hello" |
| 115 |
|
| 116 |
|
| 117 |
def test_ipy_display_hook__special_repr_raises_exception() -> None: |
| 118 |
""" |
| 119 |
When an IPython special repr method raises an exception, |
| 120 |
we treat it as if it doesn't exist and look for the next. |
| 121 |
""" |
| 122 |
console = Console(file=io.StringIO(), force_jupyter=True) |
| 123 |
|
| 124 |
class Thing: |
| 125 |
def _repr_markdown_(self): |
| 126 |
raise Exception() |
| 127 |
|
| 128 |
def _repr_latex_(self): |
| 129 |
return None |
| 130 |
|
| 131 |
def _repr_html_(self): |
| 132 |
return "hello" |
| 133 |
|
| 134 |
def __repr__(self): |
| 135 |
return "therepr" |
| 136 |
|
| 137 |
result = _ipy_display_hook(Thing(), console=console) |
| 138 |
assert result == "therepr" |
| 139 |
|
| 140 |
|
| 141 |
def test_ipy_display_hook__console_renderables_on_newline() -> None: |
| 142 |
console = Console(file=io.StringIO(), force_jupyter=True) |
| 143 |
console.begin_capture() |
| 144 |
result = _ipy_display_hook(Text("hello"), console=console) |
| 145 |
assert result == "\nhello" |
| 146 |
|
| 147 |
|
| 148 |
def test_pretty() -> None: |
| 149 |
test = { |
| 150 |
"foo": [1, 2, 3, (4, 5, {6}, 7, 8, {9}), {}], |
| 151 |
"bar": {"egg": "baz", "words": ["Hello World"] * 10}, |
| 152 |
False: "foo", |
| 153 |
True: "", |
| 154 |
"text": ("Hello World", "foo bar baz egg"), |
| 155 |
} |
| 156 |
|
| 157 |
result = pretty_repr(test, max_width=80) |
| 158 |
print(result) |
| 159 |
expected = "{\n 'foo': [1, 2, 3, (4, 5, {6}, 7, 8, {9}), {}],\n 'bar': {\n 'egg': 'baz',\n 'words': [\n 'Hello World',\n 'Hello World',\n 'Hello World',\n 'Hello World',\n 'Hello World',\n 'Hello World',\n 'Hello World',\n 'Hello World',\n 'Hello World',\n 'Hello World'\n ]\n },\n False: 'foo',\n True: '',\n 'text': ('Hello World', 'foo bar baz egg')\n}" |
| 160 |
print(expected) |
| 161 |
assert result == expected |
| 162 |
|
| 163 |
|
| 164 |
@dataclass |
| 165 |
class ExampleDataclass: |
| 166 |
foo: int |
| 167 |
bar: str |
| 168 |
ignore: int = field(repr=False) |
| 169 |
baz: List[str] = field(default_factory=list) |
| 170 |
last: int = field(default=1, repr=False) |
| 171 |
|
| 172 |
|
| 173 |
@dataclass |
| 174 |
class Empty: |
| 175 |
pass |
| 176 |
|
| 177 |
|
| 178 |
def test_pretty_dataclass() -> None: |
| 179 |
dc = ExampleDataclass(1000, "Hello, World", 999, ["foo", "bar", "baz"]) |
| 180 |
result = pretty_repr(dc, max_width=80) |
| 181 |
print(repr(result)) |
| 182 |
assert ( |
| 183 |
result |
| 184 |
== "ExampleDataclass(foo=1000, bar='Hello, World', baz=['foo', 'bar', 'baz'])" |
| 185 |
) |
| 186 |
result = pretty_repr(dc, max_width=16) |
| 187 |
print(repr(result)) |
| 188 |
assert ( |
| 189 |
result |
| 190 |
== "ExampleDataclass(\n foo=1000,\n bar='Hello, World',\n baz=[\n 'foo',\n 'bar',\n 'baz'\n ]\n)" |
| 191 |
) |
| 192 |
dc.bar = dc |
| 193 |
result = pretty_repr(dc, max_width=80) |
| 194 |
print(repr(result)) |
| 195 |
assert result == "ExampleDataclass(foo=1000, bar=..., baz=['foo', 'bar', 'baz'])" |
| 196 |
|
| 197 |
|
| 198 |
def test_empty_dataclass() -> None: |
| 199 |
assert pretty_repr(Empty()) == "Empty()" |
| 200 |
assert pretty_repr([Empty()]) == "[Empty()]" |
| 201 |
|
| 202 |
|
| 203 |
class StockKeepingUnit(NamedTuple): |
| 204 |
name: str |
| 205 |
description: str |
| 206 |
price: float |
| 207 |
category: str |
| 208 |
reviews: List[str] |
| 209 |
|
| 210 |
|
| 211 |
def test_pretty_namedtuple() -> None: |
| 212 |
console = Console(color_system=None) |
| 213 |
console.begin_capture() |
| 214 |
|
| 215 |
example_namedtuple = StockKeepingUnit( |
| 216 |
"Sparkling British Spring Water", |
| 217 |
"Carbonated spring water", |
| 218 |
0.9, |
| 219 |
"water", |
| 220 |
["its amazing!", "its terrible!"], |
| 221 |
) |
| 222 |
|
| 223 |
result = pretty_repr(example_namedtuple) |
| 224 |
|
| 225 |
print(result) |
| 226 |
assert ( |
| 227 |
result |
| 228 |
== """StockKeepingUnit( |
| 229 |
name='Sparkling British Spring Water', |
| 230 |
description='Carbonated spring water', |
| 231 |
price=0.9, |
| 232 |
category='water', |
| 233 |
reviews=['its amazing!', 'its terrible!'] |
| 234 |
)""" |
| 235 |
) |
| 236 |
|
| 237 |
|
| 238 |
def test_pretty_namedtuple_length_one_no_trailing_comma() -> None: |
| 239 |
instance = collections.namedtuple("Thing", ["name"])(name="Bob") |
| 240 |
assert pretty_repr(instance) == "Thing(name='Bob')" |
| 241 |
|
| 242 |
|
| 243 |
def test_pretty_namedtuple_empty() -> None: |
| 244 |
instance = collections.namedtuple("Thing", [])() |
| 245 |
assert pretty_repr(instance) == "Thing()" |
| 246 |
|
| 247 |
|
| 248 |
def test_pretty_namedtuple_custom_repr() -> None: |
| 249 |
class Thing(NamedTuple): |
| 250 |
def __repr__(self): |
| 251 |
return "XX" |
| 252 |
|
| 253 |
assert pretty_repr(Thing()) == "XX" |
| 254 |
|
| 255 |
|
| 256 |
def test_pretty_namedtuple_fields_invalid_type() -> None: |
| 257 |
class LooksLikeANamedTupleButIsnt(tuple): |
| 258 |
_fields = "blah" |
| 259 |
|
| 260 |
instance = LooksLikeANamedTupleButIsnt() |
| 261 |
result = pretty_repr(instance) |
| 262 |
assert result == "()" # Treated as tuple |
| 263 |
|
| 264 |
|
| 265 |
def test_pretty_namedtuple_max_depth() -> None: |
| 266 |
instance = {"unit": StockKeepingUnit("a", "b", 1.0, "c", ["d", "e"])} |
| 267 |
result = pretty_repr(instance, max_depth=1) |
| 268 |
assert result == "{'unit': StockKeepingUnit(...)}" |
| 269 |
|
| 270 |
|
| 271 |
def test_small_width() -> None: |
| 272 |
test = ["Hello world! 12345"] |
| 273 |
result = pretty_repr(test, max_width=10) |
| 274 |
expected = "[\n 'Hello world! 12345'\n]" |
| 275 |
assert result == expected |
| 276 |
|
| 277 |
|
| 278 |
def test_ansi_in_pretty_repr() -> None: |
| 279 |
class Hello: |
| 280 |
def __repr__(self): |
| 281 |
return "Hello \x1b[38;5;239mWorld!" |
| 282 |
|
| 283 |
pretty = Pretty(Hello()) |
| 284 |
|
| 285 |
console = Console(file=io.StringIO(), record=True) |
| 286 |
console.print(pretty) |
| 287 |
result = console.export_text() |
| 288 |
|
| 289 |
assert result == "Hello World!\n" |
| 290 |
|
| 291 |
|
| 292 |
def test_broken_repr() -> None: |
| 293 |
class BrokenRepr: |
| 294 |
def __repr__(self): |
| 295 |
1 / 0 |
| 296 |
|
| 297 |
test = [BrokenRepr()] |
| 298 |
result = pretty_repr(test) |
| 299 |
expected = "[<repr-error 'division by zero'>]" |
| 300 |
assert result == expected |
| 301 |
|
| 302 |
|
| 303 |
def test_broken_getattr() -> None: |
| 304 |
class BrokenAttr: |
| 305 |
def __getattr__(self, name): |
| 306 |
1 / 0 |
| 307 |
|
| 308 |
def __repr__(self): |
| 309 |
return "BrokenAttr()" |
| 310 |
|
| 311 |
test = BrokenAttr() |
| 312 |
result = pretty_repr(test) |
| 313 |
assert result == "BrokenAttr()" |
| 314 |
|
| 315 |
|
| 316 |
def test_reference_cycle_container() -> None: |
| 317 |
test = [] |
| 318 |
test.append(test) |
| 319 |
res = pretty_repr(test) |
| 320 |
assert res == "[...]" |
| 321 |
|
| 322 |
test = [1, []] |
| 323 |
test[1].append(test) |
| 324 |
res = pretty_repr(test) |
| 325 |
assert res == "[1, [...]]" |
| 326 |
|
| 327 |
# Not a cyclic reference, just a repeated reference |
| 328 |
a = [2] |
| 329 |
test = [1, [a, a]] |
| 330 |
res = pretty_repr(test) |
| 331 |
assert res == "[1, [[2], [2]]]" |
| 332 |
|
| 333 |
|
| 334 |
def test_reference_cycle_namedtuple() -> None: |
| 335 |
class Example(NamedTuple): |
| 336 |
x: int |
| 337 |
y: Any |
| 338 |
|
| 339 |
test = Example(1, [Example(2, [])]) |
| 340 |
test.y[0].y.append(test) |
| 341 |
res = pretty_repr(test) |
| 342 |
assert res == "Example(x=1, y=[Example(x=2, y=[...])])" |
| 343 |
|
| 344 |
# Not a cyclic reference, just a repeated reference |
| 345 |
a = Example(2, None) |
| 346 |
test = Example(1, [a, a]) |
| 347 |
res = pretty_repr(test) |
| 348 |
assert res == "Example(x=1, y=[Example(x=2, y=None), Example(x=2, y=None)])" |
| 349 |
|
| 350 |
|
| 351 |
def test_reference_cycle_dataclass() -> None: |
| 352 |
@dataclass |
| 353 |
class Example: |
| 354 |
x: int |
| 355 |
y: Any |
| 356 |
|
| 357 |
test = Example(1, None) |
| 358 |
test.y = test |
| 359 |
res = pretty_repr(test) |
| 360 |
assert res == "Example(x=1, y=...)" |
| 361 |
|
| 362 |
test = Example(1, Example(2, None)) |
| 363 |
test.y.y = test |
| 364 |
res = pretty_repr(test) |
| 365 |
assert res == "Example(x=1, y=Example(x=2, y=...))" |
| 366 |
|
| 367 |
# Not a cyclic reference, just a repeated reference |
| 368 |
a = Example(2, None) |
| 369 |
test = Example(1, [a, a]) |
| 370 |
res = pretty_repr(test) |
| 371 |
assert res == "Example(x=1, y=[Example(x=2, y=None), Example(x=2, y=None)])" |
| 372 |
|
| 373 |
|
| 374 |
def test_reference_cycle_attrs() -> None: |
| 375 |
@attr.define |
| 376 |
class Example: |
| 377 |
x: int |
| 378 |
y: Any |
| 379 |
|
| 380 |
test = Example(1, None) |
| 381 |
test.y = test |
| 382 |
res = pretty_repr(test) |
| 383 |
assert res == "Example(x=1, y=...)" |
| 384 |
|
| 385 |
test = Example(1, Example(2, None)) |
| 386 |
test.y.y = test |
| 387 |
res = pretty_repr(test) |
| 388 |
assert res == "Example(x=1, y=Example(x=2, y=...))" |
| 389 |
|
| 390 |
# Not a cyclic reference, just a repeated reference |
| 391 |
a = Example(2, None) |
| 392 |
test = Example(1, [a, a]) |
| 393 |
res = pretty_repr(test) |
| 394 |
assert res == "Example(x=1, y=[Example(x=2, y=None), Example(x=2, y=None)])" |
| 395 |
|
| 396 |
|
| 397 |
def test_reference_cycle_custom_repr() -> None: |
| 398 |
class Example: |
| 399 |
def __init__(self, x, y): |
| 400 |
self.x = x |
| 401 |
self.y = y |
| 402 |
|
| 403 |
def __rich_repr__(self): |
| 404 |
yield ("x", self.x) |
| 405 |
yield ("y", self.y) |
| 406 |
|
| 407 |
test = Example(1, None) |
| 408 |
test.y = test |
| 409 |
res = pretty_repr(test) |
| 410 |
assert res == "Example(x=1, y=...)" |
| 411 |
|
| 412 |
test = Example(1, Example(2, None)) |
| 413 |
test.y.y = test |
| 414 |
res = pretty_repr(test) |
| 415 |
assert res == "Example(x=1, y=Example(x=2, y=...))" |
| 416 |
|
| 417 |
# Not a cyclic reference, just a repeated reference |
| 418 |
a = Example(2, None) |
| 419 |
test = Example(1, [a, a]) |
| 420 |
res = pretty_repr(test) |
| 421 |
assert res == "Example(x=1, y=[Example(x=2, y=None), Example(x=2, y=None)])" |
| 422 |
|
| 423 |
|
| 424 |
def test_max_depth() -> None: |
| 425 |
d = {} |
| 426 |
d["foo"] = {"fob": {"a": [1, 2, 3], "b": {"z": "x", "y": ["a", "b", "c"]}}} |
| 427 |
|
| 428 |
assert pretty_repr(d, max_depth=0) == "{...}" |
| 429 |
assert pretty_repr(d, max_depth=1) == "{'foo': {...}}" |
| 430 |
assert pretty_repr(d, max_depth=2) == "{'foo': {'fob': {...}}}" |
| 431 |
assert pretty_repr(d, max_depth=3) == "{'foo': {'fob': {'a': [...], 'b': {...}}}}" |
| 432 |
assert ( |
| 433 |
pretty_repr(d, max_width=100, max_depth=4) |
| 434 |
== "{'foo': {'fob': {'a': [1, 2, 3], 'b': {'z': 'x', 'y': [...]}}}}" |
| 435 |
) |
| 436 |
assert ( |
| 437 |
pretty_repr(d, max_width=100, max_depth=5) |
| 438 |
== "{'foo': {'fob': {'a': [1, 2, 3], 'b': {'z': 'x', 'y': ['a', 'b', 'c']}}}}" |
| 439 |
) |
| 440 |
assert ( |
| 441 |
pretty_repr(d, max_width=100, max_depth=None) |
| 442 |
== "{'foo': {'fob': {'a': [1, 2, 3], 'b': {'z': 'x', 'y': ['a', 'b', 'c']}}}}" |
| 443 |
) |
| 444 |
|
| 445 |
|
| 446 |
def test_max_depth_rich_repr() -> None: |
| 447 |
class Foo: |
| 448 |
def __init__(self, foo): |
| 449 |
self.foo = foo |
| 450 |
|
| 451 |
def __rich_repr__(self): |
| 452 |
yield "foo", self.foo |
| 453 |
|
| 454 |
class Bar: |
| 455 |
def __init__(self, bar): |
| 456 |
self.bar = bar |
| 457 |
|
| 458 |
def __rich_repr__(self): |
| 459 |
yield "bar", self.bar |
| 460 |
|
| 461 |
assert ( |
| 462 |
pretty_repr(Foo(foo=Bar(bar=Foo(foo=[]))), max_depth=2) |
| 463 |
== "Foo(foo=Bar(bar=Foo(...)))" |
| 464 |
) |
| 465 |
|
| 466 |
|
| 467 |
def test_max_depth_attrs() -> None: |
| 468 |
@attr.define |
| 469 |
class Foo: |
| 470 |
foo = attr.field() |
| 471 |
|
| 472 |
@attr.define |
| 473 |
class Bar: |
| 474 |
bar = attr.field() |
| 475 |
|
| 476 |
assert ( |
| 477 |
pretty_repr(Foo(foo=Bar(bar=Foo(foo=[]))), max_depth=2) |
| 478 |
== "Foo(foo=Bar(bar=Foo(...)))" |
| 479 |
) |
| 480 |
|
| 481 |
|
| 482 |
def test_max_depth_dataclass() -> None: |
| 483 |
@dataclass |
| 484 |
class Foo: |
| 485 |
foo: object |
| 486 |
|
| 487 |
@dataclass |
| 488 |
class Bar: |
| 489 |
bar: object |
| 490 |
|
| 491 |
assert ( |
| 492 |
pretty_repr(Foo(foo=Bar(bar=Foo(foo=[]))), max_depth=2) |
| 493 |
== "Foo(foo=Bar(bar=Foo(...)))" |
| 494 |
) |
| 495 |
|
| 496 |
|
| 497 |
def test_defaultdict() -> None: |
| 498 |
test_dict = defaultdict(int, {"foo": 2}) |
| 499 |
result = pretty_repr(test_dict) |
| 500 |
assert result == "defaultdict(<class 'int'>, {'foo': 2})" |
| 501 |
|
| 502 |
|
| 503 |
def test_deque() -> None: |
| 504 |
test_deque = deque([1, 2, 3]) |
| 505 |
result = pretty_repr(test_deque) |
| 506 |
assert result == "deque([1, 2, 3])" |
| 507 |
test_deque = deque([1, 2, 3], maxlen=None) |
| 508 |
result = pretty_repr(test_deque) |
| 509 |
assert result == "deque([1, 2, 3])" |
| 510 |
test_deque = deque([1, 2, 3], maxlen=5) |
| 511 |
result = pretty_repr(test_deque) |
| 512 |
assert result == "deque([1, 2, 3], maxlen=5)" |
| 513 |
test_deque = deque([1, 2, 3], maxlen=0) |
| 514 |
result = pretty_repr(test_deque) |
| 515 |
assert result == "deque(maxlen=0)" |
| 516 |
test_deque = deque([]) |
| 517 |
result = pretty_repr(test_deque) |
| 518 |
assert result == "deque()" |
| 519 |
test_deque = deque([], maxlen=None) |
| 520 |
result = pretty_repr(test_deque) |
| 521 |
assert result == "deque()" |
| 522 |
test_deque = deque([], maxlen=5) |
| 523 |
result = pretty_repr(test_deque) |
| 524 |
assert result == "deque(maxlen=5)" |
| 525 |
test_deque = deque([], maxlen=0) |
| 526 |
result = pretty_repr(test_deque) |
| 527 |
assert result == "deque(maxlen=0)" |
| 528 |
|
| 529 |
|
| 530 |
def test_array() -> None: |
| 531 |
test_array = array("I", [1, 2, 3]) |
| 532 |
result = pretty_repr(test_array) |
| 533 |
assert result == "array('I', [1, 2, 3])" |
| 534 |
|
| 535 |
|
| 536 |
def test_tuple_of_one() -> None: |
| 537 |
assert pretty_repr((1,)) == "(1,)" |
| 538 |
|
| 539 |
|
| 540 |
def test_node() -> None: |
| 541 |
node = Node("abc") |
| 542 |
assert pretty_repr(node) == "abc: " |
| 543 |
|
| 544 |
|
| 545 |
def test_indent_lines() -> None: |
| 546 |
console = Console(width=100, color_system=None) |
| 547 |
console.begin_capture() |
| 548 |
console.print(Pretty([100, 200], indent_guides=True), width=8) |
| 549 |
expected = """\ |
| 550 |
[ |
| 551 |
│ 100, |
| 552 |
│ 200 |
| 553 |
] |
| 554 |
""" |
| 555 |
result = console.end_capture() |
| 556 |
print(repr(result)) |
| 557 |
print(result) |
| 558 |
assert result == expected |
| 559 |
|
| 560 |
|
| 561 |
def test_pprint() -> None: |
| 562 |
console = Console(color_system=None) |
| 563 |
console.begin_capture() |
| 564 |
pprint(1, console=console) |
| 565 |
assert console.end_capture() == "1\n" |
| 566 |
|
| 567 |
|
| 568 |
def test_pprint_max_values() -> None: |
| 569 |
console = Console(color_system=None) |
| 570 |
console.begin_capture() |
| 571 |
pprint([1, 2, 3, 4, 5, 6, 7, 8, 9, 0], console=console, max_length=2) |
| 572 |
assert console.end_capture() == "[1, 2, ... +8]\n" |
| 573 |
|
| 574 |
|
| 575 |
def test_pprint_max_items() -> None: |
| 576 |
console = Console(color_system=None) |
| 577 |
console.begin_capture() |
| 578 |
pprint({"foo": 1, "bar": 2, "egg": 3}, console=console, max_length=2) |
| 579 |
assert console.end_capture() == """{'foo': 1, 'bar': 2, ... +1}\n""" |
| 580 |
|
| 581 |
|
| 582 |
def test_pprint_max_string() -> None: |
| 583 |
console = Console(color_system=None) |
| 584 |
console.begin_capture() |
| 585 |
pprint(["Hello" * 20], console=console, max_string=8) |
| 586 |
assert console.end_capture() == """['HelloHel'+92]\n""" |
| 587 |
|
| 588 |
|
| 589 |
def test_tuples() -> None: |
| 590 |
console = Console(color_system=None) |
| 591 |
console.begin_capture() |
| 592 |
pprint((1,), console=console) |
| 593 |
pprint((1,), expand_all=True, console=console) |
| 594 |
pprint(((1,),), expand_all=True, console=console) |
| 595 |
result = console.end_capture() |
| 596 |
print(repr(result)) |
| 597 |
expected = "(1,)\n(\n│ 1,\n)\n(\n│ (\n│ │ 1,\n│ ),\n)\n" |
| 598 |
print(result) |
| 599 |
print("--") |
| 600 |
print(expected) |
| 601 |
assert result == expected |
| 602 |
|
| 603 |
|
| 604 |
def test_newline() -> None: |
| 605 |
console = Console(color_system=None) |
| 606 |
console.begin_capture() |
| 607 |
console.print(Pretty((1,), insert_line=True, expand_all=True)) |
| 608 |
result = console.end_capture() |
| 609 |
expected = "\n(\n 1,\n)\n" |
| 610 |
assert result == expected |
| 611 |
|
| 612 |
|
| 613 |
def test_empty_repr() -> None: |
| 614 |
class Foo: |
| 615 |
def __repr__(self): |
| 616 |
return "" |
| 617 |
|
| 618 |
assert pretty_repr(Foo()) == "" |
| 619 |
|
| 620 |
|
| 621 |
def test_attrs() -> None: |
| 622 |
@attr.define |
| 623 |
class Point: |
| 624 |
x: int |
| 625 |
y: int |
| 626 |
foo: str = attr.field(repr=str.upper) |
| 627 |
z: int = 0 |
| 628 |
|
| 629 |
result = pretty_repr(Point(1, 2, foo="bar")) |
| 630 |
print(repr(result)) |
| 631 |
expected = "Point(x=1, y=2, foo=BAR, z=0)" |
| 632 |
assert result == expected |
| 633 |
|
| 634 |
|
| 635 |
def test_attrs_empty() -> None: |
| 636 |
@attr.define |
| 637 |
class Nada: |
| 638 |
pass |
| 639 |
|
| 640 |
result = pretty_repr(Nada()) |
| 641 |
print(repr(result)) |
| 642 |
expected = "Nada()" |
| 643 |
assert result == expected |
| 644 |
|
| 645 |
|
| 646 |
@skip_py310 |
| 647 |
@skip_py311 |
| 648 |
@skip_py312 |
| 649 |
@skip_py313 |
| 650 |
@skip_py314 |
| •651 |
@skip_py315 |
| 652 |
def test_attrs_broken() -> None: |
| 653 |
@attr.define |
| 654 |
class Foo: |
| 655 |
bar: int |
| 656 |
|
| 657 |
foo = Foo(1) |
| 658 |
del foo.bar |
| 659 |
result = pretty_repr(foo) |
| 660 |
print(repr(result)) |
| 661 |
expected = "Foo(bar=AttributeError('bar'))" |
| 662 |
assert result == expected |
| 663 |
|
| 664 |
|
| 665 |
@skip_py38 |
| 666 |
@skip_py39 |
| 667 |
def test_attrs_broken_310() -> None: |
| 668 |
@attr.define |
| 669 |
class Foo: |
| 670 |
bar: int |
| 671 |
|
| 672 |
foo = Foo(1) |
| 673 |
del foo.bar |
| 674 |
result = pretty_repr(foo) |
| 675 |
print(repr(result)) |
| 676 |
if sys.version_info >= (3, 13): |
| 677 |
expected = "Foo(\n bar=AttributeError(\"'tests.test_pretty.test_attrs_broken_310.<locals>.Foo' object has no attribute 'bar'\")\n)" |
| 678 |
else: |
| 679 |
expected = "Foo(bar=AttributeError(\"'Foo' object has no attribute 'bar'\"))" |
| 680 |
assert result == expected |
| 681 |
|
| 682 |
|
| 683 |
def test_user_dict() -> None: |
| 684 |
class D1(UserDict): |
| 685 |
pass |
| 686 |
|
| 687 |
class D2(UserDict): |
| 688 |
def __repr__(self): |
| 689 |
return "FOO" |
| 690 |
|
| 691 |
d1 = D1({"foo": "bar"}) |
| 692 |
d2 = D2({"foo": "bar"}) |
| 693 |
result = pretty_repr(d1, expand_all=True) |
| 694 |
print(repr(result)) |
| 695 |
assert result == "{\n 'foo': 'bar'\n}" |
| 696 |
result = pretty_repr(d2, expand_all=True) |
| 697 |
print(repr(result)) |
| 698 |
assert result == "FOO" |
| 699 |
|
| 700 |
|
| 701 |
def test_lying_attribute() -> None: |
| 702 |
"""Test getattr doesn't break rich repr protocol""" |
| 703 |
|
| 704 |
class Foo: |
| 705 |
def __getattr__(self, attr): |
| 706 |
return "foo" |
| 707 |
|
| 708 |
foo = Foo() |
| 709 |
result = pretty_repr(foo) |
| 710 |
assert "Foo" in result |
| 711 |
|
| 712 |
|
| 713 |
def test_measure_pretty() -> None: |
| 714 |
"""Test measure respects expand_all""" |
| 715 |
# https://github.com/Textualize/rich/issues/1998 |
| 716 |
console = Console() |
| 717 |
pretty = Pretty(["alpha", "beta", "delta", "gamma"], expand_all=True) |
| 718 |
|
| 719 |
measurement = console.measure(pretty) |
| 720 |
assert measurement == Measurement(12, 12) |
| 721 |
|
| 722 |
|
| 723 |
def test_tuple_rich_repr() -> None: |
| 724 |
""" |
| 725 |
Test that can use None as key to have tuple positional values. |
| 726 |
""" |
| 727 |
|
| 728 |
class Foo: |
| 729 |
def __rich_repr__(self): |
| 730 |
yield None, (1,) |
| 731 |
|
| 732 |
assert pretty_repr(Foo()) == "Foo((1,))" |
| 733 |
|
| 734 |
|
| 735 |
def test_tuple_rich_repr_default() -> None: |
| 736 |
""" |
| 737 |
Test that can use None as key to have tuple positional values and with a default. |
| 738 |
""" |
| 739 |
|
| 740 |
class Foo: |
| 741 |
def __rich_repr__(self): |
| 742 |
yield None, (1,), (1,) |
| 743 |
|
| 744 |
assert pretty_repr(Foo()) == "Foo()" |
| 745 |
|
| 746 |
|
| 747 |
def test_dataclass_no_attribute() -> None: |
| 748 |
"""Regression test for https://github.com/Textualize/rich/issues/3417""" |
| 749 |
from dataclasses import dataclass, field |
| 750 |
|
| 751 |
@dataclass(eq=False) |
| 752 |
class BadDataclass: |
| 753 |
item: int = field(init=False) |
| 754 |
|
| 755 |
# item is not provided |
| 756 |
bad_data_class = BadDataclass() |
| 757 |
|
| 758 |
console = Console() |
| 759 |
with console.capture() as capture: |
| 760 |
console.print(bad_data_class) |
| 761 |
|
| 762 |
expected = "BadDataclass()\n" |
| 763 |
result = capture.get() |
| 764 |
assert result == expected |