Skip to content
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

support ipynb ipython parser for magic and system #88

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
23 changes: 22 additions & 1 deletion pigar/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
from .helpers import parse_git_config, trim_suffix

import nbformat
try:
import IPython
# untested
# only support IPython >= 6.0.0
_transformer_manager = IPython.core.inputtransformer2.TransformerManager()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IPython should be present in setup.py,

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly, for the parser to works (my version), it requires IPython >= 6, whereas in colab, it's IPython 5. The problem, both versions have different Transformer API.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remove IPython from the setup.py because it's not the core for pigar. It's just for pigar on IPython. If you want to make all support for IPython, that's good, but sadly I can't help because I can't run the test (can't debug your codes, i don't know why).


# future: support IPython LTS 5.0.0 used by Google Colab
# _transformer_manager = ...

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this mean?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It means that the current transformer (parser) only supports IPython > 6, and someone needs to add functionality to support IPython 5 LTS that is used by Google Colab.

I don't have IPython 5 LTS to test it and I don't use it anyway. So, Installing all the dependecy from scratch and make virtual environment is too much hassle (furthermore, I can't run your unittest).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of them seems to be an issue to me, also to you.

You can run the tests like that:

pigar/makefile

Lines 10 to 11 in 823a3c2

run-tests:
python -m unittest discover pigar/tests/ -t . -v

Copy link
Author

@yasirroni yasirroni Feb 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using IPython 7.0:

======================================================================
FAIL: test_py3_requirements (pigar.tests.test_core.ReqsTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "P:\GitHub\pigar\pigar\tests\test_core.py", line 64, in test_py3_requirements
    self.assertListEqual(sorted(guess.keys()), ['foobar'])
AssertionError: Lists differ: ['foobar', 'subbar', 'subfoo'] != ['foobar']

First list contains 2 additional elements.
First extra element 1:
'subbar'

- ['foobar', 'subbar', 'subfoo']
+ ['foobar']

----------------------------------------------------------------------

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, for IPython 5.0 LTS: transformer

I haven't figured out how to use it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

except Exception:
import warnings
_transformer_manager = None

Module = collections.namedtuple('Module', ['name', 'try_', 'file', 'lineno'])

Expand Down Expand Up @@ -61,7 +73,16 @@ def _read_code(fpath):
code = ""
for cell in nb.cells:
if cell.cell_type == "code":
code += cell.source + "\n"
if _transformer_manager:
code += _transformer_manager.transform_cell(cell.source) \
+ "\n"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the problem is the + "\n", could you remove it and run the test to see what happens?

Ref:
https://github.com/ipython/ipython/blob/86d24741188b0cedd78ab080d498e775ed0e5272/IPython/core/inputtransformer2.py#L588-L597

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

else:
# allow without !magic and !system support
warning_msg = """No IPython >= 6.0.0
Not using IPython to parse notebooks
"""
warnings.warn(warning_msg, Warning)
code += cell.source + "\n"
yasirroni marked this conversation as resolved.
Show resolved Hide resolved
return code
elif fpath.endswith(".py"):
with open(fpath, 'rb') as f:
Expand Down