Usually tests using isinstance are preferable since subclasses of nx.Graph will pass as well:

    if isinstance(G, nx.Graph):