Is it possible to specify dictview object as the typing hint in Python?

Viewed 325

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
2 Answers

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

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
Related