-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Convert MessageBuilder.disable_errors to a context manager #10569
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
Conversation
mypy/messages.py
Outdated
@@ -136,11 +137,13 @@ def add_errors(self, messages: 'MessageBuilder') -> None: | |||
for info in errs: | |||
self.errors.add_error_info(info) | |||
|
|||
def disable_errors(self) -> None: | |||
@contextmanager | |||
def disable_errors(self) -> ContextManager[None]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Return type should be Iterable[None]
(or something with Generator
). The function returns an iterable, the decorator turns it into a ContextManager.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, thanks. I guess I was trying to placate PyCharm which complained that it doesn't have __enter__()
when I tried to use it "manually" within analyze_type_type_member_access
.
Maybe I should just bite the bullet and use with
in analyze_type_type_member_access
(and indent all the code)?
if right_map is None: | ||
self.msg.disable_errors() | ||
try: | ||
# If right_map is None then we know mypy considers the right branch |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, the original comment added in #3666 got shifted around over the years.
def nullcontext() -> Iterator[None]: | ||
yield | ||
else: | ||
from contextlib import nullcontext as nullcontext, contextmanager # noqa: F401 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: don't think you need to import contextmanager again. I think you could also move this to mypy/util.py
instead of making a separate file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, copy-pasta, will fix.
Re mypy/util.py
, I followed the pattern put in place by mypy/ordered_dict.py
. Maybe we want to put both of those in mypy/backports.py
or mypy/compat.py
?
Thank you! |
Oops, I didn't notice there were other PRs and issues! Perhaps an admin can close them. |
I closed the other two PRs. I think the issue can remain open for now since there are other similar refactors to be done (e.g., #10685). |
No description provided.