Best Python code snippet using localstack_python
type_inference.py
Source:type_inference.py
...116 pass117 else:118 id_to_computation[comp.id] = comp119 closed_list.add(comp.id)120 comp.type = _apply_substitutions(comp.type, substitutions)121 for arg in comp.args:122 set_types_rec(arg)123 for c in inferred_computations:124 set_types_rec(c)125 new_expressions = []126 for expr in orig_program.expressions:127 computation = id_to_computation[expr.id]128 new_expressions.append(129 replace(130 expr,131 type=_type_to_type_name(computation.return_type),132 type_args=[133 # TODO handle the case where there remain free type variables134 _type_to_type_name(_apply_substitutions(t, substitutions))135 for t in computation.type_args136 ]137 if len(computation.type_args) > 0138 else None,139 )140 )141 return Program(new_expressions)142def _infer_types_rec(143 computation: Computation,144 library: Dict[str, Definition],145 substitutions: Dict[TypeVariable, Type],146) -> Computation:147 inferred_args = [148 _infer_types_rec(arg, library, substitutions).return_type149 for arg in computation.args150 ]151 actual_type = TypeApplication(LAMBDA, inferred_args + [computation.return_type])152 unified = _unify(actual_type, computation.type, substitutions)153 assert isinstance(154 unified, TypeApplication155 ), "Unification of lambdas should always produce a TypeApplication"156 computation.type = unified157 return computation158def _to_computations(159 program: Program,160 library: Dict[str, Definition],161 substitutions: Dict[TypeVariable, Type],162 id_to_expr: Dict[str, Expression],163) -> Sequence[Computation]:164 closed_list: Dict[str, Computation] = {}165 def rec(expression: Expression) -> Computation:166 if expression.id in closed_list:167 return closed_list[expression.id]168 rec_args = [rec(id_to_expr[arg_expr_id]) for arg_expr_id in expression.arg_ids]169 if isinstance(expression.op, (CallLikeOp, BuildStructOp)):170 if isinstance(expression.op, CallLikeOp):171 op = expression.op.name172 defn = library[op]173 defn_arg_types = [174 arg_type for (unused_arg_name, arg_type) in defn.params175 ]176 defn_type_name = defn.return_type177 elif isinstance(expression.op, BuildStructOp):178 assert (179 expression.op.empty_base180 ), "Can't handle empty_base in type inference"181 assert (182 expression.op.push_go183 ), "Can't handle non-push_go in type inference"184 op = expression.op.op_schema185 defn = library[op]186 arg_map = dict(defn.params)187 num_positional_args = 0188 while (189 num_positional_args < len(expression.op.op_fields)190 and expression.op.op_fields[num_positional_args] is None191 ):192 num_positional_args += 1193 named_args = []194 for named_arg in expression.op.op_fields[num_positional_args:]:195 defn_arg_type = arg_map[named_arg]196 named_args.append(defn_arg_type)197 defn_arg_types = [198 arg_type for (name, arg_type) in defn.params[:num_positional_args]199 ] + named_args200 defn_type_name = defn.return_type201 declared_type_args_list = [202 NamedTypeVariable(arg_name) for arg_name in defn.type_params203 ]204 declared_type_args = {var.name: var for var in declared_type_args_list}205 defn_type = _definition_to_type(206 defn_type_name, defn_arg_types, declared_type_args207 )208 assert expression.type_args is None or len(expression.type_args) == len(209 defn.type_params210 ), f"Must either have no type arguments or the same number as the function declaration, but got {expression.type_args} and {defn.type_params}"211 elif isinstance(expression.op, ValueOp):212 value_info = json.loads(expression.op.value)213 type_name = value_info["schema"]214 op = value_info["underlying"]215 defn = None216 declared_type_args_list = []217 def mk_primitive_constructor(p: str) -> TypeApplication:218 return TypeApplication(LAMBDA, [TypeApplication(p)])219 if type_name in ("String", "Long", "Number", "Boolean"):220 defn_type = mk_primitive_constructor(type_name)221 else:222 raise TypeInferenceError(f"Unknown primitive type {type_name}")223 else:224 assert False, f"Unexpected op {expression.op}"225 ascribed_return_type = (226 _type_name_to_type(expression.type, {})227 if expression.type228 else AnonTypeVariable()229 )230 if expression.type_args:231 for (ascribed_type_arg, type_var) in zip(232 expression.type_args, declared_type_args_list233 ):234 substitutions[type_var] = _type_name_to_type(ascribed_type_arg, {})235 anon_arg_types = (236 [cast(Type, AnonTypeVariable()) for arg in defn_arg_types] if defn else []237 )238 ascribed_type = TypeApplication(LAMBDA, anon_arg_types + [ascribed_return_type])239 comp_type = _unify(ascribed_type, defn_type, substitutions)240 assert isinstance(241 comp_type, TypeApplication242 ), "unification of Lambdas should always produce a TypeApplication"243 return Computation(244 op, expression.id, rec_args, declared_type_args_list, comp_type245 )246 (roots, _) = roots_and_reentrancies(program)247 return [rec(id_to_expr[root_id]) for root_id in roots]248def _definition_to_type(249 def_type: TypeName,250 declared_args: Sequence[TypeName],251 declared_type_args: Dict[str, NamedTypeVariable],252) -> Type:253 return_type = _type_name_to_type(def_type, declared_type_args)254 arg_types = [_type_name_to_type(arg, declared_type_args) for arg in declared_args]255 return TypeApplication(LAMBDA, arg_types + [return_type])256def _type_name_to_type(257 name: TypeName, declared_type_args: Dict[str, NamedTypeVariable]258) -> Type:259 if name is None:260 return AnonTypeVariable()261 if len(name.type_args) == 0:262 if name.base in declared_type_args:263 return declared_type_args[name.base]264 else:265 return TypeApplication(name.base, [])266 else:267 args = [_type_name_to_type(arg, declared_type_args) for arg in name.type_args]268 return TypeApplication(name.base, args)269def _type_to_type_name(t: Type) -> TypeName:270 assert isinstance(271 t, TypeApplication272 ), f"_type_to_type_name expects a Type that is fully instantiated with no variables, but got {t}"273 return TypeName(t.constructor, tuple([_type_to_type_name(a) for a in t.args]))274def _apply_substitutions(t: Type, substitutions: Dict[TypeVariable, Type]):275 if isinstance(t, TypeVariable):276 if t in substitutions:277 return _apply_substitutions(substitutions[t], substitutions)278 else:279 return t280 elif isinstance(t, TypeApplication):281 return TypeApplication(282 t.constructor, [_apply_substitutions(arg, substitutions) for arg in t.args]283 )284 else:285 raise TypeInferenceError(f"Unknown type {t}")286def _unify(type1: Type, type2: Type, substitutions: Dict[TypeVariable, Type]) -> Type:287 """Returns a type that substitutes any TypeVariables in of `type1` or `type2`288 with corresponding TypeApplications in the other argument.289 The substitutions are mutably recorded in `substitutions`.290 Before recursively unifying, this function first applies any substitutions already291 present in `substitutions` to both arguments.292 See e.g. section 2.2 of293 https://course.ccs.neu.edu/cs4410sp19/lec_type-inference_notes.html294 """295 type1 = _apply_substitutions(type1, substitutions)296 type2 = _apply_substitutions(type2, substitutions)297 def rec(t1: Type, t2: Type) -> Type:298 if t1 == t2:299 return t1300 # If either t1 or t2 is a type variable and it does not occur in the other type,301 # then produce the substitution that binds it to the other type.302 elif isinstance(t2, TypeVariable) and not isinstance(t1, TypeVariable):303 # Lol, pylint thinks that because t1 and t2 match the param names but are304 # out of order, there might be a bug.305 # pylint: disable=arguments-out-of-order306 return rec(t2, t1)307 elif isinstance(t1, TypeVariable) and not _occurs(t2, t1):308 substitutions[t1] = t2309 return t2310 # If we have two type applications of the same arity,...
test_cloudformation.py
Source:test_cloudformation.py
...39 for url in local_urls:40 assert template_preparer.is_local_service_url(url)41 for url in remote_urls:42 assert not template_preparer.is_local_service_url(url)43def test_apply_substitutions():44 blubstr = "something ${foo} and ${test} + ${foo}"45 subs = {"foo": "bar", "test": "resolved"}...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!