2020from dataclasses import dataclass , field
2121
2222from monic .expressions .context import ExpressionsContext
23- from monic .expressions .exceptions import (
24- SecurityError ,
25- UnsupportedUnpackingError ,
26- )
23+ from monic .expressions .exceptions import SecurityError
2724from monic .expressions .registry import registry
2825
2926
@@ -178,7 +175,6 @@ def __init__(self, context: ExpressionsContext | None = None) -> None:
178175 "TimeoutError" : TimeoutError ,
179176 "RuntimeError" : RuntimeError ,
180177 "SecurityError" : SecurityError ,
181- "UnsupportedUnpackingError" : UnsupportedUnpackingError ,
182178 }
183179
184180 # Add registered objects to global environment
@@ -622,8 +618,7 @@ def _handle_unpacking_target(self, target: ast.AST, value: t.Any) -> None:
622618 value: The value being assigned
623619
624620 Raises:
625- UnsupportedUnpackingError: If an unsupported unpacking pattern is
626- encountered
621+ TypeError: If an unsupported unpacking pattern is encountered
627622 """
628623 if isinstance (target , ast .Name ):
629624 self ._handle_name_target (target , value )
@@ -634,9 +629,7 @@ def _handle_unpacking_target(self, target: ast.AST, value: t.Any) -> None:
634629 elif isinstance (target , (ast .Tuple , ast .List )):
635630 self ._handle_sequence_unpacking (target , value )
636631 else :
637- raise UnsupportedUnpackingError (
638- f"Unsupported unpacking target type: { type (target ).__name__ } "
639- )
632+ raise TypeError (f"cannot unpack { type (target ).__name__ } " )
640633
641634 def _handle_sequence_unpacking (
642635 self ,
@@ -650,13 +643,17 @@ def _handle_sequence_unpacking(
650643 value: Value to unpack
651644
652645 Raises:
653- ValueError: If value cannot be unpacked
654- UnsupportedUnpackingError: If unpacking pattern is not supported
646+ TypeError: If value cannot be unpacked
647+ ValueError: If there are too many or too few values to unpack
648+ SyntaxError: If multiple starred expressions are used
655649 """
656650 with ScopeContext (self ):
657651 try :
658652 if not hasattr (value , "__iter__" ):
659- raise ValueError ("Cannot unpack non-iterable value" )
653+ raise TypeError (
654+ f"cannot unpack non-iterable { type (value ).__name__ } "
655+ "object"
656+ )
660657
661658 # Check for starred expressions (extended unpacking)
662659 starred_indices = [
@@ -666,8 +663,8 @@ def _handle_sequence_unpacking(
666663 ]
667664
668665 if len (starred_indices ) > 1 :
669- raise UnsupportedUnpackingError (
670- "Cannot use multiple starred expressions in assignment"
666+ raise SyntaxError (
667+ "multiple starred expressions in assignment"
671668 )
672669
673670 if starred_indices :
@@ -683,15 +680,21 @@ def _handle_sequence_unpacking(
683680 # Standard unpacking without starred expression
684681 value_list = list (value )
685682 if len (value_list ) < len (target .elts ):
686- raise ValueError ("Not enough values to unpack" )
683+ raise ValueError (
684+ "not enough values to unpack (expected "
685+ f"{ len (target .elts )} , got { len (value_list )} )"
686+ )
687687 elif len (value_list ) > len (target .elts ):
688- raise ValueError ("Too many values to unpack" )
688+ raise ValueError (
689+ "too many values to unpack (expected "
690+ f"{ len (target .elts )} )"
691+ )
689692
690693 # Unpack each element
691694 for tgt , val in zip (target .elts , value ):
692695 self ._handle_unpacking_target (tgt , val )
693- except (TypeError , ValueError ) as e :
694- raise UnsupportedUnpackingError (str (e )) from e
696+ except (TypeError , ValueError , SyntaxError ) as e :
697+ raise type ( e ) (str (e )) from e
695698
696699 def _handle_starred_unpacking (
697700 self ,
@@ -710,7 +713,7 @@ def _handle_starred_unpacking(
710713
711714 Raises:
712715 ValueError: If there are not enough values to unpack
713- UnsupportedUnpackingError : If unpacking pattern is not supported
716+ TypeError : If target is not a valid unpacking target
714717 """
715718 with ScopeContext (self ):
716719 iter_value = iter (value )
@@ -721,7 +724,7 @@ def _handle_starred_unpacking(
721724 try :
722725 self ._handle_unpacking_target (tgt , next (iter_value ))
723726 except StopIteration as e :
724- raise ValueError ("Not enough values to unpack" ) from e
727+ raise ValueError ("not enough values to unpack" ) from e
725728
726729 # Collect remaining elements for the starred target
727730 starred_values = list (iter_value )
@@ -733,7 +736,7 @@ def _handle_starred_unpacking(
733736 if after_star_count > 0 :
734737 # Make sure there are enough elements
735738 if len (starred_values ) < after_star_count :
736- raise ValueError ("Not enough values to unpack" )
739+ raise ValueError ("not enough values to unpack" )
737740
738741 # Separate starred values
739742 starred_list = starred_values [:- after_star_count ]
@@ -742,6 +745,8 @@ def _handle_starred_unpacking(
742745 # Assign starred target
743746 if isinstance (starred_target .value , ast .Name ):
744747 self ._set_name_value (starred_target .value .id , starred_list )
748+ else :
749+ raise TypeError ("starred assignment target must be a name" )
745750
746751 # Assign elements after starred
747752 after_elements = target_elts [star_index + 1 :]
@@ -754,6 +759,8 @@ def _handle_starred_unpacking(
754759 self ._set_name_value (
755760 starred_target .value .id , starred_values
756761 )
762+ else :
763+ raise TypeError ("starred assignment target must be a name" )
757764
758765 def visit_NamedExpr (self , node : ast .NamedExpr ) -> t .Any :
759766 """Handle named expressions (walrus operator).
@@ -1725,7 +1732,7 @@ def process_generator(
17251732 self ._handle_unpacking_target (
17261733 generator .target , item
17271734 )
1728- except UnsupportedUnpackingError :
1735+ except ( TypeError , ValueError , SyntaxError ) :
17291736 if isinstance (generator .target , ast .Name ):
17301737 self ._set_name_value (generator .target .id , item )
17311738 else :
@@ -1803,14 +1810,16 @@ def _process_generator_item(
18031810 bool: Whether all conditions are met
18041811
18051812 Raises:
1806- UnsupportedUnpackingError: If target unpacking fails
1813+ TypeError: If value is not iterable
1814+ ValueError: If target unpacking fails
1815+ SyntaxError: If multiple starred expressions are used
18071816 """
18081817 # Restore environment from before this generator's loop
18091818 self .local_env = current_env .copy ()
18101819
18111820 try :
18121821 self ._handle_unpacking_target (generator .target , item )
1813- except UnsupportedUnpackingError :
1822+ except ( TypeError , ValueError , SyntaxError ) :
18141823 if isinstance (generator .target , ast .Name ):
18151824 self ._set_name_value (generator .target .id , item )
18161825 else :
0 commit comments