Пример #1
0
from collections import deque
from contextlib import contextmanager
from pathlib import Path

from typing import Optional
from functools import wraps
from operator import getitem

import runtype
from rich.text import Text

from . import settings

mut_dataclass = runtype.dataclass(check_types=settings.typecheck, frozen=False)
dataclass = runtype.dataclass(check_types=settings.typecheck)
dsp = runtype.Dispatch()


class SafeDict(dict):
    def __setitem__(self, key, value):
        if key in self:
            if value is self[key]:
                return  # Nothing to do
            raise KeyError("Attempted to override key '%s' in a SafeDict" %
                           key)
        return dict.__setitem__(self, key, value)

    def update(self, *other_dicts):
        for other in other_dicts:
            for k, v in other.items():
                self[k] = v
Пример #2
0
from contextlib import contextmanager
from pathlib import Path

from typing import Optional
from functools import wraps
from operator import getitem
import dataclasses

import runtype
from rich.text import Text

from . import settings

mut_dataclass = runtype.dataclass(check_types=settings.typecheck, frozen=False)
dataclass = runtype.dataclass(check_types=settings.typecheck)
dy = runtype.Dispatch()


def field_list():
    return dataclasses.field(default_factory=list)


class SafeDict(dict):
    def __setitem__(self, key, value):
        if key in self:
            if value is self[key]:
                return  # Nothing to do
            raise KeyError("Attempted to override key '%s' in a SafeDict" %
                           key)
        return dict.__setitem__(self, key, value)
Пример #3
0
            return is_t2 and t1 <= t2
        elif is_t2:
            return False

        # Regular Python types
        return runtype.issubclass(t1, t2)

    default_type = object


class TS_Preql(ProtoTS):
    def get_type(self, obj):
        try:
            return obj.type
        except AttributeError:
            return type(obj)


class TS_Preql_subclass(ProtoTS):
    def get_type(self, obj):
        # Preql objects
        if isinstance(obj, Type):
            return obj

        # Regular Python
        return type(obj)


dp_type = runtype.Dispatch(TS_Preql_subclass())
dp_inst = runtype.Dispatch(TS_Preql())