Skip to content

Commit 196544e

Browse files
databind.core/: fix: Fixed a potential exception during generating the exception message when date object cannot be parsed.
1 parent d0939c0 commit 196544e

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[[entries]]
2+
id = "0484f896-617e-450e-8835-6bcd6f8acc80"
3+
type = "fix"
4+
description = "Fixed a potential exception during generating the exception message when date object cannot be parsed."
5+
author = "@NiklasRosenstein"

databind.core/src/databind/core/settings.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
import enum
66
import typing as t
77

8+
from nr.date import date_format, datetime_format, format_set, time_format
89
from typeapi import AnnotatedTypeHint, ClassTypeHint, TypeHint
910

1011
from databind.core.utils import T, check_instance_of, check_not_none, check_subclass_of
1112

1213
if t.TYPE_CHECKING:
13-
from nr.date import date_format, datetime_format, format_set, time_format
14-
1514
from databind.core.context import Context
1615
from databind.core.converter import Converter
1716
from databind.core.union import EntrypointUnionMembers, ImportUnionMembers, StaticUnionMembers, UnionMembers
@@ -581,8 +580,9 @@ def __iter_formats(self, type_: t.Type[Formatter]) -> t.Iterable[Formatter]:
581580
if isinstance(fmt, str):
582581
if fmt.startswith("."):
583582
yield self.__get_builtin_format(fmt)
584-
yield type_.compile(fmt) # type: ignore
585-
elif type(fmt) == type_:
583+
else:
584+
yield type_.compile(fmt) # type: ignore
585+
elif type(fmt) is type_:
586586
yield fmt
587587
elif isinstance(fmt, format_set):
588588
yield from getattr(fmt, type_.__name__ + "s")
@@ -644,9 +644,10 @@ def format(self, dt: T_Dtype) -> str:
644644
raise self._formulate_parse_error(list(self.__iter_formats(format_t)), dt)
645645

646646
@staticmethod
647-
def _formulate_parse_error(formats: t.Sequence[t.Any], s: t.Any) -> ValueError:
647+
def _formulate_parse_error(formats: t.Sequence[Formatter], s: t.Any) -> ValueError:
648648
return ValueError(
649-
f'"{s}" does not match date formats ({len(formats)}):' + "".join(f"\n | {x.format_str}" for x in formats)
649+
f'"{s}" does not match date formats ({len(formats)}):'
650+
+ "".join(f"\n | {str(x) if isinstance(x, format_set) else x.format_str}" for x in formats)
650651
)
651652

652653

0 commit comments

Comments
 (0)