Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type hint for dictionary view objects

I'm not sure the documentation contains any concrete info on the type of view objects. I wasn't able to locate it (i.e. <class 'dict_values'> in the dict's source code. Is there a way to explicitly specify this 'view class'? And does it make sense in the first place? Example:

def func1(arg: dictview) -> None:
    pass
like image 274
sweet_sauce Avatar asked Nov 15 '25 04:11

sweet_sauce


2 Answers

You're looking for the typing.MappingView type hint or any of its related types like KeysView, ItemsView, ValuesView.

like image 169
deceze Avatar answered Nov 17 '25 17:11

deceze


typing.MappingView is now deprecated in python 3.9 , You can use collections.abc.MappingView

collections.abc.MappingView
collections.abc.ItemsView
collections.abc.KeysView
collections.abc.ValuesView
like image 37
SorousH Bakhtiary Avatar answered Nov 17 '25 19:11

SorousH Bakhtiary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!