示例#1
0
 def test_raises_for_invalid_pep_604_union(self):
     parser = get_bound_parser(int | float)  # type: ignore[operator]
     with pytest.raises(
         BoundError,
         match=r"^Value is not within bound of 'typing\.Union\[int, float\]': '3'$",
     ):
         parser("3")
示例#2
0
 def test_raises_for_invalid_union(self):
     parser = get_bound_parser(Union[int, float])
     with pytest.raises(
         BoundError,
         match=r"^Value is not within bound of 'typing\.Union\[int, float\]': '3'$",
     ):
         parser("3")
示例#3
0
 def test_raises_for_invalid_intersection(self):
     parser = get_bound_parser((int, float))
     with pytest.raises(
         BoundError,
         match=r"^Value is not within bound of 'Intersection\[int, float\]': '2'$",
     ):
         parser("2")
示例#4
0
 def test_raises_for_invalid_value(self):
     parser = get_bound_parser(int)
     with pytest.raises(
         BoundError,
         match=r"^Value is not within bound of 'int': '1'$",
     ):
         parser("1")
示例#5
0
    def test_can_parse_intersection(self):
        class A:
            ...

        class B:
            ...

        class C(A, B):
            ...

        parser = get_bound_parser((A, B))
        value = C()
        assert parser(value) is value
示例#6
0
    def test_can_parse_union(self):
        class A:
            ...

        class B:
            ...

        class C(A, B):
            ...

        parser: Callable[[object], Union[A, B]] = get_bound_parser(Union[A, B])
        a = A()
        b = B()
        c = C()
        assert parser(a) is a
        assert parser(b) is b
        assert parser(c) is c
示例#7
0
 def test_can_parse_simple_bound(self):
     value = 1
     assert get_bound_parser(int)(value) is value
示例#8
0
    phone_number: str, country_code: str | None = None
) -> FormattedPhoneNumber:
    """
    Normalize ``phone_number`` using :py:const:`phonenumbers.PhoneNumberFormat.E164`.

    :raises InvalidPhoneNumber:
    """
    normalized = phonenumbers.format_number(
        _deconstruct_phone_number(phone_number, country_code),
        phonenumbers.PhoneNumberFormat.E164,
    )
    return cast(FormattedPhoneNumber, normalized)


is_phone_number = excepts(InvalidPhoneNumber)(_deconstruct_phone_number)
parse_str = get_bound_parser(str)


def is_formatted_phone_number(number: str) -> bool:
    try:
        return number == normalize_phone_number(number)
    except InvalidPhoneNumber:
        return False


class PhoneNumber(str, Phantom, predicate=is_phone_number):
    @classmethod
    def __schema__(cls) -> Schema:
        return {
            **super().__schema__(),  # type: ignore[misc]
            "description": "A valid E.164 phone number.",