11from _erg_result import is_ok
22from _erg_range import Range
3+ from _erg_type import is_type , isinstance
34
45from collections import namedtuple
56
@@ -8,7 +9,7 @@ def contains_operator(y, elem) -> bool:
89 if hasattr (elem , "type_check" ):
910 return elem .type_check (y )
1011 # 1 in Int
11- elif type (y ) == type :
12+ elif is_type (y ):
1213 if isinstance (elem , y ):
1314 return True
1415 elif hasattr (y , "try_new" ) and is_ok (y .try_new (elem )):
@@ -17,26 +18,33 @@ def contains_operator(y, elem) -> bool:
1718 return False
1819 # [1] in [Int]
1920 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 )
2122 ):
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 )
2525 return type_check and len_check
2626 # (1, 2) in (Int, Int)
2727 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 )
2929 ):
3030 if not hasattr (elem , "__iter__" ):
3131 return False
3232 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 )
3434 return type_check and len_check
3535 # {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
3745 # TODO:
3846 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
4048 return type_check and len_check
4149 elif isinstance (elem , list ):
4250 from _erg_array import Array
0 commit comments