Skip to content

Use nullcontext more as do-nothing context manager. #20097

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

Merged
merged 1 commit into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"""

from collections import namedtuple
from contextlib import contextmanager, suppress
from contextlib import contextmanager, nullcontext
from enum import Enum, IntEnum
import functools
import importlib
Expand Down Expand Up @@ -2286,10 +2286,7 @@ def print_figure(
functools.partial(
print_method, orientation=orientation)
)
ctx = (renderer._draw_disabled()
if hasattr(renderer, '_draw_disabled')
else suppress())
with ctx:
with getattr(renderer, "_draw_disabled", nullcontext)():
self.figure.draw(renderer)

if bbox_inches:
Expand Down
7 changes: 1 addition & 6 deletions lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,15 +475,10 @@ def to_filehandle(fname, flag='r', return_opened=False, encoding=None):
return fh


@contextlib.contextmanager
def open_file_cm(path_or_file, mode="r", encoding=None):
r"""Pass through file objects and context-manage path-likes."""
fh, opened = to_filehandle(path_or_file, mode, True, encoding)
if opened:
with fh:
yield fh
else:
yield fh
return fh if opened else contextlib.nullcontext(fh)


def is_scalar_or_string(val):
Expand Down
9 changes: 2 additions & 7 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -3089,21 +3089,16 @@ def tight_layout(self, *, pad=1.08, h_pad=None, w_pad=None, rect=None):
.Figure.set_tight_layout
.pyplot.tight_layout
"""

from contextlib import nullcontext
from .tight_layout import (
get_subplotspec_list, get_tight_layout_figure)
from contextlib import suppress
subplotspec_list = get_subplotspec_list(self.axes)
if None in subplotspec_list:
_api.warn_external("This figure includes Axes that are not "
"compatible with tight_layout, so results "
"might be incorrect.")

renderer = _get_renderer(self)
ctx = (renderer._draw_disabled()
if hasattr(renderer, '_draw_disabled')
else suppress())
with ctx:
with getattr(renderer, "_draw_disabled", nullcontext)():
kwargs = get_tight_layout_figure(
self, self.axes, subplotspec_list, renderer,
pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
Expand Down
5 changes: 1 addition & 4 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
from collections import namedtuple
from contextlib import nullcontext
import datetime
from decimal import Decimal
import io
from itertools import product
import platform
from types import SimpleNamespace
try:
from contextlib import nullcontext
except ImportError:
from contextlib import ExitStack as nullcontext # Py3.6.

import dateutil.tz

Expand Down