def test_dict_nested_within_generic(self, value, expected_when_max_size_is_zero, expected_when_max_size_is_none): """Return the appropriate type for dictionaries.""" actual_when_zero = get_type(value, max_typed_dict_size=0) actual_when_none = get_type(value, max_typed_dict_size=VERY_LARGE_MAX_TYPED_DICT_SIZE) assert (types_equal(actual_when_zero, expected_when_max_size_is_zero)) assert (types_equal(actual_when_none, expected_when_max_size_is_none))
def test_dict_type(self, value, expected_when_max_size_is_zero, expected_when_max_size_is_none): """Return the appropriate type for dictionaries.""" assert get_type( value, max_typed_dict_size=0) == expected_when_max_size_is_zero assert get_type(value, max_typed_dict_size=VERY_LARGE_MAX_TYPED_DICT_SIZE ) == expected_when_max_size_is_none
def test_dict_nested_within_generic(self, value, expected_when_max_size_is_zero, expected_when_max_size_is_none): """Return the appropriate type for dictionaries.""" actual_when_zero = get_type(value, max_typed_dict_size=0) actual_when_none = get_type(value, max_typed_dict_size=None) def are_generic_types_equal(actual, expected) -> bool: """Compare manually because the module name causes errors in 3.6. List[tests.test_typing.FooTypedDict] != List[monkeytype.typing.FooTypedDict].""" return name_of_generic(actual) == name_of_generic(expected) and actual.__args__ == expected.__args__ assert (are_generic_types_equal(actual_when_zero, expected_when_max_size_is_zero)) assert (are_generic_types_equal(actual_when_none, expected_when_max_size_is_none))
def handle_call(self, frame: FrameType) -> None: if self.sample_rate and random.randrange(self.sample_rate) != 0: return func = self._get_func(frame) if func is None: return code = frame.f_code # I can't figure out a way to access the value sent to a generator via # send() from a stack frame. if code.co_code[frame.f_lasti] == YIELD_VALUE_OPCODE: return arg_names = code.co_varnames[0:code.co_argcount] arg_types = {} for name in arg_names: if name in frame.f_locals: arg_types[name] = get_type(frame.f_locals[name]) self.traces[frame] = CallTrace(func, arg_types)
def handle_return(self, frame: FrameType, arg: Any) -> None: # In the case of a 'return' event, arg contains the return value, or # None, if the block returned because of an unhandled exception. We # need to distinguish the exceptional case (not a valid return type) # from a function returning (or yielding) None. In the latter case, the # the last instruction that was executed should always be a return or a # yield. typ = get_type(arg, max_typed_dict_size=self.max_typed_dict_size) last_opcode = frame.f_code.co_code[frame.f_lasti] trace = self.traces.get(frame) if trace is None: return elif last_opcode == YIELD_VALUE_OPCODE: trace.add_yield_type(typ) else: if last_opcode == RETURN_VALUE_OPCODE: trace.return_type = typ del self.traces[frame] self.logger.log(trace)
def handle_call(self, frame: FrameType) -> None: # pyre-fixme[6]: Expected `int` for 1st param but got `Optional[int]`. if self.sample_rate and random.randrange(self.sample_rate) != 0: return func = self._get_func(frame) if func is None: return code = frame.f_code # I can't figure out a way to access the value sent to a generator via # send() from a stack frame. if code.co_code[frame.f_lasti] == YIELD_VALUE_OPCODE: return arg_names = code.co_varnames[0:code.co_argcount] arg_types = {} for name in arg_names: if name in frame.f_locals: arg_types[name] = get_type(frame.f_locals[name], max_typed_dict_size=self.max_typed_dict_size) self.traces[frame] = CallTrace(func, arg_types)
def test_dict_type_with_other_max_sizes(self, value, max_typed_dict_size, expected): assert get_type(value, max_typed_dict_size) == expected
def test_dict_type(self, value, expected_when_max_size_is_zero, expected_when_max_size_is_none): """Return the appropriate type for dictionaries.""" assert get_type(value, max_typed_dict_size=0) == expected_when_max_size_is_zero assert get_type(value, max_typed_dict_size=None) == expected_when_max_size_is_none
def test_builtin_types(self, value, expected_type): """Return the appropriate type for builtins""" assert get_type(value, max_typed_dict_size=None) == expected_type assert get_type(value, max_typed_dict_size=0) == expected_type
def test_class_type(self): """Return the correct type for classes""" assert get_type(Dummy) == Type[Dummy]
def test_instance_type(self): """Return appropriate type for an instance of a user defined class""" assert get_type(Dummy()) == Dummy
def test_dict_type(self, value, max_typed_dict_size, expected_dict_type): """Return the appropriate type for dictionaries.""" assert get_type(value, max_typed_dict_size) == expected_dict_type
def test_class_type(self): """Return the correct type for classes""" assert get_type( Dummy, max_typed_dict_size=VERY_LARGE_MAX_TYPED_DICT_SIZE) == Type[Dummy]
def test_instance_type(self): """Return appropriate type for an instance of a user defined class""" assert get_type( Dummy(), max_typed_dict_size=VERY_LARGE_MAX_TYPED_DICT_SIZE) == Dummy
def test_builtin_types(self, value, expected_type): """Return the appropriate type for builtins""" assert get_type(value, max_typed_dict_size=VERY_LARGE_MAX_TYPED_DICT_SIZE ) == expected_type assert get_type(value, max_typed_dict_size=0) == expected_type
def test_instance_type(self): """Return appropriate type for an instance of a user defined class""" assert get_type(Dummy(), max_typed_dict_size=None) == Dummy
def test_class_type(self): """Return the correct type for classes""" assert get_type(Dummy, max_typed_dict_size=None) == Type[Dummy]
def test_builtin_types(self, value, expected_type): """Return the appropriate type for builtins""" assert get_type(value) == expected_type