I recently made the mistake of opening my $PYTHONSTARTUP
file with mypy
syntax checking enabled. As a result, I started getting this error:
startup.py|79 col 2 error| Incompatible types in assignment (expression has type "HistoryPrompt", variable has type "str")
On line 79:
sys.ps1 = HistoryPrompt()
I immediately thought, "By Jove, mypy
! You're entirely correct! And yet, that is exactly what I want to do, so you're also wrong!"
I went looking to see if there was some kind of "stub" for the sys
module, but couldn't find anything. I'm guessing that mypy
is determining the type by looking at the value stored in the variable (default is ">>> ", which is a str
).
In reality, of course, the type needs to be the non-existant typing.Stringifiable
, indicating an object that will respond to str(x)
.
Having reached that dead end, I went looking for a way to tell mypy
to suppress the error. So many of the other tools support # noqa: xxx
that I figured there must be something, right?
Wrong. Or at least, I couldn't find it in my version, which is: mypy 0.670
So I devised a hack clever work-around:
import typing
# Work around mypy error: Incompatible types in assignment
suppress_mypy_error: typing.Any = HistoryPrompt()
sys.ps1 = suppress_mypy_error
My question is this: Is there a way to suppress this particular error in-line (best), or in mypy.ini
, or by submitting a PR to python/mypy
, or ...?