33from typing import (
44 Optional ,
55 TYPE_CHECKING ,
6+ Union ,
67)
78
89from sqlalchemy .exc import IntegrityError
2627
2728if TYPE_CHECKING :
2829 from galaxy .model import User
30+ from galaxy .model .store import SessionlessContext
2931
3032log = logging .getLogger (__name__ )
3133
@@ -43,7 +45,9 @@ class TagHandler:
4345 Manages CRUD operations related to tagging objects.
4446 """
4547
46- def __init__ (self , sa_session : scoped_session , galaxy_session : Optional [GalaxySession ] = None ) -> None :
48+ def __init__ (
49+ self , sa_session : Union [scoped_session , "SessionlessContext" ], galaxy_session : Optional [GalaxySession ] = None
50+ ) -> None :
4751 self .sa_session = sa_session
4852 # Minimum tag length.
4953 self .min_tag_len = 1
@@ -61,7 +65,10 @@ def __init__(self, sa_session: scoped_session, galaxy_session: Optional[GalaxySe
6165
6266 def create_tag_handler_session (self , galaxy_session : Optional [GalaxySession ]):
6367 # Creates a transient tag handler that avoids repeated flushes
64- return GalaxyTagHandlerSession (self .sa_session , galaxy_session = galaxy_session )
68+ if isinstance (self .sa_session , scoped_session ):
69+ return GalaxyTagHandlerSession (self .sa_session , galaxy_session = galaxy_session )
70+ else :
71+ return self
6572
6673 def add_tags_from_list (self , user , item , new_tags_list , flush = True ):
6774 new_tags_set = set (new_tags_list )
@@ -324,7 +331,7 @@ def _get_tag(self, tag_name):
324331 def _create_tag_instance (self , tag_name ):
325332 # For good performance caller should first check if there's already an appropriate tag
326333 tag = Tag (type = 0 , name = tag_name )
327- if not self .sa_session :
334+ if not isinstance ( self .sa_session , scoped_session ) :
328335 return tag
329336 Session = sessionmaker (self .sa_session .bind )
330337 with Session () as separate_session :
0 commit comments