Skip to content

Commit 67b5d1f

Browse files
authored
Merge pull request #98 from Ryton/main
Minor change wrt typecheck (int vs float)
2 parents aa0688d + 2e9f919 commit 67b5d1f

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

aocd/models.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,14 @@ def _repr_pretty_(self, p, cycle):
261261
template = "<{0}({1.year}, {1.day}) at {2} - {1.title}>"
262262
p.text(template.format(type(self).__name__, self, hex(id(self))))
263263

264+
def _coerce_val(self, val):
265+
if isinstance(val, float) and val.is_integer():
266+
log.warning("coerced value %r for %d/%02d", val, self.year, self.day)
267+
val = int(val)
268+
if isinstance(val, int):
269+
val = str(val)
270+
return val
271+
264272
@property
265273
def answer_a(self):
266274
try:
@@ -270,8 +278,7 @@ def answer_a(self):
270278

271279
@answer_a.setter
272280
def answer_a(self, val):
273-
if isinstance(val, int):
274-
val = str(val)
281+
val = self._coerce_val(val)
275282
if getattr(self, "answer_a", None) == val:
276283
return
277284
self._submit(value=val, part="a")
@@ -289,8 +296,7 @@ def answer_b(self):
289296

290297
@answer_b.setter
291298
def answer_b(self, val):
292-
if isinstance(val, int):
293-
val = str(val)
299+
val = self._coerce_val(val)
294300
if getattr(self, "answer_b", None) == val:
295301
return
296302
self._submit(value=val, part="b")
@@ -325,7 +331,8 @@ def incorrect_answers_b(self):
325331
def _submit(self, value, part, reopen=True, quiet=False):
326332
if value in {u"", b"", None, b"None", u"None"}:
327333
raise AocdError("cowardly refusing to submit non-answer: {!r}".format(value))
328-
value = str(value)
334+
if not isinstance(value, str):
335+
value = self._coerce_val(value)
329336
part = str(part).replace("1", "a").replace("2", "b").lower()
330337
if part not in {"a", "b"}:
331338
raise AocdError('part must be "a" or "b"')

aocd/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.3.1"
1+
__version__ = "1.3.2"

tests/test_submit.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import errno
2+
import logging
23

34
import pytest
45
from termcolor import colored
@@ -252,11 +253,19 @@ def test_will_not_submit_null():
252253
submit(None, part="a")
253254

254255

255-
@pytest.mark.answer_not_cached(rv='value')
256+
@pytest.mark.answer_not_cached(rv="value")
256257
def test_submit_guess_against_saved(requests_mock, capsys):
257-
post = requests_mock.post(
258-
url="https://adventofcode.com/2018/day/1/answer",
259-
text="<article>That's the right answer. Yeah!!</article>",
260-
)
258+
post = requests_mock.post(url="https://adventofcode.com/2018/day/1/answer")
261259
submit(1234, part="a", day=1, year=2018, session="whatever", reopen=False)
262260
assert post.call_count == 0
261+
262+
263+
def test_submit_float_warns(requests_mock, capsys, caplog):
264+
post = requests_mock.post(
265+
url="https://adventofcode.com/2022/day/8/answer",
266+
text="<article>yeah</article>",
267+
)
268+
submit(1234.0, part="a", day=8, year=2022, session="whatever", reopen=False)
269+
assert post.call_count == 1
270+
log_record = ("aocd.models", logging.WARNING, "coerced value 1234.0 for 2022/08")
271+
assert log_record in caplog.record_tuples

0 commit comments

Comments
 (0)