Beispiel #1
0
def shrink_traced_types(
    traces: Iterable[CallTrace],
    max_typed_dict_size: int,
) -> Tuple[Dict[str, type], Optional[type], Optional[type]]:
    """Merges the traced types and returns the minimally equivalent types"""
    arg_types: DefaultDict[str, Set[type]] = collections.defaultdict(set)
    return_types: Set[type] = set()
    yield_types: Set[type] = set()
    for t in traces:
        for arg, typ in t.arg_types.items():
            arg_types[arg].add(typ)
        if t.return_type is not None:
            # pyre-fixme[6]: Expected `Type[typing.Any]` for 1st param but got
            #  `Optional[typing.Type[typing.Any]]`.
            return_types.add(t.return_type)
        if t.yield_type is not None:
            # pyre-fixme[6]: Expected `Type[typing.Any]` for 1st param but got
            #  `Optional[typing.Type[typing.Any]]`.
            yield_types.add(t.yield_type)
    shrunken_arg_types = {
        name: shrink_types(ts, max_typed_dict_size)
        for name, ts in arg_types.items()
    }
    return_type = shrink_types(return_types,
                               max_typed_dict_size) if return_types else None
    yield_type = shrink_types(yield_types,
                              max_typed_dict_size) if yield_types else None
    return (shrunken_arg_types, return_type, yield_type)
Beispiel #2
0
def shrink_traced_types(traces: Iterable[CallTrace]) -> Tuple[Dict[str, type], Optional[type], Optional[type]]:
    """Merges the traced types and returns the minimally equivalent types"""
    arg_types: DefaultDict[str, Set[type]] = collections.defaultdict(set)
    return_types: Set[type] = set()
    yield_types: Set[type] = set()
    for t in traces:
        for arg, typ in t.arg_types.items():
            arg_types[arg].add(typ)
        if t.return_type is not None:
            return_types.add(t.return_type)
        if t.yield_type is not None:
            yield_types.add(t.yield_type)
    shrunken_arg_types = {name: shrink_types(ts) for name, ts in arg_types.items()}
    return_type = shrink_types(return_types) if return_types else None
    yield_type = shrink_types(yield_types) if yield_types else None
    return (shrunken_arg_types, return_type, yield_type)
Beispiel #3
0
 def test_shrink_types(self, types, expected_type):
     assert shrink_types(types) == expected_type
Beispiel #4
0
 def test_shrink_types_mixed_dicts(self, types, expected_type):
     assert shrink_types(types,
                         max_typed_dict_size=VERY_LARGE_MAX_TYPED_DICT_SIZE
                         ) == expected_type
Beispiel #5
0
 def test_shrink_types(self, types, expected_type):
     assert shrink_types(types, max_typed_dict_size=0) == expected_type
Beispiel #6
0
 def test_shrink_types_non_typed_dict(self, types, expected_type):
     actual = shrink_types(types, max_typed_dict_size=10)
     assert types_equal(actual, expected_type)
Beispiel #7
0
 def test_shrink_non_uniform_typed_dict_types(self, types, expected_type):
     actual = shrink_types(types, max_typed_dict_size=10)
     assert actual == expected_type