-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from yalef/master
Add model checkers based on `astroid` nodes
- Loading branch information
Showing
23 changed files
with
803 additions
and
455 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,39 @@ | ||
import ast | ||
import astroid | ||
|
||
from .checker import Checker | ||
from .checker import AstroidBaseChecker | ||
from .utils import node_is_subclass | ||
|
||
|
||
class BaseModelChecker(Checker): | ||
class BaseModelChecker(AstroidBaseChecker): | ||
""" | ||
Base class for checkers that must lookup for Model like nodes. | ||
""" | ||
|
||
model_name_lookup = None | ||
def is_model(self, node): | ||
return node_is_subclass(node, self.model_name_lookups) | ||
|
||
@staticmethod | ||
def _is_abstract_and_set_to_true(element): | ||
def _is_abstract_and_set_to_true(node): | ||
return ( | ||
isinstance(element, ast.Assign) | ||
and any(target.id == 'abstract' for target in element.targets if isinstance(target, ast.Name)) | ||
and isinstance(element.value, ast.NameConstant) | ||
and element.value.value is True | ||
isinstance(node, astroid.Assign) | ||
and any( | ||
target.name == 'abstract' | ||
for target in node.targets | ||
if isinstance(target, astroid.AssignName) | ||
) | ||
and isinstance(node.value, astroid.Const) | ||
and node.value.value is True | ||
) | ||
|
||
def is_abstract_model(self, base): | ||
def is_abstract_model(self, node): | ||
""" | ||
Return True if AST node has a Meta class with abstract = True. | ||
Return True if astroid node has a Meta class with abstract = True. | ||
""" | ||
# look for "class Meta" | ||
for element in base.body: | ||
if isinstance(element, ast.ClassDef) and element.name == 'Meta': | ||
for element in node.body: | ||
if isinstance(element, astroid.ClassDef) and element.name == 'Meta': | ||
# look for "abstract = True" | ||
for inner_element in element.body: | ||
if self._is_abstract_and_set_to_true(inner_element): | ||
return True | ||
return False | ||
|
||
def is_model_name_lookup(self, base): | ||
""" | ||
Return True if class is defined as the respective model name lookup declaration | ||
""" | ||
return ( | ||
isinstance(base, ast.Name) and | ||
base.id == self.model_name_lookup | ||
) | ||
|
||
def is_models_name_lookup_attribute(self, base): | ||
""" | ||
Return True if class is defined as the respective model name lookup declaration | ||
""" | ||
return ( | ||
isinstance(base, ast.Attribute) and | ||
isinstance(base.value, ast.Name) and | ||
base.value.id == 'models' and base.attr == self.model_name_lookup | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.