From 449f6c58b508384485f2fe5fc9413a74c5f85e95 Mon Sep 17 00:00:00 2001 From: Steven Loria Date: Sat, 11 Jan 2025 10:05:07 -0500 Subject: [PATCH] Remove unnecessary usages of scoped_session from docs (#641) * Remove unnecessary usages of scoped_session from docs * Update changelog --- CHANGELOG.rst | 2 +- README.rst | 18 ++++++++++-------- docs/recipes.rst | 45 ++++++++++++++++++++++++--------------------- 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e0089e9..ba0e396 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -19,7 +19,7 @@ Other changes: Thanks :user:`GabrielC101` for the suggestion. * Docs: Document methods of `SQLAlchemySchema ` and `SQLAlchemyAutoSchema ` (:issue:`619`). -* Docs: Various documentation improvements (:pr:`635`, :pr:`636`, :pr:`639`). +* Docs: Various documentation improvements (:pr:`635`, :pr:`636`, :pr:`639`, :pr:`641`). 1.2.0 (2025-01-09) ++++++++++++++++++ diff --git a/README.rst b/README.rst index 549954a..14231cc 100644 --- a/README.rst +++ b/README.rst @@ -20,14 +20,13 @@ Declare your models DeclarativeBase, backref, relationship, - scoped_session, sessionmaker, ) from marshmallow_sqlalchemy import SQLAlchemySchema, auto_field engine = sa.create_engine("sqlite:///:memory:") - session = scoped_session(sessionmaker(bind=engine)) + Session = sessionmaker(engine) class Base(DeclarativeBase): @@ -112,17 +111,20 @@ Make sure to declare `Models` before instantiating `Schemas`. Otherwise `sqlalch author = Author(name="Chuck Paluhniuk") author_schema = AuthorSchema() book = Book(title="Fight Club", author=author) - session.add(author) - session.add(book) - session.commit() + + with Session() as session: + session.add(author) + session.add(book) + session.commit() dump_data = author_schema.dump(author) print(dump_data) # {'id': 1, 'name': 'Chuck Paluhniuk', 'books': [1]} - load_data = author_schema.load(dump_data, session=session) - print(load_data) - # + with Session() as session: + load_data = author_schema.load(dump_data, session=session) + print(load_data) + # Get it now ========== diff --git a/docs/recipes.rst b/docs/recipes.rst index 79ea4ca..2b99ff8 100644 --- a/docs/recipes.rst +++ b/docs/recipes.rst @@ -139,24 +139,26 @@ Serialization will look like this: from pprint import pprint - from sqlalchemy.orm import scoped_session, sessionmaker + from sqlalchemy.orm import sessionmaker engine = sa.create_engine("sqlite:///:memory:") - session = scoped_session(sessionmaker(bind=engine)) + Session = sessionmaker(engine) Base.metadata.create_all(engine) - user = User(full_name="Freddie Mercury") - post = BlogPost(title="Bohemian Rhapsody Revisited", author=user) - session.add_all([user, post]) - session.commit() + with Session() as session: + user = User(full_name="Freddie Mercury") + post = BlogPost(title="Bohemian Rhapsody Revisited", author=user) + + session.add_all([user, post]) + session.commit() - blog_post_schema = BlogPostSchema() - data = blog_post_schema.dump(post) - pprint(data, indent=2) - # { 'author': {'full_name': 'Freddie Mercury', 'id': 1}, - # 'id': 1, - # 'title': 'Bohemian Rhapsody Revisited'} + blog_post_schema = BlogPostSchema() + data = blog_post_schema.dump(post) + pprint(data, indent=2) + # { 'author': {'full_name': 'Freddie Mercury', 'id': 1}, + # 'id': 1, + # 'title': 'Bohemian Rhapsody Revisited'} Introspecting generated fields ============================== @@ -269,14 +271,14 @@ Usage: .. code-block:: python import sqlalchemy as sa - from sqlalchemy.orm import declarative_base, scoped_session, sessionmaker + from sqlalchemy.orm import declarative_base, sessionmaker from sqlalchemy import event from sqlalchemy.orm import mapper # Either import or declare setup_schema here engine = sa.create_engine("sqlite:///:memory:") - session = scoped_session(sessionmaker(bind=engine)) + Session = sessionmaker(engine) Base = declarative_base() @@ -295,15 +297,16 @@ Usage: Base.metadata.create_all(engine) - author = Author(name="Chuck Paluhniuk") - session.add(author) - session.commit() + with Session() as session: + author = Author(name="Chuck Paluhniuk") + session.add(author) + session.commit() - # Model.__marshmallow__ returns the Class not an instance of the schema - # so remember to instantiate it - author_schema = Author.__marshmallow__() + # Model.__marshmallow__ returns the Class not an instance of the schema + # so remember to instantiate it + author_schema = Author.__marshmallow__() - print(author_schema.dump(author)) + print(author_schema.dump(author)) This is inspired by functionality from `ColanderAlchemy `_.