0
def main() -> None:
    i = 12

Is there anyway PyCharm can throw a warning letting me know that this:

def main() -> None:
    i: int = 12

Is the better option? I installed mypy, but it couldn't detect anything wrong in the example above.

sam_i_am
  • 23
  • 1
  • 6
  • 2
    `mypy` is correct. Your code does not return anything. Just None. try to `return i` and see the output of `mypy` – balderman Sep 06 '21 at 20:43
  • @balderman but isn't there a way for PyCharm or some other linter to automatically detect when I just create a variable with no type? – sam_i_am Sep 06 '21 at 21:12
  • @sam_i_am keep using mypy, if you declare a variable without type and later there's an inconsistency mypy will warn you. The same applies to a variable declared with a type that is later used in an operation inconsistent with the type. Also please notice that installing mypy isn't enough, you have to [run it in the terminal](https://mypy.readthedocs.io/en/stable/getting_started.html#installing-and-running-mypy). – bad_coder Sep 06 '21 at 22:11
  • @balderman I ran this: "mypy --ignore-missing-imports Programs/cough.py" And my code was this: if __name__ == "__main__": i = 2 And it said it was all good, why didn't it warn me? – sam_i_am Sep 06 '21 at 22:58
  • @sam_i_am there's nothing to warn about. `i: int = 12` is legal and `i = 12` is also legal. If you mean setting mypy to warn you when a variable is declared without type then I think there isn't any option for that. The closest would be [`--allow-untyped-globals`](https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-allow-untyped-globals) or [`--local-partial-types`](https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-local-partial-types). I would recommend trying you continue coding using the default mypy settings while you get familiar with mypy. – bad_coder Sep 06 '21 at 23:13

0 Answers0