Skip to content
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

foreignkey infer Getattr, use inference hint instead of node replacement #27

Merged
merged 1 commit into from
Sep 29, 2014

Conversation

mbarrien
Copy link
Contributor

The foreign key transform currently only works in cases where the Model class being pointed to is directly referenced, 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)  # instead of 'ForeignKey(Fruit)'
    def get_fruit_name(self):
        return self.fruit.fruit_name

... 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.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.4%) when pulling 9c32bfa on mbarrien:foreignkey-infer into 0dd642c on landscapeio:master.

@carlio
Copy link
Collaborator

carlio commented Sep 29, 2014

Ah brilliant. I wasn't aware of the inference hint stuff.

This also coincidentally fixes a bug I was just investigating - errors like this don't happen any more:

 438, in _collect_block_lines
    for child in node.get_children():
TypeError: '_Yes' object is not iterable

carlio added a commit that referenced this pull request Sep 29, 2014
foreignkey infer Getattr, use inference hint instead of node replacement
@carlio carlio merged commit 6c31f07 into pylint-dev:master Sep 29, 2014
@mbarrien mbarrien deleted the foreignkey-infer branch September 29, 2014 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants