landscapeio/pylint-django — pull request #27 by mbarrien (2014-09-27 20:54:51+00:00)
Title: foreignkey infer Getattr, use inference hint instead of node replacement

The foreign key transform currently only works in cases where the Model class being pointed to is directly pointed to, not in cases where it is looked up in a module. For example if you have...

foods.py
```
from django.db import models
class Fruit(models.Model):
fruit_name = models.CharField(max_length=20)
```

seeds.py
```
import foods
from django.db import models
class Seed(models.Model):
fruit = ForeignKey(foods.Fruit)

```

... the current foreignkey transformer will *not* do the transform because Astroid has the first argument of the ForeignKey CallFunc as a GetAttr node, not a Name node as is currently hardcoded. Thus, we continue to get "ForeignKey has no field 'fruit_name'" errors.

This pull request does 2 things:
* Support inference on GetAttr nodes.
* Changes the strategy for making pylint understand the type to use an inference hint instead of completely replacing the node.

For the inference hint, via astroid.inference_tip, this is the same strategy used internally with astroid for things like namedtuples, e.g. https://bitbucket.org/logilab/astroid/src/e7b2aa43f1c61c7242089171bb4c407aff3fad25/brain/py2stdlib.py?at=default#cl-333

Performing inference this way instead of through completely replacing the node allows astroid/pylint to possibly do more analysis on the other nodes within the assignment, e.g. the other arguments to ForeignKey, while still allowing us to augment the type information via our own function. Another plus is that there seems to be more context passed in to the infer() call at that point rather than at the original transform time.

Unfortunately, because type inference is done at the CallFunc level and not the Assign level (infer() doesn't seem to be called on Assign nodes), I had to bump the transform registration down one level to CallFunc instead of assign, and add more ".parent" lookups when determining if we are inside a class.