def test_extract_constraints(self): parsed_two_arg_call = self._get_call_node("Get(A, B(x))") self.assertEqual(("A", "B"), Get.extract_constraints(parsed_two_arg_call)) with self.assertRaises(ValueError) as cm: Get.extract_constraints(self._get_call_node("Get(1, 2)")) # The name of the type of a number literal AST node changed from "Num" to "Constant" between Python 3.6 and Python 3.8. # This will compute the correct name for either version of Python. n = ast.parse("1").body[0].value num_ty = getattr(n, "id", type(n).__name__) msg = f"Two arg form of Get expected (product_type, subject_type(subject)), but got: ({num_ty}, {num_ty})" assert str(cm.exception) == msg parsed_three_arg_call = self._get_call_node("Get(A, B, C(x))") self.assertEqual(("A", "B"), Get.extract_constraints(parsed_three_arg_call)) with self.assertRaises(ValueError) as cm: Get.extract_constraints( self._get_call_node("Get(A, 'asdf', C(x))")) # The name of the type of a string literal AST node also changed between Python 3.6 and Python 3.8. n = ast.parse("'test'").body[0].value str_ty = getattr(n, "id", type(n).__name__) msg = f"Three arg form of Get expected (product_type, subject_declared_type, subject), but got: (A, {str_ty}, Call)" assert str(cm.exception) == msg
def test_extract_constraints(self): parsed_two_arg_call = self._get_call_node("Get(A, B(x))") self.assertEqual(("A", "B"), Get.extract_constraints(parsed_two_arg_call)) with self.assertRaises(ValueError) as cm: Get.extract_constraints(self._get_call_node("Get(1, 2)")) self.assertEqual( str(cm.exception), """\ Two arg form of Get expected (product_type, subject_type(subject)), but got: (Num, Num)""", ) parsed_three_arg_call = self._get_call_node("Get(A, B, C(x))") self.assertEqual(("A", "B"), Get.extract_constraints(parsed_three_arg_call)) with self.assertRaises(ValueError) as cm: Get.extract_constraints( self._get_call_node("Get(A, 'asdf', C(x))")) self.assertEqual( str(cm.exception), """\ Three arg form of Get expected (product_type, subject_declared_type, subject), but got: (A, Str, Call)""", )
def test_extract_constraints(self): parsed_two_arg_call = self._get_call_node("Get(A, B(x))") self.assertEqual(('A', 'B'), Get.extract_constraints(parsed_two_arg_call)) with self.assertRaises(ValueError) as cm: Get.extract_constraints(self._get_call_node("Get(1, 2)")) self.assertEqual(str(cm.exception), """\ Two arg form of Get expected (product_type, subject_type(subject)), but got: (Num, Num)""") parsed_three_arg_call = self._get_call_node("Get(A, B, C(x))") self.assertEqual(('A', 'B'), Get.extract_constraints(parsed_three_arg_call)) with self.assertRaises(ValueError) as cm: Get.extract_constraints(self._get_call_node("Get(A, 'asdf', C(x))")) self.assertEqual(str(cm.exception), """\ Three arg form of Get expected (product_type, subject_declared_type, subject), but got: (A, Str, Call)""")
def visit_Call(self, node: ast.Call) -> None: if self._is_get(node): self._gets.append(Get.extract_constraints(node)) # Ensure we descend into e.g. MultiGet(Get(...)...) calls. self.generic_visit(node)
def visit_Call(self, node): if self._is_get(node): self._gets.append(Get.extract_constraints(node))
def visit_Call(self, node): if isinstance(node.func, ast.Name) and node.func.id == Get.__name__: self._gets.append(Get.extract_constraints(node))
def visit_Call(self, node): if not isinstance(node.func, ast.Name) or node.func.id != Get.__name__: return self.gets.append(Get.extract_constraints(node))
def visit_Call(self, node) -> None: self.generic_visit(node) if self._is_get(node): self._gets.append(Get.extract_constraints(node))