You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Pymongo's mypy tests pass with mypy==0.942 but fail with mypy==0.971 with this error:
test/test_bson.py: note: In member "assertInvalid" of class "TestBSON":
test/test_bson.py:120: error: Argument 2 to "assertRaises" of "TestCase" has incompatible type
"Callable[[Union[bytes, memoryview, mmap, array[Any]], Optional[CodecOptions[_DocumentType]]], _DocumentType]"; expected "Callable[..., object]"
[arg-type]
self.assertRaises(InvalidBSON, decode, data)
^
I believe this was a regression caused by the changes in #7012
What is _DocumentType here? The error message may indicate a mypy bug, since Callable is covariant in its return type and every type should be compatible with object.
mypy --strict repro-mypy-bug.py
repro-mypy-bug.py: note: In member "test_assertRaises" of class "TestAssertRaises":
repro-mypy-bug.py:13: error: Argument 2 to "assertRaises" of "TestCase" has incompatible type "Callable[[Optional[T]], T]"; expected
"Callable[..., object]" [arg-type]
self.assertRaises(TypeError, raises, None)
^
Found 1 error in 1 file (checked 1 source file)
I merged #8373. Is there a risk that this mypy bug could hit users for any of the other functions that were changed in #7012? Or are we good to leave this closed now?
Reopening. We should at least document why we use Any in the PR, instead of object. That said, I'd prefer if mypy would fix their bug, or alternatively that mypy patches their version of typeshed when shipping the next release.
I can confirm that the issue is the bound TypeVar (and possible constrained TypeVars have a similar problem). I tried several ways of fixing, but they all broke a bunch of stuff -- curious if anyone has ideas on alternate mypy fixes.
Activity
JelleZijlstra commentedon Jul 23, 2022
What is
_DocumentTypehere? The error message may indicate a mypy bug, since Callable is covariant in its return type and every type should be compatible withobject.ShaneHarvey commentedon Jul 23, 2022
decode() is defined here:
and _DocumentType is defined here:
JelleZijlstra commentedon Jul 23, 2022
Thanks! Maybe it has something to do with the TypeVar, but I'm not having much luck trying to reproduce this in a smaller example. https://mypy-play.net/?mypy=latest&python=3.10&gist=1f95669029e61cff3091eb3b75c4e2bd
ShaneHarvey commentedon Jul 23, 2022
Here's the smallest repro I could get:
output:
JelleZijlstra commentedon Jul 23, 2022
Thanks! Here's a repro for the mypy bug that doesn't rely on the stubs:
@AlexWaygood given this mypy bug, what do you think of returning to
Callable[..., Any]for now?hauntsaninja commentedon Jul 23, 2022
Opened python/mypy#13220 to track the mypy issue
AlexWaygood commentedon Jul 23, 2022
Sounds good to me.
AlexWaygood commentedon Jul 23, 2022
I merged #8373. Is there a risk that this mypy bug could hit users for any of the other functions that were changed in #7012? Or are we good to leave this closed now?
JelleZijlstra commentedon Jul 23, 2022
It could hit in other cases, I think the main ingredient is the constrained TypeVar. Not sure how likely it is to actually come up though.
srittau commentedon Jul 23, 2022
Reopening. We should at least document why we use
Anyin the PR, instead ofobject. That said, I'd prefer if mypy would fix their bug, or alternatively that mypy patches their version of typeshed when shipping the next release.hauntsaninja commentedon Jul 23, 2022
I can confirm that the issue is the bound TypeVar (and possible constrained TypeVars have a similar problem). I tried several ways of fixing, but they all broke a bunch of stuff -- curious if anyone has ideas on alternate mypy fixes.
ShaneHarvey commentedon Jul 25, 2022
My mistake, the other functions also need to be changed to use Any, for example addCleanup is also broken: