def test_typed_namedtuple_with_field_names_as_str(): Dummy = typed_namedtuple("Dummy", "spell:str, mana:int or str,effect:list") with pytest.raises(TypeError): Dummy(mana="Lumos", spell=5, effect="Makes light") with pytest.raises(TypeError): Dummy = typed_namedtuple("Dummy", "spell:str, mana:int or str,effect:list", defaults=[0, "", ""])
def test_typed_namedtuple_rename(): Dummy = typed_namedtuple( "Dummy", ["_spell:str", "mana:int", "effect:list or tuple"], rename=True) d = Dummy("1", 2, [3, 4]) assert d._0 == "1" with pytest.raises(ValueError): Dummy = typed_namedtuple( "Dummy", ["_spell:str", "mana:int", "effect:list or tuple"])
def test_typed_namedtuple_default_values(): Dummy = typed_namedtuple("Dummy", ["spell:str", "mana:int", "effect:list"], defaults=["", 0, []]) d = Dummy() assert d.spell == "" assert d.mana == 0 assert d.effect == [] # defaults must match fields with pytest.raises(TypeError): typed_namedtuple("Dummy", ["spell:str", "mana:int", "effect:list"], defaults=[""]) with pytest.raises(TypeError): typed_namedtuple("Dummy", ["spell:str", "mana:int", "effect:list"], defaults=["", 0])
def test_typed_namedtuple_with_type_returns_object(): Dummy = typed_namedtuple("Dummy", ["spell:str", "mana:int", "effect:list"]) assert (Dummy( spell="Lumos", mana=5, effect=[ "Makes light", ], ) is not None)
def test_typed_namedtuple_no_type_returns_namedtuple(): Dummy = typed_namedtuple("Dummy", ["spell", "mana", "effect"]) assert (Dummy( spell="Lumos", mana=5, effect=[ "Makes light", ], ) is not None)
def test_typed_namedtuple_default_values_and_instantiate_with_some_values(): Dummy = typed_namedtuple("Dummy", ["spell:str", "mana:int", "effect:list or tuple"], defaults=["", 0, ()]) d = Dummy("Lumos") assert d.spell == "Lumos" assert d.mana == 0 assert d.effect == ()
def test_typed_namedtuple_incorrect_default_types_raises_type_error(): with pytest.raises(TypeError): Dummy = typed_namedtuple("Dummy", ["spell:str", "mana:int", "effect:list"], defaults=[0, "", ""]) with pytest.raises(TypeError): Dummy = typed_namedtuple( "Dummy", ["spell:str", "mana:int or str", "effect:list"], defaults=["", (), []], ) with pytest.raises(TypeError): Dummy = typed_namedtuple( "Dummy", ["spell:str", "mana:int or str", "effect:list or tuple"], defaults=["", "5", {1, 2}], )
def test_typed_namedtuple_name_type_as_tuple(): Dummy = typed_namedtuple("Dummy", [("spell", str), ("mana", int), ("effect", Union[list, tuple])]) with pytest.raises(TypeError): Dummy( [ "Mends torn pieces of paper.", ], "Papyrus Reparo", 10, ) with pytest.raises(TypeError): Dummy("Papyrus Reparo", 10, {1, 2, 3})
from django.core.mail import get_connection from django.core.mail.message import EmailMessage from django.db import models from strongtyping.type_namedtuple import typed_namedtuple from tinymce.models import HTMLField from additonal_utils.models import BigPrimaryKeyModel from kita.models import Kita from kita_friends.models import KitaFriends from kita_representative.models import KitaRepresentative from scheduler.commander import schedule_me MESSAGE_TYPE = List[Tuple[str, str, str, tuple]] DefaultSignature = typed_namedtuple('DefaultSignature', [ 'text:str', ]) default_signature = DefaultSignature(text='') EMAIL_SENDER = '*****@*****.**' def send_single_email(subject: str, message: str, recipient: tuple, fail_silently: bool): connection = get_connection(fail_silently=fail_silently) msg = EmailMessage(subject, message, EMAIL_SENDER, recipient, connection=connection) msg.content_subtype = 'html'
def test_typed_namedtuple_mixing_typ_and_no_type_not_allowed(): with pytest.raises(TypeError): Dummy = typed_namedtuple("Dummy", ["spell:str", "mana", "effect:list or tuple"])
def dummy_obj(): return typed_namedtuple("Dummy", ["spell:str", "mana:int", "effect:list"])