-
-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow registering new visit_xxx
for SourceGenerator
#113
Labels
Comments
Thanks for the report. What do you think about adding a API example: class CustomSourceGenerator(SourceGenerator):
def visit_Spam(self, node):
...
astor.to_source(node, source_generator_class=CustomSourceGenerator) |
matham
added a commit
to matham/astor
that referenced
this issue
Jun 28, 2018
Your idea sounds perfect - I added a PR for that: #114. |
berkerpeksag
pushed a commit
to matham/astor
that referenced
this issue
May 19, 2019
berkerpeksag
pushed a commit
that referenced
this issue
May 19, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I ran into an issue where I defined a custom
AST
node, and when I tried rendering the source withastor.to_source(node)
, it crashed sayingNo defined handler for node of type xxx
.I understand that
astor
would not know how to render some unknownAST
node, but it would be nice if we could register a visit function, such that whenastor
encounters a unknownAST
node, instead of crashing it checks the registry for the visit function.There are at least two reasons for having a custom
AST
class:AST
class.AST
class, e.g. aAttribute
. Becauseobj.__class__.__name__
is used, even though it inherits from some knownAST
class,astor
does not know what to do with it.I understand why the first use case would not be convincing. But number two should be common enough that allowing to register one
AST
class name as an alias for another, or alternatively allowing to register an explicit visit function would be useful (or both).My use case is as follows: I do the tree walk and then replace some nodes with a proxy node. The proxy node refers to the original node. However, later at my convenience, I replace the node the proxy refers to with another node. This allows me to switch out nodes without having to walk the tree. Currently, I monkey patched
astor
to be able to handle this as follows:But, it would be nice if there was an official way of doing this. I can work on a PR if that sounds acceptable.
The text was updated successfully, but these errors were encountered: