Skip to content
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
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ clean-test: ## remove test and coverage artifacts
typings/uvicorn:
pyright --createstub uvicorn

typings/matplotlib:
pyright --createstub matplotlib
typings/matplotlib/__init__.pyi: ## grab type stubs from GitHub
mkdir -p typings
git clone --depth 1 https://github.com/microsoft/python-type-stubs typings/python-type-stubs
mv typings/python-type-stubs/stubs/matplotlib typings/
rm -rf typings/python-type-stubs

pyright: typings/uvicorn typings/matplotlib ## type check with pyright
pyright: typings/uvicorn typings/matplotlib/__init__.pyi ## type check with pyright
pyright

lint: ## check style with flake8
Expand Down
16 changes: 8 additions & 8 deletions shiny/render/_coordmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@
from matplotlib.axes import Axes
from matplotlib.figure import Figure
from matplotlib.gridspec import SubplotSpec
from matplotlib.transforms import Transform


def get_coordmap(fig: Figure) -> Coordmap | None:
dims_ar = cast("npt.NDArray[np.double]", fig.get_size_inches() * fig.get_dpi())
dims_ar = fig.get_size_inches() * fig.get_dpi()
dims: CoordmapDims = {
"width": dims_ar[0],
"height": dims_ar[1],
Expand All @@ -54,10 +53,13 @@ def get_coordmap(fig: Figure) -> Coordmap | None:


def get_coordmap_panel(axes: Axes, panel_num: int, height: float) -> CoordmapPanel:
spspec = cast("SubplotSpec", axes.get_subplotspec())
spspec = cast(
"SubplotSpec",
axes.get_subplotspec(), # pyright: ignore[reportGeneralTypeIssues]
)

domain_xlim = cast("tuple[float, float]", axes.get_xlim())
domain_ylim = cast("tuple[float, float]", axes.get_ylim())
domain_xlim = axes.get_xlim()
domain_ylim = axes.get_ylim()

# Data coordinates of plotting area
domain: CoordmapPanelDomain = {
Expand All @@ -68,9 +70,7 @@ def get_coordmap_panel(axes: Axes, panel_num: int, height: float) -> CoordmapPan
}

# Pixel coordinates of plotting area
transdata = cast(
"Transform", axes.transData # pyright: ignore[reportGeneralTypeIssues]
)
transdata = axes.transData

range_ar = cast(
"npt.NDArray[np.double]",
Expand Down
39 changes: 11 additions & 28 deletions shiny/render/_try_render_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,16 @@ def try_render_matplotlib(
alt: Optional[str],
**kwargs: object,
) -> TryPlotResult:
fig = get_matplotlib_figure( # pyright: ignore[reportUnknownVariableType]
x, allow_global
)
fig = get_matplotlib_figure(x, allow_global)

if fig is None:
return (False, None)

try:
import matplotlib.pyplot as plt

fig.set_size_inches( # pyright: ignore[reportUnknownMemberType]
width / ppi, height / ppi
)
fig.set_dpi(ppi * pixelratio) # pyright: ignore[reportUnknownMemberType]
fig.set_size_inches(width / ppi, height / ppi)
fig.set_dpi(ppi * pixelratio)

plt.tight_layout() # pyright: ignore[reportUnknownMemberType]
coordmap = get_coordmap(fig)
Expand Down Expand Up @@ -90,17 +86,9 @@ def get_matplotlib_figure(
# TODO: Might be good to detect non-empty plt.get_fignums() before we call the user
# function, which would mean we will false-positive here. Maybe we warn in that
# case, maybe we ignore gcf(), maybe both.
if (
x is None
and len(
plt.get_fignums() # pyright: ignore[reportUnknownArgumentType, reportUnknownMemberType]
)
> 0
):
if x is None and len(plt.get_fignums()) > 0:
if allow_global:
return (
plt.gcf() # pyright: ignore[reportUnknownVariableType, reportUnknownMemberType]
)
return plt.gcf()
else:
# Must close the global figure so we don't stay in this state forever
plt.close(plt.gcf()) # pyright: ignore[reportUnknownMemberType]
Expand All @@ -124,9 +112,7 @@ def get_matplotlib_figure(
# should cover most, if not all, of these (it doesn't cover Animation, though).
# https://matplotlib.org/stable/api/artist_api.html
if isinstance(x, Artist):
return (
x.get_figure() # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
)
return x.get_figure()

# Some other custom figure-like classes such as seaborn.axisgrid.FacetGrid attach
# their figure as an attribute
Expand All @@ -138,11 +124,9 @@ def get_matplotlib_figure(
# If they all refer to the same figure, then it seems reasonable to use it
# https://docs.xarray.dev/en/latest/user-guide/plotting.html#dimension-along-y-axis
if isinstance(x, (list, tuple)):
figs = [ # pyright: ignore[reportUnknownVariableType]
get_matplotlib_figure(y, allow_global) for y in cast(List[Any], x)
]
if len(set(figs)) == 1: # pyright: ignore[reportUnknownArgumentType]
return figs[0] # pyright: ignore[reportUnknownVariableType]
figs = [get_matplotlib_figure(y, allow_global) for y in cast(List[Any], x)]
if len(set(figs)) == 1:
return figs[0]

return None

Expand Down Expand Up @@ -189,8 +173,7 @@ def try_render_plotnine(
alt: Optional[str] = None,
**kwargs: object,
) -> TryPlotResult:
# Must use `pyright: ignore` otherwise Black formats the comments into a single line
from plotnine.ggplot import ggplot # pyright: ignore
from plotnine.ggplot import ggplot

if not isinstance(x, ggplot):
return (False, None)
Expand All @@ -215,7 +198,7 @@ def try_render_plotnine(
)
coordmap = get_coordmap_plotnine(
x,
res.figure, # pyright: ignore[reportUnknownMemberType, reportGeneralTypeIssues]
res.figure, # pyright: ignore[reportUnknownMemberType, reportUnknownArgumentType, reportGeneralTypeIssues]
)
res.figure.savefig( # pyright: ignore[reportUnknownMemberType, reportGeneralTypeIssues]
**res.kwargs # pyright: ignore[reportUnknownMemberType, reportGeneralTypeIssues]
Expand Down