Best Python code snippet using avocado_python
type_inference_test.py
Source:type_inference_test.py
1# Copyright 2020 The TensorFlow Authors. All Rights Reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14# ==============================================================================15"""Tests for type_inference module."""16from typing import Any, Callable, List17from tensorflow.python.autograph.pyct import anno18from tensorflow.python.autograph.pyct import cfg19from tensorflow.python.autograph.pyct import qual_names20from tensorflow.python.autograph.pyct import transpiler21from tensorflow.python.autograph.pyct.static_analysis import activity22from tensorflow.python.autograph.pyct.static_analysis import reaching_definitions23from tensorflow.python.autograph.pyct.static_analysis import reaching_fndefs24from tensorflow.python.autograph.pyct.static_analysis import type_inference25from tensorflow.python.platform import test26class BasicTestResolver(type_inference.Resolver):27 """A very basic resolver for testing."""28 def res_name(self, ns, types_ns, name):29 str_name = str(name)30 if str_name == 'int':31 return {int}, int32 return {type(ns[str_name])}, ns[str_name]33 def res_value(self, ns, value):34 return {type(value)}35 def res_arg(self, ns, types_ns, f_name, name, type_anno, f_is_local):36 if type_anno is None:37 return None38 return {str(type_anno)}39class TestTranspiler(transpiler.GenericTranspiler):40 def __init__(self, resolver_type):41 super().__init__()42 self.resolver = resolver_type()43 def get_transformed_name(self, _):44 return 'test_item'45 def transform_ast(self, node, ctx):46 node = qual_names.resolve(node)47 node = activity.resolve(node, ctx)48 graphs = cfg.build(node)49 node = reaching_definitions.resolve(node, ctx, graphs)50 node = reaching_fndefs.resolve(node, ctx, graphs)51 node = type_inference.resolve(node, ctx, graphs, self.resolver)52 return node53class TypeInferenceAnalyzerTest(test.TestCase):54 def assertTypes(self, node, expected):55 if not isinstance(expected, tuple):56 expected = expected,57 self.assertSetEqual(58 set(anno.getanno(node, anno.Static.TYPES)), set(expected))59 def assertClosureTypes(self, node, expected):60 actual = anno.getanno(node, anno.Static.CLOSURE_TYPES)61 actual = {str(k): v for k, v in actual.items()}62 for k, v in expected.items():63 self.assertIn(k, actual)64 self.assertEqual(actual[k], v)65 def test_no_inference_on_unknown_operand_types(self):66 class Resolver(type_inference.Resolver):67 def res_arg(self, ns, types_ns, f_name, name, type_anno, f_is_local):68 return None69 def test_fn(a, b):70 return a < b, a - b71 node, _ = TestTranspiler(Resolver).transform(test_fn, None)72 fn_body = node.body73 # With no information on operand types, the operators will infer nothing.74 self.assertFalse(75 anno.hasanno(fn_body[0].value.elts[0], anno.Static.TYPES))76 self.assertFalse(77 anno.hasanno(fn_body[0].value.elts[1], anno.Static.TYPES))78 def test_resolver_output_checked(self):79 class Resolver(type_inference.Resolver):80 def res_arg(self, ns, types_ns, f_name, name, type_anno, f_is_local):81 return 182 def test_fn(a):83 del a84 pass85 with self.assertRaisesRegex(ValueError, 'expected to return set'):86 TestTranspiler(Resolver).transform(test_fn, None)87 def test_argument(self):88 test_self = self89 class Resolver(type_inference.Resolver):90 def res_arg(self, ns, types_ns, f_name, name, type_anno, f_is_local):91 test_self.assertFalse(f_is_local)92 if name == qual_names.QN('a'):93 test_self.assertEqual(type_anno, qual_names.QN('int'))94 return {str(name) + '_type'}95 def test_fn(a: int, b):96 return a, b97 node, _ = TestTranspiler(Resolver).transform(test_fn, None)98 fn_body = node.body99 self.assertTypes(fn_body[0].value.elts[0], 'a_type')100 self.assertTypes(fn_body[0].value.elts[1], 'b_type')101 def test_argument_of_local_function(self):102 test_self = self103 class Resolver(type_inference.Resolver):104 def res_arg(self, ns, types_ns, f_name, name, type_anno, f_is_local):105 if f_name == 'test_fn':106 test_self.assertFalse(f_is_local)107 test_self.assertEqual(name, qual_names.QN('a'))108 test_self.assertEqual(type_anno, qual_names.QN('int'))109 elif f_name == 'foo':110 test_self.assertTrue(f_is_local)111 if name == qual_names.QN('x'):112 test_self.assertEqual(type_anno, qual_names.QN('float'))113 elif name == qual_names.QN('y'):114 test_self.assertIsNone(type_anno)115 else:116 test_self.fail('unexpected argument {} for {}'.format(name, f_name))117 else:118 test_self.fail('unexpected function name {}'.format(f_name))119 return {str(name) + '_type'}120 def test_fn(a: int):121 def foo(x: float, y):122 return x, y123 return foo(a, a)124 tr = TestTranspiler(Resolver)125 node, _ = tr.transform(test_fn, None)126 fn_body = node.body127 self.assertTypes(fn_body[0].body[0].value, (('x_type', 'y_type'),))128 self.assertTypes(fn_body[0].body[0].value.elts[0], 'x_type')129 self.assertTypes(fn_body[0].body[0].value.elts[1], 'y_type')130 def test_assign_straightline(self):131 def test_fn(a: int, c: float):132 b = a133 return a, b, c134 node, _ = TestTranspiler(BasicTestResolver).transform(test_fn, None)135 fn_body = node.body136 self.assertTypes(fn_body[0].targets[0], 'int')137 self.assertTypes(fn_body[0].value, 'int')138 self.assertTypes(fn_body[1].value.elts[0], 'int')139 self.assertTypes(fn_body[1].value.elts[1], 'int')140 self.assertTypes(fn_body[1].value.elts[2], 'float')141 def test_expr(self):142 test_self = self143 class Resolver(type_inference.Resolver):144 def res_value(self, ns, value):145 test_self.assertEqual(value, tc.a)146 return {str}147 def res_name(self, ns, types_ns, name):148 test_self.assertEqual(name, qual_names.QN('tc'))149 return {TestClass}, tc150 def res_call(self, ns, types_ns, node, f_type, args, keywords):151 test_self.assertEqual(f_type, (str,))152 return {int}, None153 class TestClass:154 def a(self):155 pass156 tc = TestClass()157 def test_fn():158 tc.a()159 node, _ = TestTranspiler(Resolver).transform(test_fn, None)160 fn_body = node.body161 self.assertEqual(162 anno.getanno(fn_body[0].value.func, anno.Static.VALUE), tc.a)163 self.assertTypes(fn_body[0].value.func, str)164 self.assertTypes(fn_body[0].value, int)165 self.assertTypes(fn_body[0], int)166 def test_assign_overwriting(self):167 def test_fn(a: int, b: float):168 c = a169 c = b170 return c171 node, _ = TestTranspiler(BasicTestResolver).transform(test_fn, None)172 fn_body = node.body173 self.assertTypes(fn_body[0].targets[0], 'int')174 self.assertTypes(fn_body[0].value, 'int')175 self.assertTypes(fn_body[1].targets[0], 'float')176 self.assertTypes(fn_body[1].value, 'float')177 def test_dynamic_attribute_of_static_value(self):178 test_self = self179 class Resolver(type_inference.Resolver):180 def res_value(self, ns, value):181 test_self.assertEqual(value, tc.a)182 return {int}183 def res_name(self, ns, types_ns, name):184 test_self.assertEqual(name, qual_names.QN('tc'))185 return {TestClass}, tc186 class TestClass:187 def __init__(self):188 self.a = 1189 tc = TestClass()190 def test_fn():191 return tc.a192 node, _ = TestTranspiler(Resolver).transform(test_fn, None)193 fn_body = node.body194 self.assertTypes(fn_body[0].value.value, TestClass)195 self.assertTypes(fn_body[0].value, int)196 self.assertIs(anno.getanno(fn_body[0].value.value, anno.Static.VALUE), tc)197 self.assertEqual(anno.getanno(fn_body[0].value, anno.Static.VALUE), tc.a)198 def test_static_attribute_of_typed_value(self):199 test_self = self200 class TestClass:201 a = 1202 tc = TestClass()203 class Resolver(type_inference.Resolver):204 def res_name(self, ns, types_ns, name):205 test_self.assertEqual(name, qual_names.QN('tc'))206 return {TestClass}, None207 def res_value(self, ns, value):208 test_self.assertIs(value, tc.a)209 return {str}210 def test_fn():211 return tc.a212 node, _ = TestTranspiler(Resolver).transform(test_fn, None)213 fn_body = node.body214 self.assertTypes(fn_body[0].value.value, TestClass)215 self.assertTypes(fn_body[0].value, str) # Resolver is SOT216 self.assertFalse(anno.hasanno(fn_body[0].value.value, anno.Static.VALUE))217 self.assertEqual(anno.getanno(fn_body[0].value, anno.Static.VALUE), 1)218 def test_static_attribute_of_ambiguous_type(self):219 test_self = self220 class TestClass1:221 a = 1222 class TestClass2:223 a = 2224 tc = TestClass1()225 class Resolver(type_inference.Resolver):226 def res_name(self, ns, types_ns, name):227 test_self.assertEqual(name, qual_names.QN('tc'))228 return {TestClass1, TestClass2}, None229 def res_value(self, ns, value):230 test_self.assertIn(value, (1, 2))231 return {str}232 def test_fn():233 return tc.a234 node, _ = TestTranspiler(Resolver).transform(test_fn, None)235 fn_body = node.body236 self.assertTypes(fn_body[0].value.value, (TestClass1, TestClass2))237 self.assertFalse(anno.hasanno(fn_body[0].value, anno.Static.TYPES))238 self.assertFalse(anno.hasanno(fn_body[0].value.value, anno.Static.VALUE))239 self.assertFalse(anno.hasanno(fn_body[0].value, anno.Static.VALUE))240 def test_property_of_typed_value(self):241 test_self = self242 class TestClass:243 @property244 def a(self):245 return 1246 tc = TestClass()247 class Resolver(type_inference.Resolver):248 def res_name(self, ns, types_ns, name):249 test_self.assertEqual(name, qual_names.QN('tc'))250 return {TestClass}, None251 def res_value(self, ns, value):252 test_self.assertIs(value, TestClass.a)253 test_self.assertNotEqual(value, 1) # Can't evaluate property of class.254 return {property}255 def test_fn():256 return tc.a257 node, _ = TestTranspiler(Resolver).transform(test_fn, None)258 fn_body = node.body259 self.assertTypes(fn_body[0].value.value, TestClass)260 self.assertTypes(fn_body[0].value, property)261 self.assertFalse(anno.hasanno(fn_body[0].value.value, anno.Static.VALUE))262 self.assertEqual(263 anno.getanno(fn_body[0].value, anno.Static.VALUE), TestClass.a)264 def test_dynamic_attribute_of_typed_value(self):265 test_self = self266 class TestClass:267 def __init__(self):268 self.a = 1269 tc = TestClass()270 class Resolver(type_inference.Resolver):271 def res_name(self, ns, types_ns, name):272 test_self.assertEqual(name, qual_names.QN('tc'))273 return {TestClass}, None274 def test_fn():275 return tc.a276 node, _ = TestTranspiler(Resolver).transform(test_fn, None)277 fn_body = node.body278 self.assertTypes(fn_body[0].value.value, TestClass)279 self.assertFalse(anno.hasanno(fn_body[0].value, anno.Static.TYPES))280 self.assertFalse(anno.hasanno(fn_body[0].value.value, anno.Static.VALUE))281 self.assertFalse(anno.hasanno(fn_body[0].value, anno.Static.VALUE))282 def test_external_value(self):283 a = 'foo'284 def test_fn():285 b = a286 return b287 node, _ = TestTranspiler(BasicTestResolver).transform(test_fn, None)288 fn_body = node.body289 self.assertTypes(fn_body[0].targets[0], str)290 self.assertTypes(fn_body[1].value, str)291 def test_external_function(self):292 test_self = self293 class Resolver(type_inference.Resolver):294 def res_name(self, ns, types_ns, name):295 test_self.assertEqual(name, qual_names.QN('g'))296 return {str}, g297 def res_call(self, ns, types_ns, node, f_type, args, keywords):298 test_self.assertEqual(f_type, (str,))299 test_self.assertEqual(300 anno.getanno(node.func, anno.Basic.QN), qual_names.QN('g'))301 return {float}, None302 def g() -> float:303 return 1.0304 def test_fn():305 a = g()306 return a307 node, _ = TestTranspiler(Resolver).transform(test_fn, None)308 fn_body = node.body309 self.assertTypes(fn_body[0].value.func, str)310 self.assertTypes(fn_body[0].targets[0], float)311 self.assertTypes(fn_body[1].value, float)312 def test_external_function_side_effects(self):313 test_self = self314 class Resolver(type_inference.Resolver):315 def res_name(self, ns, types_ns, name):316 test_self.assertEqual(name, qual_names.QN('g'))317 return None, g318 def res_arg(self, ns, types_ns, f_name, name, type_anno, f_is_local):319 return {str(type_anno)}320 def res_call(self, ns, types_ns, node, f_type, args, keywords):321 test_self.assertIsNone(f_type)322 return None, {qual_names.QN('x'): {str}}323 def g():324 # The resolver will pretend that this function has the following body:325 #326 # nonlocal x327 # x = 'a'328 pass329 def test_fn(x: int):330 y = x331 g()332 return x, y333 node, _ = TestTranspiler(Resolver).transform(test_fn, None)334 fn_body = node.body335 self.assertTypes(fn_body[0].targets[0], 'int')336 self.assertTypes(fn_body[0].value, 'int')337 self.assertTypes(fn_body[2].value.elts[0], str)338 self.assertTypes(fn_body[2].value.elts[1], 'int')339 def test_local_function_closure(self):340 def test_fn(x: int):341 def foo():342 return x343 foo()344 node, _ = TestTranspiler(BasicTestResolver).transform(test_fn, None)345 fn_body = node.body346 self.assertTypes(fn_body[0].body[0].value, 'int')347 self.assertClosureTypes(fn_body[0], {'x': {'int'}})348 def test_local_function_closure_nested(self):349 def test_fn(x: int):350 def foo():351 def bar():352 return x353 bar()354 foo()355 node, _ = TestTranspiler(BasicTestResolver).transform(test_fn, None)356 fn_body = node.body357 self.assertTypes(fn_body[0].body[0].body[0].value, 'int')358 self.assertClosureTypes(fn_body[0], {'x': {'int'}})359 self.assertClosureTypes(fn_body[0].body[0], {'x': {'int'}})360 def test_local_function_closure_mutable_var(self):361 def test_fn(x: int):362 def foo():363 nonlocal x364 return x365 foo()366 node, _ = TestTranspiler(BasicTestResolver).transform(test_fn, None)367 fn_body = node.body368 self.assertTypes(fn_body[0].body[1].value, 'int')369 self.assertClosureTypes(fn_body[0], {'x': {'int'}})370 def test_local_function_closure_ignored_for_bound_symbols(self):371 def test_fn(x: float): # pylint:disable=unused-argument372 def foo():373 x = x + 1 # pylint:disable=used-before-assignment374 foo()375 node, _ = TestTranspiler(BasicTestResolver).transform(test_fn, None)376 fn_body = node.body377 self.assertFalse(378 anno.hasanno(fn_body[0].body[0].value.left, anno.Static.TYPES))379 self.assertClosureTypes(fn_body[0], {'x': {'float'}})380 def test_local_function_closure_uses_call_site_types(self):381 def test_fn(x: int):382 def foo():383 return x384 x = 1.0385 foo()386 node, _ = TestTranspiler(BasicTestResolver).transform(test_fn, None)387 fn_body = node.body388 self.assertTypes(fn_body[0].body[0].value, float)389 self.assertTypes(fn_body[1].targets[0], float)390 self.assertClosureTypes(fn_body[0], {'x': {float}})391 def test_local_function_hides_locals(self):392 def test_fn(a: int): # pylint:disable=unused-argument393 def local_fn(v):394 a = v395 return a396 local_fn(1)397 node, _ = TestTranspiler(BasicTestResolver).transform(test_fn, None)398 fn_body = node.body399 self.assertFalse(400 anno.hasanno(fn_body[0].body[0].targets[0], anno.Static.TYPES))401 def test_local_function_type(self):402 def test_fn(x: int):403 def foo() -> int:404 return x405 foo()406 node, _ = TestTranspiler(BasicTestResolver).transform(test_fn, None)407 fn_body = node.body408 self.assertTypes(fn_body[1].value.func, Callable[[Any], int])409 self.assertTypes(fn_body[1].value, int)410 self.assertTypes(fn_body[1], int)411 def test_side_effects_on_arg_function_closure(self):412 test_self = self413 class Resolver(type_inference.Resolver):414 def res_name(self, ns, types_ns, name):415 test_self.assertEqual(name, qual_names.QN('g'))416 return {Callable[[Callable], None]}, g417 def res_value(self, ns, value):418 test_self.assertEqual(value, 1.0)419 return {float}420 def res_arg(self, ns, types_ns, f_name, name, type_anno, f_is_local):421 return {str(type_anno)}422 def res_call(self, ns, types_ns, node, f_type, args, keywords):423 test_self.assertEqual(node.func.id, 'g')424 test_self.assertEqual(f_type, (Callable[[Callable], None],))425 return None, {qual_names.QN('x'): {str}}426 def g(foo):427 # The resolver will convey that this function has the following body:428 #429 # nonlocal x430 # x = 'a'431 # foo()432 del foo433 pass434 def test_fn(x: int): # pylint:disable=unused-argument435 def foo():436 return x437 x = 1.0438 g(foo)439 node, _ = TestTranspiler(Resolver).transform(test_fn, None)440 fn_body = node.body441 self.assertTypes(fn_body[0].body[0].value, str)442 def test_subscript(self):443 test_self = self444 class Resolver(type_inference.Resolver):445 def res_arg(self, ns, types_ns, f_name, name, type_anno, f_is_local):446 return {list}447 def res_value(self, ns, value):448 return {int}449 def res_slice(self, ns, types_ns, node, value, slice_):450 test_self.assertSetEqual(value, {list})451 test_self.assertSetEqual(slice_, {int})452 return {str}453 def test_fn(a):454 return a[1]455 node, _ = TestTranspiler(Resolver).transform(test_fn, None)456 fn_body = node.body457 self.assertTypes(fn_body[0].value, str)458 self.assertTypes(fn_body[0].value.value, list)459 self.assertTypes(fn_body[0].value.slice, int)460 def test_tuple_unpacking(self):461 test_self = self462 class Resolver(type_inference.Resolver):463 def res_arg(self, ns, types_ns, f_name, name, type_anno, f_is_local):464 return {list}465 def res_value(self, ns, value):466 return {int}467 def res_slice(self, ns, types_ns, node_or_slice, value, slice_):468 test_self.assertIn(node_or_slice, (0, 1))469 test_self.assertSetEqual(value, {list})470 test_self.assertSetEqual(slice_, {int})471 if node_or_slice == 0:472 return {float}473 else:474 return {str}475 def test_fn(t):476 a, b = t477 return a, b478 node, _ = TestTranspiler(Resolver).transform(test_fn, None)479 fn_body = node.body480 self.assertTypes(fn_body[1].value, ((float, str),))481 self.assertTypes(fn_body[1].value.elts[0], float)482 self.assertTypes(fn_body[1].value.elts[1], str)483 def test_compare(self):484 test_self = self485 class Resolver(type_inference.Resolver):486 def res_arg(self, ns, types_ns, f_name, name, type_anno, f_is_local):487 return {int}488 def res_compare(self, ns, types_ns, node, left, right):489 test_self.assertSetEqual(left, {int})490 test_self.assertListEqual(right, [{int}])491 return {bool}492 def test_fn(a, b):493 return a < b494 node, _ = TestTranspiler(Resolver).transform(test_fn, None)495 fn_body = node.body496 self.assertTypes(fn_body[0].value, bool)497 self.assertTypes(fn_body[0].value.left, int)498 self.assertTypes(fn_body[0].value.comparators[0], int)499 def test_binop(self):500 test_self = self501 class Resolver(type_inference.Resolver):502 def res_arg(self, ns, types_ns, f_name, name, type_anno, f_is_local):503 return {list}504 def res_binop(self, ns, types_ns, node, left, right):505 test_self.assertSetEqual(left, {list})506 test_self.assertSetEqual(right, {list})507 return {float}508 def test_fn(a, b):509 return a @ b510 node, _ = TestTranspiler(Resolver).transform(test_fn, None)511 fn_body = node.body512 self.assertTypes(fn_body[0].value, float)513 self.assertTypes(fn_body[0].value.left, list)514 self.assertTypes(fn_body[0].value.right, list)515 def test_unop(self):516 class Resolver(type_inference.Resolver):517 def res_arg(self, ns, types_ns, f_name, name, type_anno, f_is_local):518 return {list}519 def res_unop(self, ns, types_ns, node, opnd):520 return {float}521 def test_fn(a):522 return -a523 node, _ = TestTranspiler(Resolver).transform(test_fn, None)524 fn_body = node.body525 self.assertTypes(fn_body[0].value, float)526 self.assertTypes(fn_body[0].value.operand, list)527 def test_tuple_literal(self):528 class Resolver(type_inference.Resolver):529 def res_arg(self, ns, types_ns, f_name, name, type_anno, f_is_local):530 return {int}531 def test_fn(a, b):532 return a, b533 node, _ = TestTranspiler(Resolver).transform(test_fn, None)534 fn_body = node.body535 self.assertTypes(fn_body[0].value, ((int, int),))536 self.assertTypes(fn_body[0].value.elts[0], int)537 self.assertTypes(fn_body[0].value.elts[1], int)538 def test_list_literal(self):539 class Resolver(type_inference.Resolver):540 def res_arg(self, ns, types_ns, f_name, name, type_anno, f_is_local):541 return {int}542 def res_list_literal(self, ns, elt_types):543 all_types = set()544 for s in elt_types:545 all_types |= s546 return {List[t] for t in all_types}547 def test_fn(a, b):548 return [a, b]549 node, _ = TestTranspiler(Resolver).transform(test_fn, None)550 fn_body = node.body551 self.assertTypes(fn_body[0].value, List[int])552 self.assertTypes(fn_body[0].value.elts[0], int)553 self.assertTypes(fn_body[0].value.elts[1], int)554 def test_tuple_unpacking_syntactic(self):555 test_self = self556 class Resolver(type_inference.Resolver):557 def res_arg(self, ns, types_ns, f_name, name, type_anno, f_is_local):558 if name == qual_names.QN('a'):559 return {int}560 else:561 return {float}562 def res_value(self, ns, value):563 test_self.assertIn(value, (0, 1))564 return int565 def res_slice(self, ns, types_ns, node_or_slice, value, slice_):566 test_self.assertIn(node_or_slice, (0, 1))567 test_self.assertSetEqual(value, {(int, float)})568 test_self.assertEqual(slice_, int)569 return {t[node_or_slice] for t in value}570 def test_fn(a, b):571 c, d = a, b572 return c, d573 node, _ = TestTranspiler(Resolver).transform(test_fn, None)574 fn_body = node.body575 self.assertTypes(fn_body[1].value, ((int, float),))576 self.assertTypes(fn_body[1].value.elts[0], int)577 self.assertTypes(fn_body[1].value.elts[1], float)578 def test_tuple_unpacking_operational(self):579 test_self = self580 class Resolver(type_inference.Resolver):581 def res_arg(self, ns, types_ns, f_name, name, type_anno, f_is_local):582 return {(int, float)}583 def res_value(self, ns, value):584 test_self.assertIn(value, (0, 1))585 return int586 def res_slice(self, ns, types_ns, node_or_slice, value, slice_):587 test_self.assertIn(node_or_slice, (0, 1))588 test_self.assertSetEqual(value, {(int, float)})589 test_self.assertEqual(slice_, int)590 return {t[node_or_slice] for t in value}591 def test_fn(a):592 c, d = a593 return c, d594 node, _ = TestTranspiler(Resolver).transform(test_fn, None)595 fn_body = node.body596 self.assertTypes(fn_body[1].value, ((int, float),))597 self.assertTypes(fn_body[1].value.elts[0], int)598 self.assertTypes(fn_body[1].value.elts[1], float)599 def test_list_expansion_syntactic(self):600 test_self = self601 class Resolver(type_inference.Resolver):602 def res_arg(self, ns, types_ns, f_name, name, type_anno, f_is_local):603 if name == qual_names.QN('a'):604 return {int}605 else:606 return {float}607 def res_value(self, ns, value):608 test_self.assertIn(value, (0, 1))609 return int610 def res_slice(self, ns, types_ns, node_or_slice, value, slice_):611 test_self.assertIn(node_or_slice, (0, 1))612 test_self.assertSetEqual(value, {(int, float)})613 test_self.assertEqual(slice_, int)614 return {t[node_or_slice] for t in value}615 def test_fn(a, b):616 [c, d] = a, b617 return c, d618 node, _ = TestTranspiler(Resolver).transform(test_fn, None)619 fn_body = node.body620 # TODO(mdan): Whether it's List or Tuple might be open for interpretation.621 self.assertTypes(fn_body[1].value, ((int, float),))622 self.assertTypes(fn_body[1].value.elts[0], int)623 self.assertTypes(fn_body[1].value.elts[1], float)624 def test_list_expansion_operational(self):625 test_self = self626 class Resolver(type_inference.Resolver):627 def res_arg(self, ns, types_ns, f_name, name, type_anno, f_is_local):628 if name == qual_names.QN('a'):629 return {int}630 else:631 return {float}632 def res_value(self, ns, value):633 test_self.assertIn(value, (0, 1))634 return int635 def res_slice(self, ns, types_ns, node_or_slice, value, slice_):636 test_self.assertIn(node_or_slice, (0, 1))637 test_self.assertSetEqual(value, {(int, float)})638 test_self.assertEqual(slice_, int)639 return {t[node_or_slice] for t in value}640 def test_fn(a, b):641 [c, d] = a, b642 return c, d643 node, _ = TestTranspiler(Resolver).transform(test_fn, None)644 fn_body = node.body645 # TODO(mdan): Whether it's List or Tuple might be open for interpretation.646 self.assertTypes(fn_body[1].value, ((int, float),))647 self.assertTypes(fn_body[1].value.elts[0], int)648 self.assertTypes(fn_body[1].value.elts[1], float)649if __name__ == '__main__':...
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!!