Skip to content

Commit

Permalink
Repr for columninfo
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasaarholt committed Apr 7, 2024
1 parent fd2f49e commit b51f63d
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/patito/_pydantic/column_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ class ColumnInfo(BaseModel, arbitrary_types_allowed=True):
derived_from: Optional[Union[str, pl.Expr]] = None
unique: Optional[bool] = None

def __repr__(self) -> str:
"""Print only Field attributes whose values are not default (mainly None)."""
not_default_field = {
field: getattr(self, field)
for field in self.model_fields
if getattr(self, field) is not self.model_fields[field].default
}

string = ""
for field, value in not_default_field.items():
string += f"{field}={value}, "
if string:
# remove trailing comma and space
string = string[:-2]
return f"ColumnInfo({string})"

@field_serializer("constraints", "derived_from")
def serialize_exprs(self, exprs: str | pl.Expr | Sequence[pl.Expr] | None) -> Any:
if exprs is None:
Expand Down

0 comments on commit b51f63d

Please sign in to comment.