Example #1
0
def test_marker_object():
    assert_eq("text", str(qcore.MarkerObject("text")))

    with AssertRaises(TypeError):
        qcore.MarkerObject(b"bytes")

    # MarkerObjects should be unique
    assert_ne(qcore.MarkerObject("name"), qcore.MarkerObject("name"))
Example #2
0
def test_common():
    o = qcore.MarkerObject("o @ qcore.test_examples")
    assert_is_not(o, qcore.miss)

    o = qcore.dict_to_object({"a": 1, "b": 2})
    assert_eq(o.a, 1)  # Zero overhead!
    assert_eq(o.b, 2)  # Zero overhead!
Example #3
0
def test_marker_object():
    assert_eq("text", six.text_type(qcore.MarkerObject("text")))
    assert_is_instance(six.text_type(qcore.MarkerObject("text")),
                       six.text_type)

    # bytes should work in py2 but not py3
    if six.PY2:
        assert_eq(u"bytes", six.text_type(qcore.MarkerObject(b"bytes")))
        assert_eq(b"bytes", six.binary_type(qcore.MarkerObject(b"bytes")))
    else:
        with AssertRaises(TypeError):
            qcore.MarkerObject(b"bytes")

    # MarkerObjects should be unique
    assert_ne(qcore.MarkerObject("name"), qcore.MarkerObject("name"))
Example #4
0
def test_marker_object():
    assert_eq('text', six.text_type(qcore.MarkerObject('text')))
    assert_is_instance(six.text_type(qcore.MarkerObject('text')),
                       six.text_type)

    # bytes should work in py2 but not py3
    if six.PY2:
        assert_eq(u'bytes', six.text_type(qcore.MarkerObject(b'bytes')))
        assert_eq(b'bytes', six.binary_type(qcore.MarkerObject(b'bytes')))
    else:
        with AssertRaises(TypeError):
            qcore.MarkerObject(b'bytes')

    # MarkerObjects should be unique
    assert_ne(qcore.MarkerObject('name'), qcore.MarkerObject('name'))
Example #5
0
from .value import (
    KnownValue,
    ReferencingValue,
    SubclassValue,
    TypedValue,
    Value,
    boolean_value,
    UNINITIALIZED_VALUE,
    UNRESOLVED_VALUE,
    unite_values,
    flatten_values,
)

LEAVES_SCOPE = "%LEAVES_SCOPE"
LEAVES_LOOP = "%LEAVES_LOOP"
_UNINITIALIZED = qcore.MarkerObject("uninitialized")
_LookupContext = namedtuple("_LookupContext",
                            ["varname", "fallback_value", "node", "state"])


class VisitorState(enum.Enum):
    collect_names = 1
    check_names = 2


class ScopeType(enum.Enum):
    builtin_scope = 1
    module_scope = 2
    class_scope = 3
    function_scope = 4
Example #6
0
# See the License for the specific language governing permissions and
# limitations under the License.

__doc__ = """

Async generators.

"""
import qcore
import functools

from .decorators import asynq, async_proxy
from .futures import ConstFuture
from .utils import result

END_OF_GENERATOR = qcore.MarkerObject(u'end of generator')


def async_generator():
    """Decorator to create an async generator.

    async functions are always implemented as generators, but it is sometimes useful to yield values
    (as in a normal, non-async generator) from an async function. This decorator provides that
    capability. Inside of the generator, wrap any data (as opposed to async futures) you want to
    yield in a Value object. For example:

    >>> @asynq()
    ... def async_function():
    ...     return 42
    >>> @async_generator()
    ... def gen():
Example #7
0
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import qcore
import inspect
import six
from six.moves import xrange
from asynq import async, async_proxy, ConstFuture, result, FutureBase

unknown = qcore.MarkerObject(u'unknown @ entities')
must_load = qcore.MarkerObject(u'must_load @ entities')


class EntityState(qcore.Flags):
    # Base flags
    new = 1
    deleted = 2
    loaded = 0x10
    changed = 0x20
    # Few useful combinations
    must_load = 0
    new_must_save = new | loaded | changed
    deleted_saved = loaded | deleted
    deleted_must_save = loaded | deleted | changed
Example #8
0
    Callable,
    Dict,
    List,
    Set,
    TypeVar,
    Tuple,
    TYPE_CHECKING,
)
from typing_extensions import Literal

if TYPE_CHECKING:
    from .name_check_visitor import NameCheckVisitor

EMPTY = inspect.Parameter.empty

ARGS = qcore.MarkerObject("*args")
KWARGS = qcore.MarkerObject("**kwargs")
# Representation of a single argument to a call. Second member is
# None for positional args, str for keyword args, ARGS for *args, KWARGS
# for **kwargs.
Argument = Tuple[Composite, Union[None, str, Literal[ARGS], Literal[KWARGS]]]


class ImplReturn(NamedTuple):
    """Return value of :term:`impl` functions.

    These functions return either a single :class:`pyanalyze.value.Value`
    object, indicating what the function returns, or an instance of this class.

    """
Example #9
0
    flatten_values,
    replace_known_sequence_value,
    dump_value,
    assert_is_value,
)

from functools import reduce
import collections.abc
from itertools import product
import qcore
import inspect
import warnings
from types import FunctionType
from typing import cast, Dict, NewType, Callable, TypeVar, Optional, Union

_NO_ARG_SENTINEL = KnownValue(qcore.MarkerObject("no argument given"))

T = TypeVar("T")
IterableValue = GenericValue(collections.abc.Iterable, [TypeVarValue(T)])


def _maybe_or_constraint(
    left: AbstractConstraint, right: AbstractConstraint
) -> AbstractConstraint:
    if left is NULL_CONSTRAINT or right is NULL_CONSTRAINT:
        return NULL_CONSTRAINT
    return OrConstraint(left, right)


def clean_up_implementation_fn_return(
    return_value: Union[Value, ImplReturn]
Example #10
0
# limitations under the License.

__doc__ = """

Simple example of asynq-based batching support for a memcache client.

Requires the python-memcached library to be installed.

"""

from asynq import asynq, async_proxy, BatchBase, BatchItemBase, result
import qcore
import itertools
import memcache

MISS = qcore.MarkerObject(u'miss')


class Client(object):
    """Memcache client class supporting asynchronous, batched operations.

    Example usage:

        client = Client(['127.0.0.1:11211'])

        @client.cached('user_name')
        def name_of_user(uid):
            return database_query(...)

        @client.cached('answer_author')
        def author_of_answer(aid):