@@ -86,8 +86,7 @@ def __init__(self, hub, layer):
8686
8787 def __enter__ (self ):
8888 scope = self ._layer [1 ]
89- if scope is None :
90- scope = Scope ()
89+ assert scope is not None
9190 return scope
9291
9392 def __exit__ (self , exc_type , exc_value , tb ):
@@ -271,21 +270,24 @@ def push_scope(self, callback=None):
271270 that should be used to pop the scope again. Alternatively a callback
272271 can be provided that is executed in the context of the scope.
273272 """
273+ if callback is not None :
274+ with self .push_scope () as scope :
275+ callback (scope )
276+ return
277+
274278 client , scope = self ._stack [- 1 ]
275279 new_layer = (client , copy .copy (scope ))
276280 self ._stack .append (new_layer )
277281
278- if callback is not None :
279- if client is not None :
280- callback (scope )
281- else :
282- return _ScopeManager (self , new_layer )
282+ return _ScopeManager (self , new_layer )
283+
284+ scope = push_scope
283285
284286 def pop_scope_unsafe (self ):
285287 """Pops a scope layer from the stack. Try to use the context manager
286288 `push_scope()` instead."""
287289 rv = self ._stack .pop ()
288- assert self ._stack
290+ assert self ._stack , "stack must have at least one layer"
289291 return rv
290292
291293 def configure_scope (self , callback = None ):
@@ -305,16 +307,6 @@ def inner():
305307
306308 return inner ()
307309
308- def scope (self , callback = None ):
309- """Pushes a new scope and yields it for configuration.
310-
311- The scope is dropped at the end of the with statement. Alternatively
312- a callback can be provided similar to `configure_scope`.
313- """
314- with self .push_scope ():
315- client , scope = self ._stack [- 1 ]
316- return self .configure_scope (callback )
317-
318310
319311GLOBAL_HUB = Hub ()
320312_local .set (GLOBAL_HUB )
0 commit comments