def test_wrap_type_decl_unit(self): """Test WrapTypeDeclUnit.""" ast1 = self.Parse(""" c = ... # type: int def f(x: int) -> int: ... def f(x: float) -> float: ... class A(object): pass """) ast2 = self.Parse(""" c = ... # type: float d = ... # type: int def f(x: complex) -> complex: ... class B(object): pass """) w = pytd_utils.WrapTypeDeclUnit( "combined", ast1.classes + ast1.functions + ast1.constants + ast2.classes + ast2.functions + ast2.constants) expected = textwrap.dedent(""" from typing import Union c = ... # type: Union[int, float] d = ... # type: int def f(x: int) -> int: ... def f(x: float) -> float: ... def f(x: complex) -> complex: ... class A(object): pass class B(object): pass """) self.AssertSourceEquals(w, expected)
def testWrapTypeDeclUnit(self): """Test WrapTypeDeclUnit.""" ast1 = self.Parse(""" c = ... # type: int def f(x: int) -> int def f(x: float) -> float class A(object): pass """) ast2 = self.Parse(""" c = ... # type: float d = ... # type: int def f(x: complex) -> complex class B(object): pass """) w = pytd_utils.WrapTypeDeclUnit( "combined", ast1.classes + ast1.functions + ast1.constants + ast2.classes + ast2.functions + ast2.constants) expected = textwrap.dedent(""" c = ... # type: int or float d = ... # type: int def f(x: int) -> int def f(x: float) -> float def f(x: complex) -> complex class A(object): pass class B(object): pass """) self.AssertSourceEquals(w, expected)
def pytd_for_types(self, defs): data = [] for name, var in defs.items(): if name in output.TOP_LEVEL_IGNORE or self._is_builtin( name, var.data): continue options = var.FilteredData(self.exitpoint) if (len(options) > 1 and not all( isinstance(o, (abstract.Function, abstract.BoundFunction)) for o in options)): # It's ambiguous whether this is a type, a function or something # else, so encode it as a constant. combined_types = pytd_utils.JoinTypes( t.to_type(self.exitpoint) for t in options) data.append(pytd.Constant(name, combined_types)) elif options: for option in options: try: d = option.to_pytd_def(self.exitpoint, name) # Deep definition except NotImplementedError: d = option.to_type(self.exitpoint) # Type only if isinstance(d, pytd.NothingType): assert isinstance(option, abstract.Empty) d = pytd.AnythingType() if isinstance(d, pytd.TYPE) and not isinstance( d, pytd.TypeParameter): data.append(pytd.Constant(name, d)) else: data.append(d) else: log.error("No visible options for " + name) data.append(pytd.Constant(name, pytd.AnythingType())) return pytd_utils.WrapTypeDeclUnit("inferred", data)
def pytd_for_types(self, defs): data = [] pytd_convert = self.convert.pytd_convert annots = pytd_convert.get_annotations_dict(defs) for name, t in pytd_convert.uninitialized_annotations_to_instance_types( self.exitpoint, annots, defs): data.append(pytd.Constant(name, t)) for name, var in defs.items(): if name in output.TOP_LEVEL_IGNORE or self._is_builtin( name, var.data): continue options = [] for value, is_annotation in pytd_convert.get_annotated_values( self.exitpoint, name, var, annots): if is_annotation: data.append(pytd.Constant(name, value)) else: options.append(value) if (len(options) > 1 and not all( isinstance(o, (abstract.Function, abstract.BoundFunction)) for o in options)): # It's ambiguous whether this is a type, a function or something # else, so encode it as a constant. combined_types = pytd_utils.JoinTypes( t.to_type(self.exitpoint) for t in options) data.append(pytd.Constant(name, combined_types)) elif options: for option in options: try: d = option.to_pytd_def(self.exitpoint, name) # Deep definition except NotImplementedError: d = option.to_type(self.exitpoint) # Type only if isinstance(d, pytd.NothingType): if isinstance(option, abstract.Empty): d = pytd.AnythingType() else: assert isinstance(option, typing_overlay.NoReturn) if isinstance(d, pytd.TYPE) and not isinstance( d, pytd.TypeParameter): data.append(pytd.Constant(name, d)) else: data.append(d) else: log.error("No visible options for %s", name) data.append(pytd.Constant(name, pytd.AnythingType())) return pytd_utils.WrapTypeDeclUnit("inferred", data)
def pytd_for_types(self, defs): # If a variable is annotated, we'll always output that type. annotated_names = set() data = [] pytd_convert = self.convert.pytd_convert annots = abstract_utils.get_annotations_dict(defs) for name, t in pytd_convert.annotations_to_instance_types( self.exitpoint, annots): annotated_names.add(name) data.append(pytd.Constant(name, t)) for name, var in defs.items(): if (name in output.TOP_LEVEL_IGNORE or name in annotated_names or self._is_typing_member(name, var)): continue options = var.FilteredData(self.exitpoint, strict=False) if (len(options) > 1 and not all(isinstance(o, abstract.FUNCTION_TYPES) for o in options)): if all(isinstance(o, (abstract.ParameterizedClass, abstract.TypeParameter, abstract.Union)) for o in options ) and self.options.preserve_union_macros: # type alias data.append(pytd_utils.JoinTypes(t.to_pytd_def(self.exitpoint, name) for t in options)) else: # It's ambiguous whether this is a type, a function or something # else, so encode it as a constant. combined_types = pytd_utils.JoinTypes(t.to_type(self.exitpoint) for t in options) data.append(pytd.Constant(name, combined_types)) elif options: for option in options: try: d = option.to_pytd_def(self.exitpoint, name) # Deep definition except NotImplementedError: d = option.to_type(self.exitpoint) # Type only if isinstance(d, pytd.NothingType): if isinstance(option, abstract.Empty): d = pytd.AnythingType() else: assert isinstance(option, typing_overlay.NoReturn) if isinstance(d, pytd.Type) and not isinstance(d, pytd.TypeParameter): data.append(pytd.Constant(name, d)) else: data.append(d) else: log.error("No visible options for %s", name) data.append(pytd.Constant(name, pytd.AnythingType())) return pytd_utils.WrapTypeDeclUnit("inferred", data)