How to write multiple signature hints in PyCharm using Python3

I wrote a function with an argument to the left, just like an inline function range

The problem is how to write type hints that can show the two ways in which it should be called.

For example, when I write Command + p (Ctrl + p) in rangein PyCharm (range is an object in py3, but this is not a problem):

self:range, stop: int
-------------------------------------------------
self:range, start: int, stop: int, step: int=-1

type hints picture of range function

But for my_range:

def my_range(start: int, stop: int = None, step: int=1):
"""
list_range(stop) 
list_range(start, stop, step)

return list of integers from start (default 0) to stop,
incrementing by step (default 1).

"""
if stop is None:
    start, stop = 0, start
return list(range(start, stop, step))

after type Command + p, I got:

start: int, stop: Optional[int], step: int=-1

Does anyone know how to implement this? Thanks so much for any help!

+4
source share
1 answer

typing! , , typing.Optional. :

import typing

def f(required_arg: int, optional_arg:typing.Optional[int]=None):
    # ...
0

Source: https://habr.com/ru/post/1674296/


All Articles