Skip to content

Commit

Permalink
[refs #263] Add initial transform code for text choices
Browse files Browse the repository at this point in the history
Why:

- Transform tuples to include value and label properties

This change addresses the need by:

- Adding the text_choices transform file
- Integrating in the transforms init file
- Focusing on the predicate for the rule to trigger the transform
  • Loading branch information
moritz89 committed Jan 14, 2021
1 parent 26f7064 commit 2815db7
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pylint_django/transforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

import astroid

from pylint_django.transforms import fields
from pylint_django.transforms import fields, text_choices

fields.add_transforms(astroid.MANAGER)

text_choices.add_transforms(astroid.MANAGER)

def _add_transform(package_name):
def fake_module_builder():
Expand Down
70 changes: 70 additions & 0 deletions pylint_django/transforms/text_choices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from astroid import MANAGER, scoped_nodes, nodes, inference_tip

from pylint_django import utils

class_names = set()

def is_tuple_in_text_choices(node):
# file_name = node.root().file
# if file_name not in file_names:
# file_names.update({file_name})
# import ipdb; ipdb.set_trace()
# else:
# return False

if not (isinstance(node.parent, nodes.Assign)
or isinstance(node.parent, nodes.FunctionDef)) :
return False
if not isinstance(node.parent.parent, nodes.ClassDef):
return False
if "TextChoices" in [i.name for i in node.parent.parent.bases]:
import ipdb; ipdb.set_trace()

class_name = node.parent.parent.name
if class_name not in class_names:
class_names.update({class_name})
# import ipdb; ipdb.set_trace()
else:
return False

# if node.parent.parent.name == "SomeTextChoices":
# import ipdb; ipdb.set_trace()
# else:
# return False

# if node.parent.parent.parent.name in ["TextChoices", "SomeClass", "ChoicesMeta", "TextChoices"]:
# import ipdb; ipdb.set_trace()
# else:
# return False

# if node.root().file.endswith("enums.py"):
# import ipdb; ipdb.set_trace()
# else:
# return False

# try:
# if node.root().file.endswith("model_enum.py"):
# import ipdb; ipdb.set_trace()
# except:
# import ipdb; ipdb.set_trace()
# if (node.parent.parent.name != "LazyObject"
# and node.parent.parent.parent.name != "django.db.models.expressions"
# and node.parent.parent.parent.name != "django.db.models.fields"):
# import ipdb; ipdb.set_trace()

if isinstance(node.func, nodes.Attribute):
attr = node.func.attrname
elif isinstance(node.func, nodes.Name):
attr = node.func.name
else:
return False
return True


def infer_key_classes(node, context=None):
pass


def add_transforms(manager):
manager.register_transform(nodes.Call, inference_tip(infer_key_classes),
is_tuple_in_text_choices)

0 comments on commit 2815db7

Please sign in to comment.