0

Unlike other methods used in different parts of the function in my program, the methods used in this part of the function is plain white and it says "method name: Any". For example, .isupper() shows isupper: Any whereas .lower() displays a different one.

enter image description here

CoreVisional
  • 99
  • 1
  • 6

1 Answers1

2

The variable symbol is not explicitly stated to be of type str, which isupper() and islower() are methods of. In ascii_letters, I imagine you have explicitly stated str types as values/elements, which is why vscode properly recognizes ascii_letters[key_position] to be of type str and thus why the lower() method is properly highlighted and described.

To remove the ambiguity, you could introduce type hinting:

def check_text_case(symbol: str, result: list, key_position: int) -> None:
    if symbol.isupper():
        result.append(ascii_letters[key_position])
    elif symbol.islower():
        result.append(ascii_letters[key_position].lower())

This way, the types are explicitly stated and the code is properly highlighted and described.

Scene
  • 489
  • 4
  • 16
  • I see, I have one question. In the doc where you linked the "typing', it also uses "-> None", what exactly is the usage here because I'm still confused after reading the doc. – CoreVisional Aug 19 '21 at 09:04
  • 1
    The `-> None` refers to the `return` type of the function. In your case, there is no `return` statement, but the function implicitly returns `None`. Here are some nice articles on the subject: https://realpython.com/lessons/type-hinting/#:~:text=Type%20hinting%20is%20a%20formal,type%20information%20to%20a%20function, https://stackoverflow.com/questions/32557920/what-are-type-hints-in-python-3-5 – Scene Aug 19 '21 at 16:30