1
1
from _erg_result import is_ok
2
2
from _erg_range import Range
3
+ from _erg_type import is_type , isinstance
3
4
4
5
from collections import namedtuple
5
6
@@ -8,7 +9,7 @@ def contains_operator(y, elem) -> bool:
8
9
if hasattr (elem , "type_check" ):
9
10
return elem .type_check (y )
10
11
# 1 in Int
11
- elif type (y ) == type :
12
+ elif is_type (y ):
12
13
if isinstance (elem , y ):
13
14
return True
14
15
elif hasattr (y , "try_new" ) and is_ok (y .try_new (elem )):
@@ -17,26 +18,33 @@ def contains_operator(y, elem) -> bool:
17
18
return False
18
19
# [1] in [Int]
19
20
elif isinstance (y , list ) and isinstance (elem , list ) and (
20
- type ( y [ 0 ] ) == type or isinstance (y [0 ], Range )
21
+ len ( y ) == 0 or is_type ( y [ 0 ]) or isinstance (y [0 ], Range )
21
22
):
22
- # FIXME:
23
- type_check = contains_operator (y [0 ], elem [0 ])
24
- len_check = len (elem ) == len (y )
23
+ type_check = all (map (lambda x : contains_operator (x [0 ], x [1 ]), zip (y , elem )))
24
+ len_check = len (elem ) <= len (y )
25
25
return type_check and len_check
26
26
# (1, 2) in (Int, Int)
27
27
elif isinstance (y , tuple ) and isinstance (elem , tuple ) and (
28
- type ( y [ 0 ] ) == type or isinstance (y [0 ], Range )
28
+ len ( y ) == 0 or is_type ( y [ 0 ]) or isinstance (y [0 ], Range )
29
29
):
30
30
if not hasattr (elem , "__iter__" ):
31
31
return False
32
32
type_check = all (map (lambda x : contains_operator (x [0 ], x [1 ]), zip (y , elem )))
33
- len_check = len (elem ) = = len (y )
33
+ len_check = len (elem ) < = len (y )
34
34
return type_check and len_check
35
35
# {1: 2} in {Int: Int}
36
- elif isinstance (y , dict ) and isinstance (elem , dict ) and isinstance (next (iter (y .keys ())), type ):
36
+ elif isinstance (y , dict ) and isinstance (elem , dict ) and (
37
+ len (y ) == 0 or is_type (next (iter (y .keys ())))
38
+ ):
39
+ if len (y ) == 1 :
40
+ key = next (iter (y .keys ()))
41
+ key_check = all ([contains_operator (key , el ) for el in elem .keys ()])
42
+ value = next (iter (y .values ()))
43
+ value_check = all ([contains_operator (value , el ) for el in elem .values ()])
44
+ return key_check and value_check
37
45
# TODO:
38
46
type_check = True # contains_operator(next(iter(y.keys())), x[next(iter(x.keys()))])
39
- len_check = len ( elem ) >= len ( y )
47
+ len_check = True # It can be True even if either elem or y has the larger number of elems
40
48
return type_check and len_check
41
49
elif isinstance (elem , list ):
42
50
from _erg_array import Array
0 commit comments