2

Good morning everybody,

I need to know if there is a way, at the moment I say no, for having the behavior I write in JavaScript also in Python.

If in JavaScript, when I have a function with a parameter that can assume only some values, I write the following code

/**
 * Set the status of session
 * @param {('ACTIVE'|'LOCKED'|'UNAUTHORIZED')} status
 */
function set(status = 'UNAUTHORIZED') {
    console.log(status);
}

Writing in this way, calling the function makes my IDE (PHPStorm) to suggest me the list of values I can use as shown in the following image

enter image description here

This is my function in Python

def set(status: str) -> None:
    """
    Set the status of session

    :param status: ('ACTIVE','LOCKED','UNAUTHORIZED')

    :return:
    :rtype: None
    """
    print(status)

but I can't get the same result with IDE (PYCharm) probably because the syntax of docstring is wrong.

enter image description here

Is there a way to get in Python the same result I have in JavaScript? For example, if I wrote the same function, how can I define the list of values that the parameter can assume?

Thanks to all of you

Luca Romano
  • 116
  • 7
  • 3
    Not docstring, but actual type hinting: https://stackoverflow.com/a/58521635/476 – deceze Feb 17 '22 at 10:34
  • @deceze already tried, but doesn't work as I'm expected. ``` from typing import Literal def typed_method(parameter: Literal["value", "other value"]): pass typed_method(parameter="valuessss") # "value" is not suggested ``` – Luca Romano Feb 17 '22 at 10:40
  • You mean PyCharm doesn't auto-complete the value for you? That's PyCharm's problem… The type hint works as is, and PyCharm even hints it properly, and it flags the call if you pass some other value. ‍♂️ – deceze Feb 17 '22 at 10:43
  • 1
    [enum](https://docs.python.org/3/library/enum.html) could be an alternative. – Wups Feb 17 '22 at 10:43
  • @deceze there is some specific configuration in PyCharm to get the behavior? Because in my version I can pass every value I want without getting suggestion – Luca Romano Feb 17 '22 at 10:46
  • @Wups enum doesn't match my needs – Luca Romano Feb 17 '22 at 10:48
  • Frankly, I don't think I've ever seen literal auto-completion in PyCharm [for Python code], so it might not exist at all…? – deceze Feb 17 '22 at 10:50

0 Answers0