本文整理汇总了Python中pylint_django.utils.node_is_subclass函数的典型用法代码示例。如果您正苦于以下问题:Python node_is_subclass函数的具体用法?Python node_is_subclass怎么用?Python node_is_subclass使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了node_is_subclass函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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()
开发者ID:mbarrien,项目名称:pylint-django,代码行数:30,代码来源:__init__.py
示例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)
开发者ID:jproffitt,项目名称:pylint-django,代码行数:33,代码来源:models.py
示例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])
开发者ID:jproffitt,项目名称:pylint-django,代码行数:8,代码来源:__init__.py
示例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])
开发者ID:mbarrien,项目名称:pylint-django,代码行数:9,代码来源:__init__.py
示例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)
开发者ID:ocadotechnology,项目名称:pylint-django,代码行数:11,代码来源:__init__.py
示例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)
开发者ID:Coldwings,项目名称:pylint-django,代码行数:54,代码来源:models.py
示例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])
开发者ID:mbarrien,项目名称:pylint-django,代码行数:11,代码来源:__init__.py
示例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)
开发者ID:OscaTutenchamon,项目名称:pylint-django,代码行数:12,代码来源:__init__.py
示例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()
开发者ID:OscaTutenchamon,项目名称:pylint-django,代码行数:53,代码来源:__init__.py
示例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
开发者ID:jproffitt,项目名称:pylint-django,代码行数:13,代码来源:__init__.py
示例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])
开发者ID:rinkurajole,项目名称:appointment-system,代码行数:14,代码来源:__init__.py
示例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)
开发者ID:Meixin-Finance,项目名称:pylint-django,代码行数:14,代码来源:__init__.py
示例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
开发者ID:OscaTutenchamon,项目名称:pylint-django,代码行数:16,代码来源:__init__.py
示例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
开发者ID:OscaTutenchamon,项目名称:pylint-django,代码行数:17,代码来源:__init__.py
示例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)
开发者ID:OscaTutenchamon,项目名称:pylint-django,代码行数:17,代码来源:__init__.py
示例16: is_class
def is_class(class_name):
"""Shortcut for node_is_subclass."""
return lambda node: node_is_subclass(node, class_name)
开发者ID:OscaTutenchamon,项目名称:pylint-django,代码行数:3,代码来源:__init__.py
示例17: is_model_test_case_subclass
def is_model_test_case_subclass(node):
"""Checks that node is derivative of TestCase class."""
if not node.name.endswith('Test') and not isinstance(node.parent, ClassDef):
return False
return node_is_subclass(node, 'django.test.testcases.TestCase')
开发者ID:OscaTutenchamon,项目名称:pylint-django,代码行数:6,代码来源:__init__.py
示例18: is_model_admin_subclass
def is_model_admin_subclass(node):
"""Checks that node is derivative of ModelAdmin class."""
if node.name[-5:] != 'Admin' or isinstance(node.parent, ClassDef):
return False
return node_is_subclass(node, 'django.contrib.admin.options.ModelAdmin')
开发者ID:OscaTutenchamon,项目名称:pylint-django,代码行数:6,代码来源:__init__.py
示例19: 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.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, Getattr):
func_name = getattr(node.parent, 'attrname', None)
# Note: it would have been nice to import the Manager object from Django and
# get its attributes that way - and this used to be the method - but unfortunately
# there's no guarantee that Django is properly configured at that stage, and importing
# anything from the django.db package causes an ImproperlyConfigured exception.
# Therefore we'll fall back on a hard-coded list of attributes which won't be as accurate,
# but this is not 100% accurate anyway.
manager_attrs = (
'none',
'all',
'count',
'dates',
'distinct',
'extra',
'get',
'get_or_create',
'create',
'bulk_create',
'filter',
'aggregate',
'annotate',
'complex_filter',
'exclude',
'in_bulk',
'iterator',
'latest',
'order_by',
'select_for_update',
'select_related',
'prefetch_related',
'values',
'values_list',
'update',
'reverse',
'defer',
'only',
'using',
'exists',
)
if func_name in manager_attrs:
quack = True
if quack:
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()
开发者ID:ocadotechnology,项目名称:pylint-django,代码行数:90,代码来源:__init__.py
示例20: is_class
def is_class(class_name):
return lambda node: node_is_subclass(node, class_name)
开发者ID:jproffitt,项目名称:pylint-django,代码行数:2,代码来源:__init__.py
注:本文中的pylint_django.utils.node_is_subclass函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论