66 Optional ,
77 Tuple ,
88 TYPE_CHECKING ,
9+ Union ,
910)
1011
1112from sqlalchemy .exc import IntegrityError
2930
3031if TYPE_CHECKING :
3132 from galaxy .model import User
33+ from galaxy .model .store import SessionlessContext
3234
3335log = logging .getLogger (__name__ )
3436
@@ -46,7 +48,9 @@ class TagHandler:
4648 Manages CRUD operations related to tagging objects.
4749 """
4850
49- def __init__ (self , sa_session : scoped_session , galaxy_session : Optional [GalaxySession ] = None ) -> None :
51+ def __init__ (
52+ self , sa_session : Union [scoped_session , "SessionlessContext" ], galaxy_session : Optional [GalaxySession ] = None
53+ ) -> None :
5054 self .sa_session = sa_session
5155 # Minimum tag length.
5256 self .min_tag_len = 1
@@ -64,7 +68,10 @@ def __init__(self, sa_session: scoped_session, galaxy_session: Optional[GalaxySe
6468
6569 def create_tag_handler_session (self , galaxy_session : Optional [GalaxySession ]):
6670 # Creates a transient tag handler that avoids repeated flushes
67- return GalaxyTagHandlerSession (self .sa_session , galaxy_session = galaxy_session )
71+ if isinstance (self .sa_session , scoped_session ):
72+ return GalaxyTagHandlerSession (self .sa_session , galaxy_session = galaxy_session )
73+ else :
74+ return self
6875
6976 def add_tags_from_list (self , user , item , new_tags_list , flush = True ):
7077 new_tags_set = set (new_tags_list )
@@ -327,7 +334,7 @@ def _get_tag(self, tag_name):
327334 def _create_tag_instance (self , tag_name ):
328335 # For good performance caller should first check if there's already an appropriate tag
329336 tag = Tag (type = 0 , name = tag_name )
330- if not self .sa_session :
337+ if not isinstance ( self .sa_session , scoped_session ) :
331338 return tag
332339 Session = sessionmaker (self .sa_session .bind )
333340 with Session () as separate_session :
0 commit comments