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

Commit 6a85078

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 888650c commit 6a85078

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

data_diff/abcs/database_types.py

Lines changed: 18 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,25 @@
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+
21+
# Arbitrary metadata added and fetched at runtime.
22+
_notes: List[N] = attrs.field(factory=list, init=False, hash=False)
23+
24+
def add_note(self, note: N) -> None:
25+
self._notes.append(note)
26+
27+
def get_note(self, cls: Type[N]) -> Optional[N]:
28+
"""Get the latest added note of type ``cls`` or its descendants."""
29+
for note in reversed(self._notes):
30+
if isinstance(note, cls):
31+
return note
32+
return None
33+
1834
@property
1935
def supported(self) -> bool:
2036
return True

0 commit comments

Comments
 (0)