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

Prevent a crash when transform lookups return an AssignAttr #350

Merged
merged 1 commit into from
Feb 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion pylint_django/transforms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,25 @@ def apply_type_shim(cls, _context=None): # noqa
# is an ImportFrom which has no qname() method, causing the checker
# to die...
if utils.PY3:
base_nodes = [n for n in base_nodes[1] if not isinstance(n, nodes.ImportFrom)]
base_nodes = [_valid_base_node(n, _context) for n in base_nodes[1]]
base_nodes = [n for n in base_nodes if n]
else:
base_nodes = list(base_nodes[1])

return iter([cls] + base_nodes)


def _valid_base_node(node, context):
"""Attempts to convert `node` to a valid base node, returns None if it cannot."""
if isinstance(node, nodes.AssignAttr):
inferred = next(node.parent.value.infer(context), None)
if inferred and isinstance(node, nodes.ClassDef):
return inferred
return None
if isinstance(node, nodes.ImportFrom):
return None
return node


def add_transforms(manager):
manager.register_transform(nodes.ClassDef, inference_tip(apply_type_shim), is_model_or_form_field)