Technologicat / pyan

Static call graph generator. The official Python 3 version. Development repo.
GNU General Public License v2.0
323 stars 56 forks source link

CallGraphVisitor wrong parameters #96

Open mhechthz opened 4 months ago

mhechthz commented 4 months ago

It seems that the library is somewhat outdated. I get with Python 3.11:

pyan3 *.py --uses --no-defines --colored --grouped --annotated --svg > callgraph.svg
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Python311\Scripts\pyan3.exe\__main__.py", line 7, in <module>
  File "C:\Python311\Lib\site-packages\pyan\main.py", line 206, in main
    v = CallGraphVisitor(filenames, logger, root=root)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: CallGraphVisitor.__init__() got multiple values for argument 'root'

Seems that there was a change in how to call the function. Is this library still maintained?

egoughnour commented 3 months ago

See [Fix wrong arguments for CallGraphVisitor] (https://github.com/Technologicat/pyan/pull/65). Also the comments on got multiple values for .... You can install, open line 206 and insert =logger so that the second arg becomes a kwarg, i.e., CallGraphVisitor(filenames, logger=logger, root=root) Then calling with an absolute path to your glob and setting the --root might help. Anyway this seems to be a non-issue when calling programmatically. So you might try importing and going from there--as suggested in the discussion of the second issue linked above.

HoseynAAmiri commented 3 weeks ago

@egoughnour How can I use --root ? Can you make an example please?

egoughnour commented 3 weeks ago

TL;DR maybe try installing from the github URI first, then pass the root directory as the value of --root.

Anyhow, let's start with what's broken. Running something like the below command is going to fail, probably.

$ pyan3 taskAutom.py --uses --no-defines --colored --grouped --annotated --dot-rankdir LR --html > taskAutom.html

There are a lot of arguments but notice root isn't specified. The command gives an error like this:

Traceback (most recent call last):
  File "/usr/local/bin/pyan3", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.8/dist-packages/pyan/main.py", line 206, in main
    v = CallGraphVisitor(filenames, logger, root=root)
TypeError: __init__() got multiple values for argument 'root'

This is fixed and explained in a diff (which doesn't seem to survive copy-paste):

v = CallGraphVisitor(filenames, logger=logger, root=root)

That is the right hand side. The left hand side is identical except that the keyword args/kwargs don't get supplied explicitly.

Anyhow the point is that the call from the CLI/shell does not (in some versions--probably including what's in PyPI now) call the constructor correctly based on the CLI arguments when it is run directly.

If you call pyan (such as create_callgraph()) from your code, it's fine. So if you call from code, import x syntax and from x import a, b, c, d syntax would work.

If you want the latest code (via pip) you could try something like this:

pip install --user git+https://github.com/kuuurt/pyan.git

or skip the user flag if you're already in a virtual env.

Otherwise try something like this (supplying root)

pyan3 ${PWD}/*.py --uses --no-defines --colored --dot --root ${PWD} > myuses.dot

You might still need to make the change on line 206 of pyan/main.py. Note that installing from the Github URI directly would (probably) make this manual code change unnecessary since the default branch should have the change already, though it seems PyPI doesn't have it yet.