@@ -257,6 +257,20 @@ def mp_length_impl(self, tx: InstructionTranslatorBase) -> VariableTracker:
257257 """Mapping length - delegates to len_impl for constants."""
258258 return self .len_impl (tx )
259259
260+ def mp_subscript_impl (
261+ self ,
262+ tx : InstructionTranslatorBase ,
263+ key : VariableTracker ,
264+ ) -> VariableTracker :
265+ from .object_protocol import type_implements_mp_subscript
266+
267+ if type_implements_mp_subscript (type (self .value )) and key .is_python_constant ():
268+ try :
269+ return ConstantVariable .create (self .value [key .as_python_constant ()])
270+ except Exception as e :
271+ raise_observed_exception (type (e ), tx , args = list (e .args ))
272+ return super ().mp_subscript_impl (tx , key )
273+
260274 def const_getattr (
261275 self , tx : InstructionTranslatorBase , name : str
262276 ) -> VariableTracker :
@@ -267,6 +281,18 @@ def const_getattr(
267281 raise NotImplementedError
268282 return member
269283
284+ def sq_concat_impl (
285+ self , tx : InstructionTranslatorBase , other : VariableTracker
286+ ) -> VariableTracker :
287+ from .object_protocol import type_implements_sq_concat
288+
289+ if type_implements_sq_concat (type (self .value )) and other .is_python_constant ():
290+ try :
291+ return ConstantVariable .create (self .value + other .as_python_constant ())
292+ except Exception as e :
293+ raise_observed_exception (type (e ), tx , args = list (e .args ))
294+ return super ().sq_concat_impl (tx , other )
295+
270296 def sq_contains_impl (self , tx : InstructionTranslatorBase , item : VariableTracker ):
271297 """Sequence contains for constants."""
272298 if item .is_python_constant ():
@@ -468,6 +494,18 @@ def get_id(self, tx: InstructionTranslatorBase) -> int | None:
468494 def get_real_python_backed_value (self ) -> object :
469495 return self .value
470496
497+ def nb_bool_impl (
498+ self ,
499+ tx : InstructionTranslatorBase ,
500+ ) -> VariableTracker :
501+ # CPython: int, float, and bool define nb_bool (returns self for int,
502+ # bool(self) for float). All other constant types do not.
503+ from .object_protocol import type_implements_nb_bool
504+
505+ if type_implements_nb_bool (type (self .value )):
506+ return ConstantVariable .create (bool (self .value ))
507+ return super ().nb_bool_impl (tx )
508+
471509 def nb_index_impl (
472510 self ,
473511 tx : InstructionTranslatorBase ,
@@ -913,6 +951,11 @@ def tp_repr_impl(self, tx: InstructionTranslatorBase) -> VariableTracker:
913951 # FakeIdVariable already resolves same-kind id()/hash() comparisons.
914952 return ConstantVariable .create (repr (self .value ))
915953
954+ def nb_bool_impl (self , tx : InstructionTranslatorBase ) -> VariableTracker :
955+ # Mirrors long_bool. The fake value is only meaningful at compile time,
956+ # but its truthiness is, like tp_repr_impl, a plain function of it.
957+ return ConstantVariable .create (bool (self .value ))
958+
916959 def tp_richcompare_impl (
917960 self , tx : InstructionTranslatorBase , other : VariableTracker , op : str
918961 ) -> VariableTracker :
0 commit comments