pylint-dev / pylint

It's not just a linter that annoys you!
https://pylint.readthedocs.io/en/latest/
GNU General Public License v2.0
5.29k stars 1.13k forks source link

pylint does not infer the type of some object #6407

Closed omarandlorraine closed 2 years ago

omarandlorraine commented 2 years ago

Current problem

I have the following file:

li = []
li.append("hello")
li.append(3)

python blithely runs it, but neither pylint nor mypy actually catch the problem. mypy will complain about the above if we add a type annotation, but Python of course does not care.

Desired solution

I consider it bad style to put objects of differing types in lists, and I think a linter should warn of this kind of thing.

I would expect a message like W1234: appending object of type "int" to list also containing objects of type "str"

The same goes for the keys of a dictionary and the values of a dictionary.

What do you think?

Additional context

Usually, when we consume iterables, it's to yield all objects of type Foo, so that we can call foo.bar(). We wouldn't want that kind of thing to throw a ThingNotFound exception at runtime.

mbyrnepr2 commented 2 years ago

It looks like mypy does handle this situation:

mypy x.py

x.py:3: error: Argument 1 to "append" of "list" has incompatible type "int"; expected "str"
Found 1 error in 1 file (checked 1 source file)

mypy -V mypy 0.942

python -V Python 3.10.0b2Python 3.10.0b2`

omarandlorraine commented 2 years ago

Oh right, that's different from what I'm seeing here. I've got mypy version 0.812 though.

What should pylint do?

DanielNoord commented 2 years ago

I think we should defer this to type checkers such as mypy. We really struggle with inferring control flow in python files ourselves and I think we shouldn't try re-inventing the wheel. If we ever have less than 10 issues open we could obviously revisit this, but I think for now there are pressing matters 😄

Pierre-Sassoulas commented 2 years ago

If we ever have less than 10 issues open

:)

I'm closing as out of scope for pylint for now, mypy is already doing type checking.