GRAYBYTE WORDPRESS FILE MANAGER7745

Server IP : 198.54.121.189 / Your IP : 216.73.216.224
System : Linux premium69.web-hosting.com 4.18.0-553.44.1.lve.el8.x86_64 #1 SMP Thu Mar 13 14:29:12 UTC 2025 x86_64
PHP Version : 7.4.33
Disable Function : NONE
cURL : ON | WGET : ON | Sudo : OFF | Pkexec : OFF
Directory : /opt/cloudlinux/venv/lib/python3.11/site-packages/astroid/
Upload Files :
Current_dir [ Not Writeable ] Document_root [ Writeable ]

Command :


Current File : /opt/cloudlinux/venv/lib/python3.11/site-packages/astroid//constraint.py
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt

"""Classes representing different types of constraints on inference values."""
from __future__ import annotations

import sys
from abc import ABC, abstractmethod
from collections.abc import Iterator
from typing import Union

from astroid import bases, nodes, util
from astroid.typing import InferenceResult

if sys.version_info >= (3, 11):
    from typing import Self
else:
    from typing_extensions import Self

_NameNodes = Union[nodes.AssignAttr, nodes.Attribute, nodes.AssignName, nodes.Name]


class Constraint(ABC):
    """Represents a single constraint on a variable."""

    def __init__(self, node: nodes.NodeNG, negate: bool) -> None:
        self.node = node
        """The node that this constraint applies to."""
        self.negate = negate
        """True if this constraint is negated. E.g., "is not" instead of "is"."""

    @classmethod
    @abstractmethod
    def match(
        cls: type[Self], node: _NameNodes, expr: nodes.NodeNG, negate: bool = False
    ) -> Self | None:
        """Return a new constraint for node matched from expr, if expr matches
        the constraint pattern.

        If negate is True, negate the constraint.
        """

    @abstractmethod
    def satisfied_by(self, inferred: InferenceResult) -> bool:
        """Return True if this constraint is satisfied by the given inferred value."""


class NoneConstraint(Constraint):
    """Represents an "is None" or "is not None" constraint."""

    CONST_NONE: nodes.Const = nodes.Const(None)

    @classmethod
    def match(
        cls: type[Self], node: _NameNodes, expr: nodes.NodeNG, negate: bool = False
    ) -> Self | None:
        """Return a new constraint for node matched from expr, if expr matches
        the constraint pattern.

        Negate the constraint based on the value of negate.
        """
        if isinstance(expr, nodes.Compare) and len(expr.ops) == 1:
            left = expr.left
            op, right = expr.ops[0]
            if op in {"is", "is not"} and (
                _matches(left, node) and _matches(right, cls.CONST_NONE)
            ):
                negate = (op == "is" and negate) or (op == "is not" and not negate)
                return cls(node=node, negate=negate)

        return None

    def satisfied_by(self, inferred: InferenceResult) -> bool:
        """Return True if this constraint is satisfied by the given inferred value."""
        # Assume true if uninferable
        if isinstance(inferred, util.UninferableBase):
            return True

        # Return the XOR of self.negate and matches(inferred, self.CONST_NONE)
        return self.negate ^ _matches(inferred, self.CONST_NONE)


def get_constraints(
    expr: _NameNodes, frame: nodes.LocalsDictNodeNG
) -> dict[nodes.If, set[Constraint]]:
    """Returns the constraints for the given expression.

    The returned dictionary maps the node where the constraint was generated to the
    corresponding constraint(s).

    Constraints are computed statically by analysing the code surrounding expr.
    Currently this only supports constraints generated from if conditions.
    """
    current_node: nodes.NodeNG | None = expr
    constraints_mapping: dict[nodes.If, set[Constraint]] = {}
    while current_node is not None and current_node is not frame:
        parent = current_node.parent
        if isinstance(parent, nodes.If):
            branch, _ = parent.locate_child(current_node)
            constraints: set[Constraint] | None = None
            if branch == "body":
                constraints = set(_match_constraint(expr, parent.test))
            elif branch == "orelse":
                constraints = set(_match_constraint(expr, parent.test, invert=True))

            if constraints:
                constraints_mapping[parent] = constraints
        current_node = parent

    return constraints_mapping


