AttributeError: 'Unparser' object has no attribute '_str'. Did you mean: '_Str'? pyflowchart 0.3.1 with python 3.11 and astunparse 1.6.3
As pyflowchart 0.3.1 used with python 3.11 and astunparse 1.6.3 throws an exception:
Traceback (most recent call last):
File "C:\Downloads\sc-navigator-model-generation-cp\APO Input Workflow\src\flowchart.py", line 12, in <module>
fc = pfc.parse(code)
^^^^^^^^^^^^^^^
File "C:\Downloads\sc-navigator-model-generation-cp\APO Input Workflow\src\new_env\Lib\site-packages\pyflowchart\ast_node.py", line 659, in parse
node = ast_node_class(ast_object, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Downloads\sc-navigator-model-generation-cp\APO Input Workflow\src\new_env\Lib\site-packages\pyflowchart\ast_node.py", line 463, in __init__
OperationNode.__init__(self, operation=self.ast_to_source())
^^^^^^^^^^^^^^^^^^^^
File "C:\Downloads\sc-navigator-model-generation-cp\APO Input Workflow\src\new_env\Lib\site-packages\pyflowchart\ast_node.py", line 33, in ast_to_source
return astunparse.unparse(self.ast_object).strip()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Downloads\sc-navigator-model-generation-cp\APO Input Workflow\src\new_env\Lib\site-packages\astunparse\__init__.py", line 13, in unparse
Unparser(tree, file=v)
File "C:\Downloads\sc-navigator-model-generation-cp\APO Input Workflow\src\new_env\Lib\site-packages\astunparse\unparser.py", line 38, in __init__
self.dispatch(tree)
File "C:\Downloads\sc-navigator-model-generation-cp\APO Input Workflow\src\new_env\Lib\site-packages\astunparse\unparser.py", line 65, in dispatch
meth = getattr(self, "_"+tree.__class__.__name__)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Unparser' object has no attribute '_str'. Did you mean: '_Str'?
How do I resolve this issue?
Originally posted by @kaasima10 in https://github.com/cdfmlr/pyflowchart/issues/28#issuecomment-2101265384
@kaasima10 I have tested pyflowchart 0.3.1 with python 3.11 and astunparse 1.6.3 as you described, on some basic cases I tried, it works fine. Would you mind to post your inputted python source code and command executed that helps reproduce this problem?
this is my python code :-
import pyflowchart as pfc import inspect
def example_function(x): if x > 0: return 'Positive' elif x < 0: return 'Negative' else: return 'Zero'
code = inspect.getsource(example_function) fc = pfc.parse(code) flowchart = fc.flowchart() print(flowchart)
@cdfmlr I encountered an AttributeError when attempting to convert a simple Python function to a flowchart using the pyflowchart library. The function is straightforward, but the error occurs during the conversion process.
Thanks. Have figured it out.
You should use:
from pyflowchart import Flowchart
...
fc = Flowchart.from_code(code)
...
instead of:
import pyflowchart as pfc
...
fc = pfc.parse(code)
...
As described in our document section Python to Flowchart, Flowchart.from_code is the right method you are expected to converts python source code from a str to the flowchart:
https://github.com/cdfmlr/pyflowchart/blob/dcb5128ac34a8ac76676877fcde17613b00704be/pyflowchart/flowchart.py#L42-L56
Meanwhile, parse is an internal function that parses the abstract syntax trees of python codes, instead of a str of python source code:
https://github.com/cdfmlr/pyflowchart/blob/dcb5128ac34a8ac76676877fcde17613b00704be/pyflowchart/ast_node.py#L929-L943
Users shall never call this function from the out of the package.