11#!/usr/bin/python
22# -*- coding: utf-8 -*-
33"""Callable subscript."""
4+ from collections .abc import Mapping , Sequence
45import itertools
56import json
67from typing import Generator , Tuple , Union
@@ -80,13 +81,13 @@ class EntriesCallableSubscript(CallableSubscript):
8081
8182 def __call__ (self , root_value : object , current_value : object ) -> Generator [MatchData , None , None ]:
8283 """Perform entries() call."""
83- if isinstance (current_value , dict ):
84+ if isinstance (current_value , Mapping ):
8485 if not self .args :
8586 value = list (map (list , current_value .items ()))
8687
8788 yield MatchData (SubscriptNode (TerminalNode (), [self ]),
8889 root_value , value )
89- elif isinstance (current_value , list ):
90+ elif isinstance (current_value , Sequence ) and not isinstance ( current_value , str ):
9091 if not self .args :
9192 value = list (map (list , enumerate (current_value )))
9293
@@ -101,13 +102,13 @@ class KeysCallableSubscript(CallableSubscript):
101102
102103 def __call__ (self , root_value : object , current_value : object ) -> Generator [MatchData , None , None ]:
103104 """Perform keys() call."""
104- if isinstance (current_value , dict ):
105+ if isinstance (current_value , Mapping ):
105106 if not self .args :
106107 value = list (current_value .keys ())
107108
108109 yield MatchData (SubscriptNode (TerminalNode (), [self ]),
109110 root_value , value )
110- elif isinstance (current_value , list ):
111+ elif isinstance (current_value , Sequence ) and not isinstance ( current_value , str ):
111112 if not self .args :
112113 value = list (range (len (current_value )))
113114
@@ -122,7 +123,7 @@ class LengthCallableSubscript(CallableSubscript):
122123
123124 def __call__ (self , root_value : object , current_value : object ) -> Generator [MatchData , None , None ]:
124125 """Perform length() call."""
125- if isinstance (current_value , list ):
126+ if isinstance (current_value , Sequence ) and not isinstance ( current_value , str ):
126127 if not self .args :
127128 value = len (current_value )
128129
@@ -166,13 +167,13 @@ class ValuesCallableSubscript(CallableSubscript):
166167
167168 def __call__ (self , root_value : object , current_value : object ) -> Generator [MatchData , None , None ]:
168169 """Perform values() call."""
169- if isinstance (current_value , dict ):
170+ if isinstance (current_value , Mapping ):
170171 if not self .args :
171172 value = list (current_value .values ())
172173
173174 yield MatchData (SubscriptNode (TerminalNode (), [self ]),
174175 root_value , value )
175- elif isinstance (current_value , list ):
176+ elif isinstance (current_value , Sequence ) and not isinstance ( current_value , str ):
176177 if not self .args :
177178 value = current_value
178179
0 commit comments