ALL_CONSTRAINT_CLASSES = frozenset((NoneConstraint,))
"""All supported constraint types."""


def _matches(node1: nodes.NodeNG | bases.Proxy, node2: nodes.NodeNG) -> bool:
    """Returns True if the two nodes match."""
    if isinstance(node1, nodes.Name) and isinstance(node2, nodes.Name):
        return node1.name == node2.name
    if isinstance(node1, nodes.Attribute) and isinstance(node2, nodes.Attribute):
        return node1.attrname == node2.attrname and _matches(node1.expr, node2.expr)
    if isinstance(node1, nodes.Const) and isinstance(node2, nodes.Const):
        return node1.value == node2.value

    return False


def _match_constraint(
    node: _NameNodes, expr: nodes.NodeNG, invert: bool = False
) -> Iterator[Constraint]:
    """Yields all constraint patterns for node that match."""
    for constraint_cls in ALL_CONSTRAINT_CLASSES:
        constraint = constraint_cls.match(node, expr, invert)
        if constraint:
            yield constraint

[ Back ]
Name
Size
Last Modified
Owner / Group
Permissions
Options
..
--
June 25 2025 08:31:36
root / root
0755
__pycache__
--
May 15 2025 08:30:33
root / root
0755
brain
--
May 15 2025 08:30:33
root / root
0755
interpreter
--
May 15 2025 08:30:33
root / root
0755
nodes
--
May 15 2025 08:30:33
root / root
0755
__init__.py
4.984 KB
April 17 2025 13:10:59
root / root
0644
__pkginfo__.py
0.268 KB
April 17 2025 13:10:59
root / root
0644
_ast.py
4.049 KB
April 17 2025 13:10:59
root / root
0644
_backport_stdlib_names.py
6.852 KB
April 17 2025 13:10:59
root / root
0644
_cache.py
0.768 KB
April 17 2025 13:10:59
root / root
0644
arguments.py
12.654 KB
April 17 2025 13:10:59
root / root
0644
astroid_manager.py
0.559 KB
April 17 2025 13:10:59
root / root
0644
bases.py
24.994 KB
April 17 2025 13:10:59
root / root
0644
builder.py
18.348 KB
April 17 2025 13:10:59
root / root
0644
const.py
1.069 KB
April 17 2025 13:10:59
root / root
0644
constraint.py
4.925 KB
April 17 2025 13:10:59
root / root
0644
context.py
5.854 KB
April 17 2025 13:10:59
root / root
0644
decorators.py
9.854 KB
April 17 2025 13:10:59
root / root
0644
exceptions.py
12.782 KB
April 17 2025 13:10:59
root / root
0644
filter_statements.py
9.417 KB
April 17 2025 13:10:59
root / root
0644
helpers.py
11.07 KB
April 17 2025 13:10:59
root / root
0644
inference.py
44.063 KB
April 17 2025 13:10:59
root / root
0644
inference_tip.py
2.82 KB
April 17 2025 13:10:59
root / root
0644
manager.py
17.539 KB
April 17 2025 13:10:59
root / root
0644
mixins.py
1.154 KB
April 17 2025 13:10:59
root / root
0644
modutils.py
22.957 KB
April 17 2025 13:10:59
root / root
0644
node_classes.py
1.797 KB
April 17 2025 13:10:59
root / root
0644
objects.py
12.458 KB
April 17 2025 13:10:59
root / root
0644
protocols.py
32.203 KB
April 17 2025 13:10:59
root / root
0644
raw_building.py
22.339 KB
April 17 2025 13:10:59
root / root
0644
rebuilder.py
77.862 KB
April 17 2025 13:10:59
root / root
0644
scoped_nodes.py
0.936 KB
April 17 2025 13:10:59
root / root
0644
test_utils.py
2.377 KB
April 17 2025 13:10:59
root / root
0644
transforms.py
3.194 KB
April 17 2025 13:10:59
root / root
0644
typing.py
1.937 KB
April 17 2025 13:10:59
root / root
0644
util.py
4.618 KB
April 17 2025 13:10:59
root / root
0644

GRAYBYTE WORDPRESS FILE MANAGER @ 2025
CONTACT ME
Static GIF