Registry / database / sqlalchemy-repr

sqlalchemy-repr

JSON →
library0.1.0pypypi✓ verified 82d ago

sqlalchemy-repr, currently at version 0.1.0, is a lightweight Python library that automatically generates human-readable `__repr__` methods for SQLAlchemy declarative models. It aims to simplify debugging and logging by providing informative string representations of model instances without manual boilerplate. The project is stable but has a low release cadence.

pip install sqlalchemy-repr
INSTALL
IMPORT
SIG · SQLALCHEMY-REPR
S
sqlalchemy-repr
databasepythonv0.1.0
Install
4.1s avg
Import
Disk
42MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 43.9MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 4.1s · import 0.000s · 42MB
42MB installed
● package 42MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Repr
from sqlalchemy_repr import Repr
from sqlalchemy_repr import ReprMixin
PrettyRepr
from sqlalchemy_repr import PrettyRepr
RepresentableBase
from sqlalchemy_repr import RepresentableBase

This quickstart demonstrates how to integrate `ReprMixin` with a SQLAlchemy declarative model. By inheriting `ReprMixin` alongside `Base`, your model instances will automatically gain a sensible string representation. The example also shows how to customize the included attributes using `_repr_values`.

from sqlalchemy import create_engine, Column, Integer, String from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base from sqlalchemy_repr import ReprMixin # 1. Setup SQLAlchemy Base Base = declarative_base() # 2. Define a model with ReprMixin class User(ReprMixin, Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) name = Column(String) email = Column(String) # Optional: Customize which attributes are shown in repr _repr_values = ['id', 'name'] # 3. Create an engine and tables engine = create_engine('sqlite:///:memory:') Base.metadata.create_all(engine) # 4. Create a session Session = sessionmaker(bind=engine) session = Session() # 5. Create and add an instance user1 = User(id=1, name='Alice', email='alice@example.com') user2 = User(id=2, name='Bob', email='bob@example.com') session.add_all([user1, user2]) session.commit() # 6. Demonstrate the custom repr retrieved_user = session.query(User).filter_by(name='Alice').first() print(f"User instance repr: {retrieved_user}") # Expected output (approx): User(id=1, name='Alice') # Clean up session.close()
Debug
Known issues
gotchaThe project `sqlalchemy-repr` is stable but has a low level of active development. While it works reliably with existing SQLAlchemy versions, new SQLAlchemy features or major API changes might not be immediately supported.
fix
Monitor the project's GitHub repository for updates if using very new SQLAlchemy versions. Be prepared to implement custom `__repr__` methods or fork the library for compatibility if encountering issues with future SQLAlchemy releases.
affects: 0.1.0
gotchaBy default, `ReprMixin` includes all mapped columns in the `__repr__` output. For models containing sensitive data (e.g., passwords, API keys, private information), this could lead to unintended exposure in logs or during debugging.
fix
Explicitly define the `_repr_values` attribute on your model to control which columns are included in the representation. For example: `_repr_values = ['id', 'username']` to exclude sensitive fields like `password_hash`.
affects: all
gotchaFor models with complex, deeply nested, or circular relationships, relying solely on `ReprMixin`'s default behavior can lead to overly verbose representations or even infinite recursion if relationships are eagerly loaded.
fix
When defining models with such relationships, consider carefully setting `_repr_values` to exclude relationship attributes. For very complex cases, implement a custom `__repr__` method to ensure controlled and efficient output.
affects: all
Upgrade
Version history
0.1.0latest on PyPI · released Oct 20, 2022
Audit
Dependencies
SQLAlchemyrequiredRequired for ORM model integration.
Agent activity
17 hits · last 30 days
node
14
Meta
1
OpenAI (training)
1
Resources
sqlalchemy-repr — pip install sqlalchemy-repr · libregistry