On 05 octobre 21:12, Marc-Antoine Ruel wrote:
> Le 5 octobre 2010 17:14, Maarten ter Huurne <[email protected]> a écrit
> :
> 
> > On Tuesday 05 October 2010, Marc-Antoine Ruel wrote:
> >
> > > And that's the problem I see, astng sees "isinstance" as a normal
> > > CallFunc() node instead of a specialized node (?)
> >
> > "isinstance" is a built-in function, not a keyword:
> >
> > >>> isinstance
> > <built-in function isinstance>
> >
> > So I think astng is correct in returning a CallFunc node for it.
> >
> > Maybe you can check for the function name being "isinstance" and the
> > function module being "__builtin__"?
> >
> 
> My bad, you are right. I'm just no sure where the pattern matching code
> should go, basically I want to match the tree for this layout:
> 
>         Assert()
>             test =
>             CallFunc()
>                 func =
>                 Name(isinstance)
>                 args = [
>                 Name(x)
>                 Name(A)
>                 ]
>                 starargs =
>                 kwargs =
> 
> In that case, Name(isinstance) needs to confirm it's the __builtin__ version
> (how?) and Name(A) must be resolved to the type. The shortest hack would be
> to conceptually replace this form with the equivalent inference from "x =
> A()".
> 
> I wonder where this check should be.

I'm afraid all this would require some changes in astng to store such inference
tips. Currently, type inference works by navigating into the ast, also there
is no notion of "variable x". In the code

 1. assert isinstance(x, A)
 2. x.method()
 
astng sees both statements as unrelated (it only knows nodes of the ast). One
can use node.infer() (where node is a Name node for instance) to try to resolve
its possible values. This works basically by filtering assigments and resolving
them until we reach a "final" node like a typed constant, a class instance, etc.

So to make this work we should first store type annotation (extracted from 
detected calls to isinstance as proposed, python 3 annotations... during astng
building step), then detect and use them when infering.

You should first take a look at LocalsDictNodeNG / scope  lookup.


> If you have the time, please also let astng recognise this pattern:
> >
> > if not isinstance(arg1, SomeType):
> >        raise TypeError(type(arg1))
> > # Here arg1 is a SomeType.
> >
> 
> That makes sense but it looks slightly more complex to implement. From what
> I understand, you cannot scope type inference inside a frame.

If you dig you'll see that indeed astng has a somewhat "flat" representation
and is not well able to handle data flow...

-- 
Sylvain Thénault                               LOGILAB, Paris (France)
Formations Python, Debian, Méth. Agiles: http://www.logilab.fr/formations
Développement logiciel sur mesure:       http://www.logilab.fr/services
CubicWeb, the semantic web framework:    http://www.cubicweb.org

_______________________________________________
Python-Projects mailing list
[email protected]
http://lists.logilab.org/mailman/listinfo/python-projects

Reply via email to