rich icon indicating copy to clipboard operation
rich copied to clipboard

[BUG] Behavior of console.print() with empty *args is different from regular print()

Open omaxx opened this issue 7 months ago • 4 comments

Describe the bug

Expected that console.print(end="!") will print only ! as regular python print(), but it print line break without ending symbol.

from rich.console import Console

console = Console()

for f in (
    lambda: print("A", end="!"),
    lambda: print(end="!"),
    lambda: console.print("A", end="!"),
    lambda: console.print(end="!"),
):
    print("=>", end="")
    f()
    print("<=")

Output:

❯ python3 test.py
=>A!<=
=>!<=
=>A!<=
=>
<=

rich == 14.0.0

omaxx avatar Apr 15 '25 22:04 omaxx

We found the following entry in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

github-actions[bot] avatar Apr 15 '25 22:04 github-actions[bot]

I tried reproducing it with your test code, and i can say it does output a line break instead of '!'.

im thinking the issue might be to do with how 'console.print' handles empty content, and could be ignoring the custom 'end' parm

im gonna take a look at the class and write some tests and just try help

dillionhuston avatar Apr 16 '25 12:04 dillionhuston

I’ve looked at the Console.print method in console.py, which accepts *objects, sep, and end parameters

The issue is likely happening when 'objects' is empty, causing 'print' to default to a new line

The issue happens when *objects is empty, causing print to default to a newline (possibly via self.file.write

dillionhuston avatar Apr 16 '25 12:04 dillionhuston

Are these lines necessary: https://github.com/Textualize/rich/blob/master/rich/console.py#L1680

        if not objects:
            objects = (NewLine(),)

?

omaxx avatar Apr 16 '25 15:04 omaxx