-1

Going through FastAPI docs, it looks like the following function

def get_name_with_age(name: str, age: int):
    name_with_age = name + " is this old: " + age
    return name_with_age

should trigger an error message in VSCode via a Python linter (mypy, in the example)

enter image description here

I'm already using flake8 but it doesn't detect such kinds of error. Is there a way to make flake8 behave like that?

floatingpurr
  • 7,749
  • 9
  • 46
  • 106
  • your screenshot literally tells you the answer: use mypy – anthony sottile Nov 09 '21 at 13:47
  • yes, this is a way. However, I need to stick with flake8, eventually extending its capabilities. Switching to or using also mypy is not a good option in my case. – floatingpurr Nov 09 '21 at 13:59
  • Does this answer your question? [Is there a way to make flake8 check for type hints in the source](https://stackoverflow.com/questions/43187829/is-there-a-way-to-make-flake8-check-for-type-hints-in-the-source) – alex_noname Nov 09 '21 at 15:25
  • Not really. I had already checked out such a question before posting here. They cite https://pypi.org/project/flake8-annotations/ but this plugin doesn't check expression consistency respect to involved datatypes. `flake8` can't do those checks. `mypy` looks like the only way to go. – floatingpurr Nov 09 '21 at 16:08

1 Answers1

2

Sorry, but it looks impossible.

In the mypy, this function was provided by "check_untyped_defs=true". We can disabled it with this configuration in the settings.json:

  "python.linting.mypyArgs": [
    "--follow-imports=silent",
    "--ignore-missing-imports",
    "--show-column-numbers",
    "--no-pretty",
    "check_untyped_defs=false"  //this line
  ],

And from the official docs of flake8, it only disabled these functions:

By default, Flake8 ignores E121, E123, E126, E226, E24, and E704.

But although you enable both of them, the flake8 still does not work.

So, it looks like the flake8 has not this feature.

Steven-MSFT
  • 7,438
  • 1
  • 5
  • 13