xflr6 / graphviz

Simple Python interface for Graphviz
https://graphviz.readthedocs.io
MIT License
1.63k stars 211 forks source link

Simple inheritance class fails when subgraph() is called #164

Closed youstanzr closed 2 years ago

youstanzr commented 2 years ago

I am trying to create a simple inheritance class to Graph but it seems to be failing when calling the subgraph()

code below:

import graphviz
class MyTree(graphviz.Graph):
    def __init__(self):
        super(graphviz.Graph, self).__init__()

tree = MyTree()
with tree.subgraph() as sub_tree:
    sub_tree.node('1', 'test')

tree.view()

Traceback below:

  File "/Users/myuser/Desktop/TreeGraph.py", line 7, in <module>
    with tree.subgraph() as sub_tree:
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/graphviz/_tools.py", line 172, in wrapper
    return func(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/graphviz/dot.py", line 314, in subgraph
    subgraph = self.__class__(**kwargs)
TypeError: __init__() got an unexpected keyword argument 'encoding'
xflr6 commented 2 years ago

super(graphviz.Graph, self).__init__()

You need to accept the same __init__ arguments and pass them on to the base class (otherwise calls that use arguments won't work because your subclass is not compatible with the base class):

import graphviz

class MyGraph(graphviz.Graph):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

g = MyGraph()
with g.subgraph() as s:
    s.node('spam')
g

spam