Created
July 5, 2022 08:51
-
-
Save a-recknagel/5eb703d443296f534eca47d3d9095a9f to your computer and use it in GitHub Desktop.
how I'd like a no-op type to behave
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pydantic import BaseModel | |
Sensitive = ... | |
class Config(BaseModel): | |
user: str | |
password: Sensitive[str] | |
def to_anonymized_dict(self): | |
values = self.dict() | |
for name in values: | |
if isinstance(values[name], Sensitive): | |
value = str(values[name])[:] | |
peek = len(value) // 5 | |
values[name] = value[:peek] + ("*"*(len(value)-(2*peek))) + value[-peek:] | |
return values | |
cfg = Config(user="foo", password="12345678") # should create an instance as if `Sensitive[str]` were the same as `str` | |
print(cfg.to_anonymized_dict()) # {'user': 'foo', 'password': '1******8'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment