本文整理汇总了Python中pylint_django.utils.node_is_subclass函数的典型用法代码示例。如果您正苦于以下问题:Python node_is_subclass函数的具体用法?Python node_is_subclass怎么用?Python node_is_subclass使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了node_is_subclass函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: foreign_key_sets
def foreign_key_sets(chain, node):
"""
When a Django model has a ForeignKey to another model, the target
of the foreign key gets a '<modelname>_set' attribute for accessing
a queryset of the model owning the foreign key - eg:
class ModelA(models.Model):
pass
class ModelB(models.Model):
a = models.ForeignKey(ModelA)
Now, ModelA instances will have a modelb_set attribute.
"""
if node.attrname.endswith('_set'):
children = list(node.get_children())
for child in children:
try:
inferred = child.infered()
except InferenceError:
pass
else:
for cls in inferred:
if node_is_subclass(cls, 'django.db.models.base.Model'):
# This means that we are looking at a subclass of models.Model
# and something is trying to access a <something>_set attribute.
# Since this could exist, we will return so as not to raise an
# error.
return
chain()
示例2: visit_class
def visit_class(self, node):
if not node_is_subclass(node, 'django.db.models.base.Model'):
# we only care about models
return
for child in node.get_children():
if isinstance(child, Assign):
grandchildren = list(child.get_children())
if not isinstance(grandchildren[0], AssName):
continue
name = grandchildren[0].name
if name != '__unicode__':
continue
assigned = grandchildren[1].infered()[0]
if assigned.callable():
return
self.add_message('E%s01' % BASE_ID, args=node.name, node=node)
return
if isinstance(child, Function) and child.name == '__unicode__':
if sys.version_info[0] >= 3:
self.add_message('W%s02' % BASE_ID, args=node.name, node=node)
return
# if we get here, then we have no __unicode__ method
if sys.version_info[0] >= 3:
return
self.add_message('W%s01' % BASE_ID, args=node.name, node=node)
示例3: is_model_meta_subclass
def is_model_meta_subclass(node):
if node.name != 'Meta' or not isinstance(node.parent, Class):
return False
parents = ('django.db.models.base.Model',
'django.forms.forms.Form',
'django.forms.models.ModelForm')
return any([node_is_subclass(node.parent, parent) for parent in parents])
示例4: is_model_meta_subclass
def is_model_meta_subclass(node):
"""Checks that node is derivative of Meta class."""
if node.name != 'Meta' or not isinstance(node.parent, Class):
return False
parents = ('django.db.models.base.Model',
'django.forms.forms.Form',
'django.forms.models.ModelForm')
return any([node_is_subclass(node.parent, parent) for parent in parents])
示例5: is_model_view_subclass_method_shouldnt_be_function
def is_model_view_subclass_method_shouldnt_be_function(node):
"""Checks that node is get or post method of the View class."""
if node.name not in ('get', 'post'):
return False
parent = node.parent
while parent and not isinstance(parent, ScopedClass):
parent = parent.parent
subclass = '.View'
return parent is not None and parent.name.endswith('View') and node_is_subclass(parent, subclass)
示例6: _visit_classdef
def _visit_classdef(self, node):
"""Class visitor."""
if not node_is_subclass(node, 'django.db.models.base.Model', '.Model'):
# we only care about models
return
for child in node.get_children():
if _is_meta_with_abstract(child):
return
if isinstance(child, Assign):
grandchildren = list(child.get_children())
if not isinstance(grandchildren[0], AssignName):
continue
name = grandchildren[0].name
if name != '__unicode__':
continue
grandchild = grandchildren[1]
assigned = inferred(grandchild)()[0]
if assigned.callable():
return
self.add_message('E%s01' % BASE_ID, args=node.name, node=node)
return
if isinstance(child, FunctionDef) and child.name == '__unicode__':
if PY3:
self.add_message('W%s02' % BASE_ID, args=node.name, node=node)
return
# if we get here, then we have no __unicode__ method directly on the class itself
if PY3:
return
# a different warning is emitted if a parent declares __unicode__
for method in node.methods():
if method.name == '__unicode__':
# this happens if a parent declares the unicode method but
# this node does not
self.add_message('W%s03' % BASE_ID, args=node.name, node=node)
return
# if the Django compatibility decorator is used then we don't emit a warning
# see https://github.com/landscapeio/pylint-django/issues/10
if node.decorators is not None:
for decorator in node.decorators.nodes:
if getattr(decorator, 'name', None) == 'python_2_unicode_compatible':
return
self.add_message('W%s01' % BASE_ID, args=node.name, node=node)
示例7: is_model_media_subclass
def is_model_media_subclass(node):
"""Checks that node is derivative of Media class."""
if node.name != 'Media' or not isinstance(node.parent, Class):
return False
parents = ('django.contrib.admin.options.ModelAdmin',
'django.forms.widgets.Media',
'django.db.models.base.Model',
'django.forms.forms.Form',
'django.forms.models.ModelForm')
return any([node_is_subclass(node.parent, parent) for parent in parents])
示例8: is_model_mpttmeta_subclass
def is_model_mpttmeta_subclass(node):
"""Checks that node is derivative of MPTTMeta class."""
if node.name != 'MPTTMeta' or not isinstance(node.parent, ClassDef):
return False
parents = ('django.db.models.base.Model',
'.Model', # for the transformed version used in this plugin
'django.forms.forms.Form',
'.Form',
'django.forms.models.ModelForm',
'.ModelForm')
return node_is_subclass(node.parent, *parents)
示例9: foreign_key_sets
def foreign_key_sets(chain, node):
"""
When a Django model has a ForeignKey to another model, the target
of the foreign key gets a '<modelname>_set' attribute for accessing
a queryset of the model owning the foreign key - eg:
class ModelA(models.Model):
pass
class ModelB(models.Model):
a = models.ForeignKey(ModelA)
Now, ModelA instances will have a modelb_set attribute.
It's also possible to explicitly name the relationship using the related_name argument
to the ForeignKey constructor. As it's impossible to know this without inspecting all
models before processing, we'll instead do a "best guess" approach and see if the attribute
being accessed goes on to be used as a queryset. This is via 'duck typing': if the method
called on the attribute being accessed is something we might find in a queryset, we'll
warn.
"""
quack = False
if node.attrname in MANAGER_ATTRS or node.attrname.endswith('_set'):
# if this is a X_set method, that's a pretty strong signal that this is the default
# Django name, rather than one set by related_name
quack = True
else:
# we will
if isinstance(node.parent, Attribute):
func_name = getattr(node.parent, 'attrname', None)
if func_name in MANAGER_ATTRS:
quack = True
if quack:
children = list(node.get_children())
for child in children:
try:
inferred_cls = inferred(child)()
except InferenceError:
pass
else:
for cls in inferred_cls:
if (node_is_subclass(cls,
'django.db.models.manager.Manager',
'django.db.models.base.Model',
'.Model')):
# This means that we are looking at a subclass of models.Model
# and something is trying to access a <something>_set attribute.
# Since this could exist, we will return so as not to raise an
# error.
return
chain()
示例10: is_model_field_display_method
def is_model_field_display_method(node):
if not node.attrname.endswith('_display'):
return
if not node.attrname.startswith('get_'):
return
if node.last_child():
# TODO: could validate the names of the fields on the model rather than
# blindly accepting get_*_display
for cls in node.last_child().infered():
if node_is_subclass(cls, 'django.db.models.base.Model'):
return True
return False
示例11: is_model_meta_subclass
def is_model_meta_subclass(node):
"""Checks that node is derivative of Meta class."""
if node.name != 'Meta' or not isinstance(node.parent, Class):
return False
parents = ('django.db.models.base.Model',
'django.forms.forms.Form',
'django.forms.models.ModelForm',
'rest_framework.serializers.ModelSerializer',
'rest_framework.generics.GenericAPIView',
'rest_framework.viewsets.ReadOnlyModelViewSet',
'rest_framework.viewsets.ModelViewSet',
'django_filters.filterset.FilterSet',)
return any([node_is_subclass(node.parent, parent) for parent in parents])
示例12: is_model_media_subclass
def is_model_media_subclass(node):
"""Checks that node is derivative of Media class."""
if node.name != 'Media' or not isinstance(node.parent, ClassDef):
return False
parents = ('django.contrib.admin.options.ModelAdmin',
'django.forms.widgets.Media',
'django.db.models.base.Model',
'.Model', # for the transformed version used in this plugin
'django.forms.forms.Form',
'.Form',
'django.forms.models.ModelForm',
'.ModelForm')
return node_is_subclass(node.parent, *parents)
示例13: _attribute_is_magic
def _attribute_is_magic(node, attrs, parents):
"""Checks that node is an attribute used inside one of allowed parents"""
if node.attrname not in attrs:
return False
if not node.last_child():
return False
try:
for cls in inferred(node.last_child())():
if isinstance(cls, Super):
cls = cls._self_class
if node_is_subclass(cls, *parents) or cls.qname() in parents:
return True
except InferenceError:
pass
return False
示例14: is_model_field_display_method
def is_model_field_display_method(node):
"""Accept model's fields with get_*_display names."""
if not node.attrname.endswith('_display'):
return
if not node.attrname.startswith('get_'):
return
if node.last_child():
# TODO: could validate the names of the fields on the model rather than
# blindly accepting get_*_display
try:
for cls in inferred(node.last_child())():
if node_is_subclass(cls, 'django.db.models.base.Model', '.Model'):
return True
except InferenceError:
return False
return False
示例15: is_model_meta_subclass
def is_model_meta_subclass(node):
"""Checks that node is derivative of Meta class."""
if node.name != 'Meta' or not isinstance(node.parent, ClassDef):
return False
parents = ('.Model', # for the transformed version used here
'django.db.models.base.Model',
'.Form',
'django.forms.forms.Form',
'.ModelForm',
'django.forms.models.ModelForm',
'rest_framework.serializers.ModelSerializer',
'rest_framework.generics.GenericAPIView',
'rest_framework.viewsets.ReadOnlyModelViewSet',
'rest_framework.viewsets.ModelViewSet',
'django_filters.filterset.FilterSet',)
return node_is_subclass(node.parent, *parents)