예제 #1
0
    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))
예제 #2
0
 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
예제 #3
0
    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))
예제 #4
0
 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)
예제 #5
0
 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)
예제 #6
0
 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)
예제 #7
0
 def test_dict_type_with_other_max_sizes(self, value, max_typed_dict_size, expected):
     assert get_type(value, max_typed_dict_size) == expected
예제 #8
0
 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
예제 #9
0
 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
예제 #10
0
 def test_class_type(self):
     """Return the correct type for classes"""
     assert get_type(Dummy) == Type[Dummy]
예제 #11
0
 def test_instance_type(self):
     """Return appropriate type for an instance of a user defined class"""
     assert get_type(Dummy()) == Dummy
예제 #12
0
 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
예제 #13
0
 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]
예제 #14
0
 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
예제 #15
0
 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
예제 #16
0
 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
예제 #17
0
 def test_class_type(self):
     """Return the correct type for classes"""
     assert get_type(Dummy, max_typed_dict_size=None) == Type[Dummy]
예제 #18
0
 def test_builtin_types(self, value, expected_type):
     """Return the appropriate type for builtins"""
     assert get_type(value) == expected_type