-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Incorrect hints for TestCase.assertRaises #8372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
What is |
decode() is defined here: def decode(
data: _ReadableBuffer, codec_options: "Optional[CodecOptions[_DocumentType]]" = None
) -> _DocumentType:
... and _DocumentType is defined here: class CodecOptions(Tuple, Generic[_DocumentType]):
document_class: Type[_DocumentType]
tz_aware: bool
uuid_representation: int
unicode_decode_error_handler: Optional[str]
tzinfo: Optional[datetime.tzinfo]
type_registry: TypeRegistry
def __new__(
cls: Type[CodecOptions],
document_class: Optional[Type[_DocumentType]] = ...,
tz_aware: bool = ...,
uuid_representation: Optional[int] = ...,
unicode_decode_error_handler: Optional[str] = ...,
tzinfo: Optional[datetime.tzinfo] = ...,
type_registry: Optional[TypeRegistry] = ...,
) -> CodecOptions[_DocumentType]: ... |
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 |
Here's the smallest repro I could get: import unittest
from typing import TypeVar, Any, Mapping, Optional
T = TypeVar("T", bound=Mapping[str, Any])
def raises(opts: Optional[T] = None) -> T:
if opts is None:
raise TypeError()
return opts
class TestAssertRaises(unittest.TestCase):
def test_assertRaises(self) -> None:
self.assertRaises(TypeError, raises, None) output:
|
Thanks! Here's a repro for the mypy bug that doesn't rely on the stubs: from typing import Callable, TypeVar, Any, Mapping, Optional
T = TypeVar("T", bound=Mapping[str, Any])
def raises(opts: Optional[T]) -> T: pass
def assertRaises(cb: Callable[..., object]) -> None: pass
assertRaises(raises)
@AlexWaygood given this mypy bug, what do you think of returning to |
Opened python/mypy#13220 to track the mypy issue |
Sounds good to me. |
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. |
Reopening. We should at least document why we use |
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. |
My mistake, the other functions also need to be changed to use Any, for example addCleanup is also broken:
|
Uh oh!
There was an error while loading. Please reload this page.
Pymongo's mypy tests pass with mypy==0.942 but fail with mypy==0.971 with this error:
I believe this was a regression caused by the changes in #7012
The text was updated successfully, but these errors were encountered: