assigning node type with function annotations?
Hello Been using astroid extensively this past week, really enjoying it! Great work! What's the best way I can deliberately set the type of an argument, based on the function annotations?
On Tue, Apr 28, 2015 at 9:03 AM, Jarrad Hope <me@jarradhope.com> wrote:
Hello
Been using astroid extensively this past week, really enjoying it! Great work!
Thank you.
What's the best way I can deliberately set the type of an argument, based on the function annotations?
Currently we don't have any mechanism for this (but it is something that we want). One way to do it would be to add a Transform in astroid.brain for Function nodes, which checks that it has annotations, infers them and replaces the default inference with that one.
This is how far I got with this, https://gist.github.com/jarradh/02dd8f5ad07527c9fd10 In the example go through Arguments(i imagine I'll have to apply transform on functiondef later) and apply an explicit transformation, and I manage to apply a Const type to all of them but I got stuck at how to switch based on node._annotype.name since Const instantiates with the name as a value and therefore it's a string type. How can I switch the Const type based on node._annotype.name ? Or is there a better way of going about this? On Tue, Apr 28, 2015 at 1:55 PM, Claudiu Popa <pcmanticore@gmail.com> wrote:
On Tue, Apr 28, 2015 at 9:03 AM, Jarrad Hope <me@jarradhope.com> wrote:
Hello
Been using astroid extensively this past week, really enjoying it! Great work!
Thank you.
What's the best way I can deliberately set the type of an argument, based on the function annotations?
Currently we don't have any mechanism for this (but it is something that we want). One way to do it would be to add a Transform in astroid.brain for Function nodes, which checks that it has annotations, infers them and replaces the default inference with that one.
On Thu, May 7, 2015 at 6:02 PM, Jarrad Hope <me@jarradhope.com> wrote:
This is how far I got with this, https://gist.github.com/jarradh/02dd8f5ad07527c9fd10
In the example go through Arguments(i imagine I'll have to apply transform on functiondef later) and apply an explicit transformation, and I manage to apply a Const type to all of them but I got stuck at how to switch based on node._annotype.name since Const instantiates with the name as a value and therefore it's a string type.
How can I switch the Const type based on node._annotype.name ? Or is there a better way of going about this?
You'll have to infer node._annontype.name (which can be a Class) and get its name. Then retrieving the name from builtins and try to go from there. Something along these lines: import builtins type = next(node._annotype.infer()) const = Const(vars(builtins)[type.name]())
Ahh thank-you so much! I ended up with this so I can define list, seq, dict, etc def argument_annotation_inference(node, context=None): _type = next(node._annotype.infer()) klass = vars(builtins)[_type.name] return iter([CONST_CLS[klass](klass())]) On Thu, May 7, 2015 at 10:39 PM, Claudiu Popa <pcmanticore@gmail.com> wrote:
On Thu, May 7, 2015 at 6:02 PM, Jarrad Hope <me@jarradhope.com> wrote:
This is how far I got with this, https://gist.github.com/jarradh/02dd8f5ad07527c9fd10
In the example go through Arguments(i imagine I'll have to apply transform on functiondef later) and apply an explicit transformation, and I manage to apply a Const type to all of them but I got stuck at how to switch based on node._annotype.name since Const instantiates with the name as a value and therefore it's a string type.
How can I switch the Const type based on node._annotype.name ? Or is there a better way of going about this?
You'll have to infer node._annontype.name (which can be a Class) and get its name. Then retrieving the name from builtins and try to go from there. Something along these lines:
import builtins
type = next(node._annotype.infer()) const = Const(vars(builtins)[type.name]())
participants (2)
-
Claudiu Popa
-
Jarrad Hope