Skip to content

Commit c694662

Browse files
authored
Fix printing long strings in %whos magic command. (ipython#14938)
In trying to improve printing for dataframe and series objects, in pull request ipython#14926, i inadvertently messed up printing of long strings...sorry! This change fixes that issue. ```python ## Does not mess up long strings In [1]: sunday_qry = """ ...: with ...: df as (select unnest(generate_series(date '1900-01-01',date '2100-12-31',interval 1 day)) as dt) ...: select * from df where extract(dayofweek from dt)=0; ...: """ ...: In [2]: df = pd.DataFrame({"a": range(10), "b": range(10,20),"c":list(string.ascii_letters[:10])}) In [3]: %whos Variable Type Data/Info ----------------------------------- df DataFrame Shape: (10, 3) sunday_qry str \nwith\n df as (select<...>t(dayofweek from dt)=0;\n In [4]: """ ``` Compare this to what it currently does. ```python ## inadvertently messed up in ipython#14926 In [1]: sunday_qry = """ ...: with ...: df as (select unnest(generate_series(date '1900-01-01',date '2100-12-31',interval 1 day)) as dt) ...: select * from df where extract(dayofweek from dt)=0; ...: """ ...: In [2]: df = pd.DataFrame({"a": range(10), "b": range(10,20),"c":list(string.ascii_letters[:10])}) In [3]: %whos Variable Type Data/Info ---------------------------------------- df DataFrame Shape: (10, 3) sunday_qry str with df as (select unnest(generate_series(date '1900-01-01',date '2100-12-31',interval 1 day)) as dt) select * from df where extract(dayofweek from dt)=0; In [4]: ```
2 parents d2342c8 + 4a1130e commit c694662

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

IPython/core/magics/namespace.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,9 @@ def type_name(v):
470470
# Useful for DataFrames and Series
471471
# Ought to work for both pandas and polars
472472
print(f"Shape: {var.shape}")
473-
elif hasattr(var, "__len__"):
473+
elif hasattr(var, "__len__") and not isinstance(var, str):
474474
## types that can be used in len function
475-
print(var if isinstance(var, str) else f"n={len(var)}")
475+
print(f"n={len(var)}")
476476
else:
477477
try:
478478
vstr = str(var)

tests/test_magic.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,40 @@ def __repr__(self):
732732
_ip.run_line_magic("whos", "")
733733

734734

735+
@pytest.mark.parametrize(
736+
"input,expected",
737+
(
738+
(
739+
"The quick brown fox jumps over the lazy dog",
740+
"The quick brown fox jumps over the lazy dog",
741+
),
742+
(
743+
" ".join(["The quick brown fox jumps over the lazy dog"] * 2),
744+
"The quick brown fox jumps<...>x jumps over the lazy dog",
745+
),
746+
("\nThis is \n\na long\n\nstring\n", r"\nThis is \n\na long\n\nstring\n"),
747+
(
748+
"\rThis is \r\ra long\r\rstring\r",
749+
r"\rThis is \r\ra long\r\rstring\r",
750+
),
751+
),
752+
)
753+
def test_whos_longstr(input, expected):
754+
ip = get_ipython()
755+
ip.user_ns["input"] = input
756+
ip.user_ns["expected"] = expected
757+
with capture_output() as captured:
758+
ip.run_line_magic("whos", "")
759+
stdout = captured.stdout
760+
from_whos = " ".join(
761+
re.split(
762+
r"\s+str\s+",
763+
[l for l in stdout.splitlines() if l.startswith("expected")][0],
764+
)[1:]
765+
)
766+
assert expected == from_whos
767+
768+
735769
def doctest_precision():
736770
"""doctest for %precision
737771

0 commit comments

Comments
 (0)