Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Using SQLAlchemy
Lecture: Adding type hints to the session factory

Login or purchase this course to watch this video and the rest of the course contents.
0:00 I guess, real, real quick, let's make this a little bit nicer. I don't like that this doesn't give us much information about what comes out of it
0:09 and it's not super easy to get that to happen, so let's do this. Let's define a function called create_session(). That's more obvious.
0:16 And it's going to return a session object that comes from sqlalchemy.orm and for now it's just going to say return __factory(), like this.
0:28 Let's tell it that this is global, like that. Okay, this is pretty good. But of course when we now go to db_session. Uou still see it.
0:39 You still see it at the top and I'd kind of like that to not be the case so let's refactor rename this
0:46 to double underscore and you see it renamed it everywhere. And now, is it gone? Ah, yeah. We just have create_session and global_init.
0:57 So where are we doing this factory we don't need any of this stuff anymore. That can all be gone, we just say = DBsession.createsession
1:06 the type annotation on the return value there should be enough so that when we say session. Boom, there it all is. Okay, that's it.
1:16 We're all good to go. We clear the session, we make the changes we call commit, beautiful.


Talk Python's Mastodon Michael Kennedy's Mastodon