2

I just had to debug a problem in production that boils down to the following behavior.

What I should have typed:

>>> import pandas as pd
>>> from io import StringIO
>>> a: pd.DataFrame = pd.read_csv(StringIO('a,b,c\n1,2,3'))
>>> isinstance(a, pd.DataFrame)
True

What I actually typed:

>>> a = pd.DataFrame = pd.read_csv(StringIO('a,b,c\n1,2,3'))
>>> isinstance(a, pd.DataFrame)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: isinstance() arg 2 must be a type or tuple of types

This was hard to track, because the isinstance() check was added months after the typo was made. The original code worked because pd.DataFrame wasn't used anywhere else in the scope.

Is there any way to catch this nasty typo? mypy doesn't see a problem with it.


Edit: the original question used list instead of pd.DataFrame, but as pointed out by some users, both this flake8 plugin and pylint W0622 can detect redefined builtins.

Rafael Barbosa
  • 1,120
  • 12
  • 17
  • [Flake8 Builtins plugin](https://pypi.org/project/flake8-builtins/): "Check for python builtins being used as variables or parameters." – Cristian Ciupitu Mar 25 '21 at 10:57
  • Does this answer your question? [lint for variable names such as 'len' (built-in functions/reserved words etc)](https://stackoverflow.com/questions/50609715/lint-for-variable-names-such-as-len-built-in-functions-reserved-words-etc) – Cristian Ciupitu Mar 25 '21 at 11:11
  • Does this answer your question? [Is there a way to protect built-ins in python?](https://stackoverflow.com/questions/27191277/is-there-a-way-to-protect-built-ins-in-python) – Georgy Mar 25 '21 at 12:02
  • You're all correct, but the error can still happen with non-bultin types. I've updated the question. – Rafael Barbosa Mar 25 '21 at 12:32

1 Answers1

1

TL;DR

pip install pandas-stubs

And mypy should start failing with:

error: Cannot assign to a type

Long answer

You are correct in assuming that mypy should have blocked this. But the problem is that the pandas project does not includes type information yet. By now (March 2021) the pandas team has it's own stub project in progress see https://pypi.org/project/pandas-stubs/.

alejandro zuleta
  • 13,962
  • 3
  • 28
  • 48
Cesar Canassa
  • 18,659
  • 11
  • 66
  • 69