F
F
Finnick is healthy2020-06-23 18:17:03
Python
Finnick is healthy, 2020-06-23 18:17:03

What is the best way to organize a function call in python with type typing?

Hello, I am writing a wrapper over vk api for python. I want to make full type typing for all functions and objects of VK, as well as for VK api methods and their responses.
the question arose about the convenience of such a format. Data typing is certainly good, but it makes the code too cumbersome. For example, a function to call groups with the "filter" parameter will have to be done like this:

vk.groups.get(filter=GroupsFilter.ADMIN)
>>> {'response': {'count': 0, 'items': []}}

because there is a given type GroupsFilter, which has the following structure:
class GroupsFilter(Enum):
  ADMIN = 'admin'
  EDITOR = 'editor'
  MODER = 'moder'
  GROUPS = 'groups'
  PUBLICS = 'publics'
  EVENTS = 'events'
  HAS_ADDRESSES = 'has_addresses'


Although one could simply call:
vk.groups.get(filter='admin')
>>> {'response': {'count': 0, 'items': []}}


The advantage of the first option is that when programming in an IDE, for example in pycharm, prompts will appear as in the photo below, and it will immediately be clear what values ​​the parameter can take and not go into the documentation: 5ef21c0ea279d998211844.png
On the other hand, this makes the code cumbersome for When the prompt appears, you need to know that the "filter" parameter accepts the GroupsFilter type.

Do you have any advice on how best to do it?

ps All data types are taken from the official Json-Schema (Github)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2020-06-23
@tumbler

You can also use typing.Literal, but you can't count on PyCharm support.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question