Skip to content

Commit

Permalink
Merge pull request #1610 from google/google_sync
Browse files Browse the repository at this point in the history
Google sync
  • Loading branch information
rchen152 committed Apr 4, 2024
2 parents 468f687 + 7e01d87 commit dac9453
Show file tree
Hide file tree
Showing 83 changed files with 2,623 additions and 1,098 deletions.
4 changes: 2 additions & 2 deletions docs/support.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ of pytype.
* [Third-Party Libraries](#third-party-libraries)

<!-- Created by https://github.com/ekalinin/github-markdown-toc -->
<!-- Added by: rechen, at: Tue Mar 19 02:50:25 PM PDT 2024 -->
<!-- Added by: rechen, at: Wed Apr 3 02:22:17 PM PDT 2024 -->

<!--te-->

Expand Down Expand Up @@ -89,7 +89,7 @@ Feature
[PEP 696 -- Type Defaults for Type Parameters][696] | 3.13 | ❌ | [#1597][type-parameter-defaults]
[PEP 702 -- Marking deprecations using the type system][702] | 3.13 | ❌ |
[PEP 705 -- TypedDict: Read-only items][705] | 3.13 | ❌ |
[PEP 742 -- Narrowing types with TypeIs][742] | TBD | ✅ | open PEP, under consideration
[PEP 742 -- Narrowing types with TypeIs][742] | 3.13 | ✅ |
Custom Recursive Types | 3.6 | ✅ |
Generic Type Aliases | 3.6 | ✅ |
Type Annotation Inheritance | 3.6 | ❌ | [#81][annotation-inheritance]
Expand Down
2 changes: 2 additions & 0 deletions pytype/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -531,9 +531,11 @@ py_test(
io_test.py
DEPS
.config
.file_utils
.io
pytype.platform_utils.platform_utils
pytype.pytd.pytd
pytype.tests.test_utils
)

py_test(
Expand Down
3 changes: 2 additions & 1 deletion pytype/abstract/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ def register_instance(self, instance): # pylint: disable=unused-arg
instance: An instance of this class (as a BaseValue)
"""

def to_pytd_instance(self, node=None, instance=None, seen=None, view=None):
def to_pytd_type_of_instance(
self, node=None, instance=None, seen=None, view=None):
"""Get the type an instance of us would have."""
return self.ctx.pytd_convert.value_instance_to_pytd_type(
node, self, instance, seen, view)
Expand Down
6 changes: 2 additions & 4 deletions pytype/abstract/_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,7 @@ def _convert_member(self, name, member, subst=None):
subst = subst or datatypes.AliasingDict()
node = self.ctx.root_node
if isinstance(member, pytd.Constant):
return self.ctx.convert.constant_to_var(
abstract_utils.AsInstance(member.type), subst, node)
return self.ctx.convert.pytd_cls_to_instance_var(member.type, subst, node)
elif isinstance(member, pytd.Function):
c = self.ctx.convert.constant_to_value(member, subst=subst, node=node)
c.parent = self
Expand All @@ -529,8 +528,7 @@ def _new_instance(self, container, node, args):
return value

def instantiate(self, node, container=None):
return self.ctx.convert.constant_to_var(
abstract_utils.AsInstance(self.pytd_cls), {}, node)
return self.ctx.convert.pytd_cls_to_instance_var(self.pytd_cls, {}, node)

def __repr__(self):
return f"PyTDClass({self.name})"
Expand Down
4 changes: 2 additions & 2 deletions pytype/abstract/_interpreter_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ def _check_signature(self):
m = self.ctx.matcher(self.ctx.root_node).compute_one_match(
guard_var, input_type)
if not m.success:
guard_pytd = pytd_utils.Print(guard_type.to_pytd_instance())
input_pytd = pytd_utils.Print(input_type.to_pytd_instance())
guard_pytd = pytd_utils.Print(guard_type.to_pytd_type_of_instance())
input_pytd = pytd_utils.Print(input_type.to_pytd_type_of_instance())
self.ctx.errorlog.invalid_function_definition(
self.ctx.vm.frames,
f"TypeIs[{guard_pytd}] is not consistent with input type "
Expand Down
7 changes: 2 additions & 5 deletions pytype/abstract/_pytd_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,11 +788,8 @@ def append_float(x: list[int]):
log.info("Mutating %s to %s",
tparam.name,
pytd_utils.Print(type_actual))
type_actual_val = self.ctx.convert.constant_to_var(
abstract_utils.AsInstance(type_actual),
subst,
node,
discard_concrete_values=True)
type_actual_val = self.ctx.convert.pytd_cls_to_instance_var(
type_actual, subst, node, discard_concrete_values=True)
mutations.append(
function.Mutation(arg, tparam.full_name, type_actual_val))
if self.name == "__new__":
Expand Down
10 changes: 5 additions & 5 deletions pytype/abstract/_special_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def build_class(node, props, kwargs, ctx):
node, props.bases, props.class_dict_var, total=kwargs.get("total"))
elif "__dataclass_transform__" in base.metadata:
node, cls_var = ctx.make_class(node, props)
return ctx.convert.apply_dataclass_transform(cls_var, node, ctx)
return ctx.convert.apply_dataclass_transform(cls_var, node)
return node, None


Expand Down Expand Up @@ -82,10 +82,10 @@ def matches_mro(self, c):
for b in c.mro)

def make_base_class(self):
return self.convert.make_typed_dict_builder(self.ctx)
return self.convert.make_typed_dict_builder()

def make_derived_class(self, name, pytd_cls):
return self.convert.make_typed_dict(name, pytd_cls, self.ctx)
return self.convert.make_typed_dict(name, pytd_cls)


class _NamedTupleBuilder(_Builder):
Expand All @@ -105,10 +105,10 @@ def matches_mro(self, c):
return False

def make_base_class(self):
return self.convert.make_namedtuple_builder(self.ctx)
return self.convert.make_namedtuple_builder()

def make_derived_class(self, name, pytd_cls):
return self.convert.make_namedtuple(name, pytd_cls, self.ctx)
return self.convert.make_namedtuple(name, pytd_cls)


_BUILDERS = (_TypedDictBuilder, _NamedTupleBuilder)
Expand Down
2 changes: 1 addition & 1 deletion pytype/abstract/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def _build_value(self, node, inner, ellipses):
if i in ellipses:
printed_params.append("...")
else:
typ = param.to_pytd_instance(node)
typ = param.to_pytd_type_of_instance(node)
annot, typing_imports = pytd_utils.MakeTypeAnnotation(typ)
printed_params.append(annot)
added_typing_imports.update(typing_imports)
Expand Down
36 changes: 18 additions & 18 deletions pytype/abstract/abstract_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ def setUp(self):
super().setUp()
self._is_instance = special_builtins.IsInstance.make(self._ctx)
# Easier access to some primitive instances.
self._bool = self._ctx.convert.primitive_class_instances[bool]
self._int = self._ctx.convert.primitive_class_instances[int]
self._str = self._ctx.convert.primitive_class_instances[str]
self._bool = self._ctx.convert.primitive_instances[bool]
self._int = self._ctx.convert.primitive_instances[int]
self._str = self._ctx.convert.primitive_instances[str]
# Values that represent primitive classes.
self._obj_class = self._ctx.convert.primitive_classes[object]
self._int_class = self._ctx.convert.primitive_classes[int]
Expand Down Expand Up @@ -234,7 +234,7 @@ def test_to_pytd_type_with_view1(self):
self._ctx.root_node))
param_binding = instance.get_instance_type_parameter(
abstract_utils.T).AddBinding(
self._ctx.convert.primitive_class_instances[int], [],
self._ctx.convert.primitive_instances[int], [],
self._ctx.root_node)
view = {
instance.get_instance_type_parameter(abstract_utils.T): param_binding}
Expand All @@ -245,8 +245,8 @@ def test_to_pytd_type_with_view1(self):

def test_to_pytd_type_with_view2(self):
# to_pytd_type(<tuple (int or str,)>, view={0: str})
param1 = self._ctx.convert.primitive_class_instances[int]
param2 = self._ctx.convert.primitive_class_instances[str]
param1 = self._ctx.convert.primitive_instances[int]
param2 = self._ctx.convert.primitive_instances[str]
param_var = param1.to_variable(self._ctx.root_node)
str_binding = param_var.AddBinding(param2, [], self._ctx.root_node)
instance = self._ctx.convert.tuple_to_value((param_var,))
Expand All @@ -266,7 +266,7 @@ def test_typing_container(self):
container = abstract.AnnotationContainer("List", self._ctx, cls)
expected = pytd.GenericType(pytd.NamedType("builtins.list"),
(pytd.AnythingType(),))
actual = container.to_pytd_instance(self._ctx.root_node)
actual = container.to_pytd_type_of_instance(self._ctx.root_node)
self.assertEqual(expected, actual)


Expand Down Expand Up @@ -299,15 +299,15 @@ def test_call_with_empty_arg(self):
def test_call_with_bad_arg(self):
f = self._make_pytd_function(
(self._ctx.loader.lookup_pytd("builtins", "str"),))
arg = self._ctx.convert.primitive_class_instances[int].to_variable(
arg = self._ctx.convert.primitive_instances[int].to_variable(
self._ctx.root_node)
self.assertRaises(
error_types.WrongArgTypes, self._call_pytd_function, f, (arg,))

def test_simple_call(self):
f = self._make_pytd_function(
(self._ctx.loader.lookup_pytd("builtins", "str"),))
arg = self._ctx.convert.primitive_class_instances[str].to_variable(
arg = self._ctx.convert.primitive_instances[str].to_variable(
self._ctx.root_node)
node, ret = self._call_pytd_function(f, (arg,))
self.assertIs(node, self._ctx.root_node)
Expand All @@ -318,9 +318,9 @@ def test_call_with_multiple_arg_bindings(self):
f = self._make_pytd_function(
(self._ctx.loader.lookup_pytd("builtins", "str"),))
arg = self._ctx.program.NewVariable()
arg.AddBinding(self._ctx.convert.primitive_class_instances[str], [],
arg.AddBinding(self._ctx.convert.primitive_instances[str], [],
self._ctx.root_node)
arg.AddBinding(self._ctx.convert.primitive_class_instances[int], [],
arg.AddBinding(self._ctx.convert.primitive_instances[int], [],
self._ctx.root_node)
node, ret = self._call_pytd_function(f, (arg,))
self.assertIs(node, self._ctx.root_node)
Expand All @@ -331,7 +331,7 @@ def test_call_with_skipped_combination(self):
f = self._make_pytd_function(
(self._ctx.loader.lookup_pytd("builtins", "str"),))
node = self._ctx.root_node.ConnectNew()
arg = self._ctx.convert.primitive_class_instances[str].to_variable(node)
arg = self._ctx.convert.primitive_instances[str].to_variable(node)
node, ret = self._call_pytd_function(f, (arg,))
self.assertIs(node, self._ctx.root_node)
self.assertFalse(ret.bindings)
Expand Down Expand Up @@ -534,7 +534,7 @@ def test_signature_insert_varargs_and_kwargs(self):
annotations={},
)
# f(1, 2, y=3, z=4)
int_inst = self._ctx.convert.primitive_class_instances[int]
int_inst = self._ctx.convert.primitive_instances[int]
int_binding = int_inst.to_binding(self._node)
arg_dict = {
"x": int_binding, "_1": int_binding, "y": int_binding, "z": int_binding}
Expand Down Expand Up @@ -751,9 +751,9 @@ def test_call_with_no_args(self):
def test_call_with_multiple_arg_bindings(self):
f = self._simple_sig([self._ctx.convert.str_type])
arg = self._ctx.program.NewVariable()
arg.AddBinding(self._ctx.convert.primitive_class_instances[str], [],
arg.AddBinding(self._ctx.convert.primitive_instances[str], [],
self._ctx.root_node)
arg.AddBinding(self._ctx.convert.primitive_class_instances[int], [],
arg.AddBinding(self._ctx.convert.primitive_instances[int], [],
self._ctx.root_node)
args = function.Args((arg,))
node, ret = f.call(self._ctx.root_node, f, args)
Expand Down Expand Up @@ -790,9 +790,9 @@ def test_call_with_multiple_varargs_bindings(self):
f = self._make_func(
varargs_name="arg", annotations={"arg": self._ctx.convert.str_type})
arg = self._ctx.program.NewVariable()
arg.AddBinding(self._ctx.convert.primitive_class_instances[str], [],
arg.AddBinding(self._ctx.convert.primitive_instances[str], [],
self._ctx.root_node)
arg.AddBinding(self._ctx.convert.primitive_class_instances[int], [],
arg.AddBinding(self._ctx.convert.primitive_instances[int], [],
self._ctx.root_node)
starargs = self._ctx.convert.build_tuple(self._ctx.root_node, (arg,))
args = function.Args(posargs=(), starargs=starargs)
Expand Down Expand Up @@ -872,7 +872,7 @@ def test_call_with_all_args(self):
})
posargs = (self._ctx.convert.build_string(self._ctx.root_node, "1"),
self._ctx.convert.build_int(self._ctx.root_node))
float_inst = self._ctx.convert.primitive_class_instances[float]
float_inst = self._ctx.convert.primitive_instances[float]
stararg = self._ctx.convert.build_tuple(
self._ctx.root_node, (float_inst.to_variable(self._ctx.root_node),))
namedargs = {}
Expand Down
2 changes: 1 addition & 1 deletion pytype/abstract/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def get_signatures(func):


def _print(t):
return pytd_utils.Print(t.to_pytd_instance())
return pytd_utils.Print(t.to_pytd_type_of_instance())

_SigT = TypeVar("_SigT", bound="Signature")

Expand Down
2 changes: 1 addition & 1 deletion pytype/annotation_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def _do_sub_one_annotation(self, node, annot, get_type_parameter_subst_fn):
param_strings = []
for t in utils.unique_list(self.get_type_parameters(cur)):
s = pytd_utils.Print(
get_type_parameter_subst_fn(t).to_pytd_instance(node))
get_type_parameter_subst_fn(t).to_pytd_type_of_instance(node))
param_strings.append(s)
expr = f"{cur.expr}[{', '.join(param_strings)}]"
late_annot = abstract.LateAnnotation(expr, cur.stack, cur.ctx)
Expand Down
8 changes: 4 additions & 4 deletions pytype/attribute_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def setUp(self):
def test_type_parameter_instance(self):
t = abstract.TypeParameter(abstract_utils.T, self._ctx)
t_instance = abstract.TypeParameterInstance(
t, self._ctx.convert.primitive_class_instances[str], self._ctx)
t, self._ctx.convert.primitive_instances[str], self._ctx)
node, var = self._ctx.attribute_handler.get_attribute(
self._ctx.root_node, t_instance, "upper")
self.assertIs(node, self._ctx.root_node)
Expand All @@ -131,7 +131,7 @@ def test_type_parameter_instance(self):
def test_type_parameter_instance_bad_attribute(self):
t = abstract.TypeParameter(abstract_utils.T, self._ctx)
t_instance = abstract.TypeParameterInstance(
t, self._ctx.convert.primitive_class_instances[str], self._ctx)
t, self._ctx.convert.primitive_instances[str], self._ctx)
node, var = self._ctx.attribute_handler.get_attribute(
self._ctx.root_node, t_instance, "rumpelstiltskin")
self.assertIs(node, self._ctx.root_node)
Expand All @@ -146,12 +146,12 @@ def test_empty_type_parameter_instance(self):
self._ctx.root_node, t_instance, "real")
self.assertIs(node, self._ctx.root_node)
attr, = var.data
self.assertIs(attr, self._ctx.convert.primitive_class_instances[int])
self.assertIs(attr, self._ctx.convert.primitive_instances[int])

def test_type_parameter_instance_set_attribute(self):
t = abstract.TypeParameter(abstract_utils.T, self._ctx)
t_instance = abstract.TypeParameterInstance(
t, self._ctx.convert.primitive_class_instances[str], self._ctx)
t, self._ctx.convert.primitive_instances[str], self._ctx)
node = self._ctx.attribute_handler.set_attribute(
self._ctx.root_node, t_instance, "rumpelstiltskin",
self._ctx.new_unsolvable(self._ctx.root_node))
Expand Down
24 changes: 12 additions & 12 deletions pytype/compare_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def test_cmp_rel__prefix_equal(self):
tup1 = self._ctx.convert.tuple_to_value(
(self._convert.constant_to_value(3).to_variable(self._node),
self._convert.constant_to_value(1).to_variable(self._node),
self._convert.primitive_class_instances[int].to_variable(self._node)))
self._convert.primitive_instances[int].to_variable(self._node)))
tup2 = self._convert.constant_to_value((3, 1))
self.assertIs(False, compare.cmp_rel(self._ctx, slots.LT, tup1, tup2))
self.assertIs(True, compare.cmp_rel(self._ctx, slots.LT, tup2, tup1))
Expand All @@ -168,7 +168,7 @@ def test_cmp_rel__prefix_not_equal(self):
tup1 = self._ctx.convert.tuple_to_value(
(self._convert.constant_to_value(3).to_variable(self._node),
self._convert.constant_to_value(1).to_variable(self._node),
self._convert.primitive_class_instances[int].to_variable(self._node)))
self._convert.primitive_instances[int].to_variable(self._node)))
tup2 = self._convert.constant_to_value((4, 2))
self.assertIs(True, compare.cmp_rel(self._ctx, slots.LT, tup1, tup2))
self.assertIs(False, compare.cmp_rel(self._ctx, slots.LT, tup2, tup1))
Expand All @@ -186,7 +186,7 @@ def test_cmp_rel__prefix_not_equal(self):
def test_cmp_rel__prefix_unknown(self):
tup1 = self._ctx.convert.tuple_to_value(
(self._convert.constant_to_value(3).to_variable(self._node),
self._convert.primitive_class_instances[int].to_variable(self._node)))
self._convert.primitive_instances[int].to_variable(self._node)))
tup2 = self._convert.constant_to_value((3, 1))
for op in (slots.LT, slots.LE, slots.EQ, slots.NE, slots.GE, slots.GT):
self.assertIsNone(compare.cmp_rel(self._ctx, op, tup1, tup2))
Expand Down Expand Up @@ -269,7 +269,7 @@ def test_bad_pop(self):
self.assertTruthy(self._d)

def test_bad_pop_with_default(self):
val = self._convert.primitive_class_instances[int]
val = self._convert.primitive_instances[int]
self._d.set_str_item(self._node, "a", val.to_variable(self._node))
node, ret = self._d.pop_slot(self._node,
self._convert.build_string(self._node, "b"),
Expand All @@ -279,19 +279,19 @@ def test_bad_pop_with_default(self):
self.assertListEqual(ret.data, [self._convert.none])

def test_ambiguous_pop(self):
val = self._convert.primitive_class_instances[int]
val = self._convert.primitive_instances[int]
self._d.set_str_item(self._node, "a", val.to_variable(self._node))
ambiguous_key = self._convert.primitive_class_instances[str]
ambiguous_key = self._convert.primitive_instances[str]
node, ret = self._d.pop_slot(
self._node, ambiguous_key.to_variable(self._node))
self.assertAmbiguous(self._d)
self.assertIs(node, self._node)
self.assertListEqual(ret.data, [val])

def test_ambiguous_pop_with_default(self):
val = self._convert.primitive_class_instances[int]
val = self._convert.primitive_instances[int]
self._d.set_str_item(self._node, "a", val.to_variable(self._node))
ambiguous_key = self._convert.primitive_class_instances[str]
ambiguous_key = self._convert.primitive_instances[str]
default_var = self._convert.none.to_variable(self._node)
node, ret = self._d.pop_slot(
self._node, ambiguous_key.to_variable(self._node), default_var)
Expand All @@ -300,8 +300,8 @@ def test_ambiguous_pop_with_default(self):
self.assertSetEqual(set(ret.data), {val, self._convert.none})

def test_ambiguous_dict_after_pop(self):
ambiguous_key = self._convert.primitive_class_instances[str]
val = self._convert.primitive_class_instances[int]
ambiguous_key = self._convert.primitive_instances[str]
val = self._convert.primitive_instances[int]
node, _ = self._d.setitem_slot(
self._node, ambiguous_key.to_variable(self._node),
val.to_variable(self._node))
Expand All @@ -310,8 +310,8 @@ def test_ambiguous_dict_after_pop(self):
self.assertListEqual(ret.data, [val])

def test_ambiguous_dict_after_pop_with_default(self):
ambiguous_key = self._convert.primitive_class_instances[str]
val = self._convert.primitive_class_instances[int]
ambiguous_key = self._convert.primitive_instances[str]
val = self._convert.primitive_instances[int]
node, _ = self._d.setitem_slot(
self._node, ambiguous_key.to_variable(self._node),
val.to_variable(self._node))
Expand Down

0 comments on commit dac9453

Please sign in to comment.