晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。   林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。   见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝)   既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。   南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。 sh-3ll

HOME


sh-3ll 1.0
DIR:/opt/cloudlinux/venv/lib64/python3.11/site-packages/pylint_django/checkers/
Upload File :
Current File : //opt/cloudlinux/venv/lib64/python3.11/site-packages/pylint_django/checkers/models.py
"""Models."""
from astroid import Const
from astroid.nodes import Assign, AssignName, ClassDef, FunctionDef
from pylint.checkers import BaseChecker
from pylint.checkers.utils import check_messages
from pylint.interfaces import IAstroidChecker

from pylint_django.__pkginfo__ import BASE_ID
from pylint_django.utils import PY3, node_is_subclass

MESSAGES = {
    f"E{BASE_ID}01": (
        "__unicode__ on a model must be callable (%s)",
        "model-unicode-not-callable",
        "Django models require a callable __unicode__ method",
    ),
    f"W{BASE_ID}01": (
        "No __unicode__ method on model (%s)",
        "model-missing-unicode",
        "Django models should implement a __unicode__ method for string representation",
    ),
    f"W{BASE_ID}02": (
        "Found __unicode__ method on model (%s). Python3 uses __str__.",
        "model-has-unicode",
        "Django models should not implement a __unicode__ method for string representation when using Python3",
    ),
    f"W{BASE_ID}03": (
        "Model does not explicitly define __unicode__ (%s)",
        "model-no-explicit-unicode",
        "Django models should implement a __unicode__ method for string representation. "
        "A parent class of this model does, but ideally all models should be explicit.",
    ),
}


def _is_meta_with_abstract(node):
    if isinstance(node, ClassDef) and node.name == "Meta":
        for meta_child in node.get_children():
            if not isinstance(meta_child, Assign):
                continue
            if not meta_child.targets[0].name == "abstract":
                continue
            if not isinstance(meta_child.value, Const):
                continue
                # TODO: handle tuple assignment?
            # eg:
            #    abstract, something_else = True, 1
            if meta_child.value.value:
                # this class is abstract
                return True
    return False


def _has_python_2_unicode_compatible_decorator(node):
    if node.decorators is None:
        return False

    for decorator in node.decorators.nodes:
        if getattr(decorator, "name", None) == "python_2_unicode_compatible":
            return True

    return False


def _is_unicode_or_str_in_python_2_compatibility(method):
    if method.name == "__unicode__":
        return True

    if method.name == "__str__" and _has_python_2_unicode_compatible_decorator(method.parent):
        return True

    return False


class ModelChecker(BaseChecker):
    """Django model checker."""

    __implements__ = IAstroidChecker

    name = "django-model-checker"
    msgs = MESSAGES

    @check_messages("model-missing-unicode")
    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 = grandchild.inferred()[0]

                if assigned.callable():
                    return

                self.add_message(f"E{BASE_ID}01", args=node.name, node=node)
                return

            if isinstance(child, FunctionDef) and child.name == "__unicode__":
                if PY3:
                    self.add_message(f"W{BASE_ID}02", args=node.name, node=node)
                return

        # if we get here, then we have no __unicode__ method directly on the class itself

        # a different warning is emitted if a parent declares __unicode__
        for method in node.methods():
            if method.parent != node and _is_unicode_or_str_in_python_2_compatibility(method):
                # this happens if a parent declares the unicode method but
                # this node does not
                self.add_message(f"W{BASE_ID}03", args=node.name, node=node)
                return

        # if the Django compatibility decorator is used then we don't emit a warning
        # see https://github.com/PyCQA/pylint-django/issues/10
        if _has_python_2_unicode_compatible_decorator(node):
            return

        if PY3:
            return

        self.add_message(f"W{BASE_ID}01", args=node.name, node=node)