Skip to content

Commit 2815db7

Browse files
committed
[refs #263] Add initial transform code for text choices
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
1 parent 26f7064 commit 2815db7

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

pylint_django/transforms/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
import astroid
1515

16-
from pylint_django.transforms import fields
16+
from pylint_django.transforms import fields, text_choices
1717

1818
fields.add_transforms(astroid.MANAGER)
19-
19+
text_choices.add_transforms(astroid.MANAGER)
2020

2121
def _add_transform(package_name):
2222
def fake_module_builder():
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from astroid import MANAGER, scoped_nodes, nodes, inference_tip
2+
3+
from pylint_django import utils
4+
5+
class_names = set()
6+
7+
def is_tuple_in_text_choices(node):
8+
# file_name = node.root().file
9+
# if file_name not in file_names:
10+
# file_names.update({file_name})
11+
# import ipdb; ipdb.set_trace()
12+
# else:
13+
# return False
14+
15+
if not (isinstance(node.parent, nodes.Assign)
16+
or isinstance(node.parent, nodes.FunctionDef)) :
17+
return False
18+
if not isinstance(node.parent.parent, nodes.ClassDef):
19+
return False
20+
if "TextChoices" in [i.name for i in node.parent.parent.bases]:
21+
import ipdb; ipdb.set_trace()
22+
23+
class_name = node.parent.parent.name
24+
if class_name not in class_names:
25+
class_names.update({class_name})
26+
# import ipdb; ipdb.set_trace()
27+
else:
28+
return False
29+
30+
# if node.parent.parent.name == "SomeTextChoices":
31+
# import ipdb; ipdb.set_trace()
32+
# else:
33+
# return False
34+
35+
# if node.parent.parent.parent.name in ["TextChoices", "SomeClass", "ChoicesMeta", "TextChoices"]:
36+
# import ipdb; ipdb.set_trace()
37+
# else:
38+
# return False
39+
40+
# if node.root().file.endswith("enums.py"):
41+
# import ipdb; ipdb.set_trace()
42+
# else:
43+
# return False
44+
45+
# try:
46+
# if node.root().file.endswith("model_enum.py"):
47+
# import ipdb; ipdb.set_trace()
48+
# except:
49+
# import ipdb; ipdb.set_trace()
50+
# if (node.parent.parent.name != "LazyObject"
51+
# and node.parent.parent.parent.name != "django.db.models.expressions"
52+
# and node.parent.parent.parent.name != "django.db.models.fields"):
53+
# import ipdb; ipdb.set_trace()
54+
55+
if isinstance(node.func, nodes.Attribute):
56+
attr = node.func.attrname
57+
elif isinstance(node.func, nodes.Name):
58+
attr = node.func.name
59+
else:
60+
return False
61+
return True
62+
63+
64+
def infer_key_classes(node, context=None):
65+
pass
66+
67+
68+
def add_transforms(manager):
69+
manager.register_transform(nodes.Call, inference_tip(infer_key_classes),
70+
is_tuple_in_text_choices)

0 commit comments

Comments
 (0)