Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 5153196

Browse files
author
Sergey Vasilyev
committed
Annotate inferred column types with arbitrary runtime notes (metadata)
Similar to pytest's marks or Python's Exception's notes, we can add arbitrary data classes and retrieve the latest note (marker). This makes the customization of column types easier without overriding the whole hierarchy of column types.
1 parent a8eb6f5 commit 5153196

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

data_diff/abcs/database_types.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import decimal
22
from abc import ABC, abstractmethod
3-
from typing import Tuple, Union
3+
from typing import List, Optional, Tuple, Type, TypeVar, Union
44
from datetime import datetime
55

66
import attrs
@@ -12,9 +12,24 @@
1212
DbKey = Union[int, str, bytes, ArithUUID, ArithAlphanumeric]
1313
DbTime = datetime
1414

15+
N = TypeVar("N")
1516

16-
@attrs.define(frozen=True)
17+
18+
@attrs.define(frozen=True, kw_only=True)
1719
class ColType:
20+
# Arbitrary metadata added and fetched at runtime.
21+
_notes: List[N] = attrs.field(factory=list, init=False, hash=False, eq=False)
22+
23+
def add_note(self, note: N) -> None:
24+
self._notes.append(note)
25+
26+
def get_note(self, cls: Type[N]) -> Optional[N]:
27+
"""Get the latest added note of type ``cls`` or its descendants."""
28+
for note in reversed(self._notes):
29+
if isinstance(note, cls):
30+
return note
31+
return None
32+
1833
@property
1934
def supported(self) -> bool:
2035
return True

0 commit comments

Comments
 (0)