foreignkey infer Getattr, use inference hint instead of node replacement #27
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
seeds.py
... 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:
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.