Skip to content

Commit

Permalink
feat: Show non-fatal stanc warnings
Browse files Browse the repository at this point in the history
Show non-fatal stanc warning messages to user.
Exposes information available since httpstan 2.2.0.

Closes #144
  • Loading branch information
riddell-stan committed Oct 10, 2020
1 parent c62994a commit 0de28cb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ classifiers = [
[tool.poetry.dependencies]
python = "^3.7"
aiohttp = "^3.6"
httpstan = "^2.1.0"
httpstan = "^2.2.0"
numpy = "^1.7"
clikit = "^0.6.2"

Expand Down
9 changes: 6 additions & 3 deletions stan/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,13 @@ async def go():
# Note: during compilation `httpstan` redirects stderr to /dev/null, making `print` impossible.
path, payload = "/v1/models", {"program_code": program_code}
async with aiohttp.request("POST", f"http://{host}:{port}{path}", json=payload) as resp:
response_payload = await resp.json()
if resp.status != 201:
raise RuntimeError((await resp.json())["message"])
assert model_name == (await resp.json())["name"]

raise RuntimeError(response_payload["message"])
assert model_name == response_payload["name"]
if response_payload.get("stanc_warnings"):
io.error_line("<comment>Warnings from stanc:</comment>")
io.error_line(response_payload["stanc_warnings"])
path, payload = f"/v1/{model_name}/params", {"data": data}
async with aiohttp.request("POST", f"http://{host}:{port}{path}", json=payload) as resp:
if resp.status != 200:
Expand Down
33 changes: 33 additions & 0 deletions tests/test_stanc_warnings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Test that stanc warnings are visible."""
import contextlib
import io

import stan


def test_stanc_no_warning() -> None:
"""No warnings."""
program_code = "parameters {real y;} model {y ~ normal(0,1);}"
buffer = io.StringIO()
with contextlib.redirect_stderr(buffer):
stan.build(program_code=program_code)
assert "warning" not in buffer.getvalue().lower()


def test_stanc_warning() -> None:
"""Test that stanc warning is shown to user."""
# stanc prints warning:
# assignment operator <- is deprecated in the Stan language; use = instead.
program_code = """
parameters {
real y;
}
model {
real x;
x <- 5;
}
"""
buffer = io.StringIO()
with contextlib.redirect_stderr(buffer):
stan.build(program_code=program_code)
assert "assignment operator <- is deprecated in the Stan language" in buffer.getvalue()

0 comments on commit 0de28cb

Please sign in to comment.