Skip to content

Commit 1f23fd1

Browse files
improvement: Mark some more settings as frozen or add unsafe hash functions to support passing them as metadata to typing_extensions.Annotated in Python 3.6
1 parent b05388c commit 1f23fd1

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

databind.core/.changelog/_unreleased.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ id = "78606f4e-7b4c-4f88-9143-e40b12d86100"
99
type = "improvement"
1010
description = "Expose public API in `databind.core` root module."
1111
author = "@NiklasRosenstein"
12+
13+
[[entries]]
14+
id = "8d5cd812-ba8f-4c4d-9681-b0d8866ae023"
15+
type = "improvement"
16+
description = "Mark some more settings as frozen or add unsafe hash functions to support passing them as metadata to `typing_extensions.Annotated` in Python 3.6"
17+
author = "@NiklasRosenstein"

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ class SerializeDefaults(BooleanSetting):
326326
to how the name of the setting appears assertive of the fact that the instance indicates the setting is enabled."""
327327

328328

329-
@dataclasses.dataclass
329+
@dataclasses.dataclass(frozen=True)
330330
class DeserializeAs(Setting):
331331
"""Indicates that a field should be deserialized as the given type instead of the type of the field. This is
332332
typically used when a field should be typed as an abstract class or interface, but during deserialization of the
@@ -360,7 +360,7 @@ class MyClass:
360360
priority: Priority = Priority.NORMAL
361361

362362

363-
@dataclasses.dataclass
363+
@dataclasses.dataclass(frozen=True)
364364
class Precision(Setting):
365365
"""A setting to describe the precision for #decimal.Decimal fields."""
366366

@@ -530,7 +530,7 @@ def import_() -> "ImportUnionMembers":
530530
return ImportUnionMembers()
531531

532532

533-
@dataclasses.dataclass
533+
@dataclasses.dataclass(init=False, unsafe_hash=True)
534534
class DateFormat(Setting):
535535
"""The #DateFormat setting is used to describe the date format to use for #datetime.datetime, #datetime.date
536536
and #datetime.time values when formatting them as a string, i.e. usually when the date/time is serialized, and

docs/content/core/settings.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ unless specified otherwise. The following priority groups exist:
2929
* `ULTIMATE`: Settings with this priority group are resolved before `HIGH` settings.
3030

3131
Converters are usually only interested in the first instance of any setting type.
32+
33+
## Notes about implementing custom settings
34+
35+
To support Python 3.6, `typing_extensions.Annotated` metadata must be hashable. As such, settings that you want users
36+
to be able to pass into annotated type hints in Python 3.6 must support hashing (identity hashing is often sufficient).

0 commit comments

Comments
 (0)