-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Terminal width inference issues causing doctest failures #4030
Comments
Hi @standage, Thanks for the report. I have scanned the standard |
@standage Can you post a minimal example please, which we can use to reproduce your issue? |
Ok, here is a minimal example. example.pyimport pandas
moduledata = pandas.read_table('data.tsv')
def query(q):
"""Bug demo
When running the code below in the interactive python interpreter or with
doctest, the output is sensitive to the window width. With py.test it
always assumes a width of 80 chars.
>>> 1 + 1
2
>>> query('ID.str.contains("qwerty")')
ID Name Chromosome Start End Variants
1 qwertyqwerty qwertyqwerty 2 3333 4444 rs123456789,rs987654321,rs42
"""
return moduledata.query(q) data.tsv
When I run the following in the interactive interpreter I get expected behavior: truncated output with a thin window, full output with a wide window. >>> import example
>>> example.query('ID.str.contains("qwerty")')
ID Name Chromosome Start End Variants
1 qwertyqwerty qwertyqwerty 2 3333 4444 rs123456789,rs987654321,rs42
>>> When I run When I run |
Hi @standage, Thanks for the reproducible example, I can reproduce it locally. The problem seems to be on how terminal width is detected by from example import query
import pandas
moduledata = pandas.read_table('data.tsv')
print(query('ID.str.contains("qwerty")')) When I execute this script in my terminal (Windows) while setting
So it seems to be related to how |
This has to do with terminal capturing:
when the stdin / stdout are redirected they are not |
@asottile thanks, makes sense. I wonder what |
The difference is when doctest patches The ordering from pytest:
The ordering from doctest
under doctest, |
Can you point to some code? I would like to know if we can reproduce this behavior in pytest. |
I guess it doesn't determine it as part of import, but it determines whether it should try to determine it: https://github.com/pandas-dev/pandas/blob/0c58a825181bde2c4d7e1a912e246181d33f55d6/pandas/core/config_init.py#L318-L321 (and since |
ESSS/pytest-regressions#3 (comment) seems like a reasonable approach -- probably as an autouse fixture that sets / unsets it 🤷♂️ |
Ok, so if I understand correctly:
I'd be happy to submit a PR, although I don't know enough about pytest internals to know where a command like By the way, I've finally posted the package from which I made my contrived example above at https://github.com/bioforensics/MicroHapDB. |
this wouldn't go in |
I'm familiar with fixtures, but not |
something like this: import pandas
import pytest
@pytest.fixture(autouse=True, scope='session')
def pandas_terminal_width():
pandas.set_option('max_cols', 1000) put that in |
Oh good, that looked a lot like my first blundering attempts! :-) Thanks. For the contrived example above, if I put |
ah yeah, try |
Actually, I had to set both. Only increasing the width truncated the number of columns, and only increasing the number of columns just wrapped the output at 80 chars. Doing both did the trick! Updating the example now. Thanks!!! |
Oops, since I can't update your example it's: import pandas
import pytest
@pytest.fixture(autouse=True, scope='session')
def pandas_terminal_width():
pandas.set_option('display.width', 1000)
pandas.set_option('display.max_columns', 1000) |
Greetings. Thanks for your work on an excellent library!
I am writing some doctests whose output seems to be sensitive to the width of the terminal—dumps of Pandas dataframes that sometimes exceed 80 chars in width. When I run the commands on the Python interpreter interactively, I can reproduce the Pandas behavior: when the window is wide, the full table is printed; when the window is narrow, columns in the middle are truncated.
I can confirm that doctest handles things correctly as well. When I execute
python -m doctest myfile.py
with the window at 80 chars width, the output is truncated and the test fails. When I execute the same command at 120 chars width, the output is not truncated and the doctests pass.However, when running
py.test --doctest-module myfile.py
the tests fail regardless of window width. I can restart my window manager (tmux), I can restart the window manager, resize the window itself, or even close the window and start over from scratch, but no combination of these operations seems to convince py.test that my terminal is wider than 80 chars.This isn’t a problem on my personal machine (OS X High Sierra) but it is on a work CentOS 6 system running py.test 3.6.2.
Thanks for submitting an issue!
Here's a quick checklist in what to include:
[ ]pip list
of the virtual environment you are usingThe text was updated successfully, but these errors were encountered: