Skip to content

Instantly share code, notes, and snippets.

@a-recknagel
Created July 5, 2022 08:51
Show Gist options
  • Save a-recknagel/5eb703d443296f534eca47d3d9095a9f to your computer and use it in GitHub Desktop.
Save a-recknagel/5eb703d443296f534eca47d3d9095a9f to your computer and use it in GitHub Desktop.
how I'd like a no-op type to behave
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