Open chedv opened 2 years ago
That's a good point! Good catch, @chedv !
So I think when I wrote it - I was expecting that if you write a custom resolver function, you use a 'KeyError
' to say 'Not a valid name - but hey simpleeval - go check functions
. Maybe they can help' kind of thing.
Which is ... not really defined behaviour.
Maybe a couple of custom exceptions would be the better way to do this?
So if the custom resolver raises NameNotDefinedCheckFunctions
- then it would go on to check in the functions
as before - but if it raises NameNotDefined
- then it bypasses it, and any other exception gets raises directly...
Thanks for your response, @danthedeckie. When I apply functions to resolve variable names, I may use a complicated implementation with my own dictionaries and if I face a bug in my code that causes KeyError
, it will confuse me with the NameNotDefined
message instead of the root cause. Moreover, if it confuses end users, it will be much worse.
So I agree that a custom exception would be better instead of using standard Python exceptions. But also it would be good to mention the feature in the documentation.
See the betters-names-exceptions
branch...
Here is the simplest example:
Output:
But if I use, for example,
Exception
instead ofKeyError
, I have only raised Exception. That's because of the following logic in the library:try ... except KeyError
block is used on the entire function instead of just handlingreturn self.names[node.id]