berkerpeksag / astor

Python AST read/write
https://pypi.org/project/astor/
BSD 3-Clause "New" or "Revised" License
803 stars 102 forks source link

No defined handler for some Expression nodes #147

Closed greenmonn closed 4 years ago

greenmonn commented 5 years ago

Hi, I'm using astor for manipulating AST nodes from target source file.

When I try to call astor.to_source with only the AST node of compare operator, such as GtE, Gt, LtE, Lt, etc, I faced to the error No defined handler for node of type GtE (or others).

I'm wondered if you have any plan to support these types of nodes.

Thank you! #

berkerpeksag commented 5 years ago

You can subclass astor.astor.SourceGenerator, implement visit_ methods for those nodes, and then pass it as source_generator_class=YourCustomGenerator to astor.to_source(). Would that cover your use case?

jeybrahms commented 4 years ago

currently, version 0.8, there is a minor bug in to_source() that gets in a way:

    if source_generator_class is None:
        source_generator_class = SourceGenerator
    elif not isinstance(source_generator_class, SourceGenerator):
        raise TypeError('source_generator_class should be a subclass of SourceGenerator')

The isinstance() should have been issubclass()

berkerpeksag commented 4 years ago

@jeybrahms thanks for the report! It has been fixed in https://github.com/berkerpeksag/astor/commit/51c9e70f1b3a8877cb531ab9c5b1a814eefe7532.

@greenmonn please reopen if using my suggestion wouldn't solve your problem.