Skip to content

Commit

Permalink
Merge pull request #1536 from google/google_sync
Browse files Browse the repository at this point in the history
Google sync
  • Loading branch information
rchen152 authored Nov 29, 2023
2 parents bdb0fe4 + 41556a8 commit 32c2620
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 47 deletions.
90 changes: 44 additions & 46 deletions pytype/abstract/_pytd_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,58 +259,56 @@ def compatible_with(new, existing, view):
obj = mutation.instance
name = mutation.name
values = mutation.value
if obj.from_annotation:
# We should check for parameter mismatches only if the class is
# generic. Consider:
# class A(tuple[int, int]): ...
# class B(tuple): ...
# Although pytype computes mutations for tuple.__new__ for both
# classes, the second implicitly inherits from tuple[Any, ...], so
# there are no restrictions on the container contents.
check_params = False
for cls in obj.cls.mro:
if isinstance(cls, _classes.ParameterizedClass):
check_params = True
break
elif cls.template:
break
else:
check_params = False
if not check_params:
if not obj.from_annotation:
filtered_mutations.append(function.Mutation(obj, name, values))
continue
params = obj.get_instance_type_parameter(name)
ps = {v for v in params.data if should_check(v)}
if ps:
filtered_values = self.ctx.program.NewVariable()
# check if the container type is being broadened.
new = []
short_name = name.rsplit(".", 1)[-1]
for b in values.bindings:
if not should_check(b.data) or b.data in ps:
filtered_values.PasteBinding(b)
continue
new_view = datatypes.AccessTrackingDict.merge(
combined_view, view, {values: b})
if not compatible_with(values, ps, new_view):
combination = [b]
bad_param = b.data.get_instance_type_parameter(short_name)
if bad_param in new_view:
combination.append(new_view[bad_param])
if not node.HasCombination(combination):
# Since HasCombination is expensive, we don't use it to
# pre-filter bindings, but once we think we have an error, we
# should double-check that the binding is actually visible. We
# also drop non-visible bindings from filtered_values.
continue
filtered_values.PasteBinding(b)
new.append(b.data)
if not ps:
# By updating filtered_mutations only when ps is non-empty, we
# filter out mutations to parameters with type Any.
filtered_mutations.append(
function.Mutation(obj, name, filtered_values))
if new:
errors[obj][short_name] = (params, values, obj.from_annotation)
continue
# We should check for parameter mismatches only if the class is generic.
# Consider:
# class A(tuple[int, int]): ...
# class B(tuple): ...
# Although pytype computes mutations for tuple.__new__ for both classes,
# the second implicitly inherits from tuple[Any, ...], so there are no
# restrictions on the container contents.
check_params = False
for cls in obj.cls.mro:
if isinstance(cls, _classes.ParameterizedClass):
check_params = True
break
elif cls.template:
break
filtered_values = self.ctx.program.NewVariable()
# check if the container type is being broadened.
new = []
short_name = name.rsplit(".", 1)[-1]
for b in values.bindings:
if not check_params or not should_check(b.data) or b.data in ps:
filtered_values.PasteBinding(b)
continue
new_view = datatypes.AccessTrackingDict.merge(
combined_view, view, {values: b})
if not compatible_with(values, ps, new_view):
combination = [b]
bad_param = b.data.get_instance_type_parameter(short_name)
if bad_param in new_view:
combination.append(new_view[bad_param])
if not node.HasCombination(combination):
# Since HasCombination is expensive, we don't use it to
# pre-filter bindings, but once we think we have an error, we
# should double-check that the binding is actually visible. We
# also drop non-visible bindings from filtered_values.
continue
filtered_values.PasteBinding(b)
new.append(b.data)
filtered_mutations.append(
function.Mutation(obj, name, filtered_values))
if new:
errors[obj][short_name] = (params, values, obj.from_annotation)

all_mutations = filtered_mutations

Expand Down
2 changes: 1 addition & 1 deletion pytype/stubs/builtins/typing.pytd
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ def is_typeddict(tp) -> bool: ...

# Python 3.11
LiteralString = str # TODO(b/303083512): Support LiteralString
class Required: ...
class Required(Generic[_T]): ...
class NotRequired(Generic[_T]): ...
Self = TypeVar('Self')
class TypeVarTuple: ...
Expand Down
8 changes: 8 additions & 0 deletions pytype/tests/test_dict2.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ def f(**kwargs):
kwargs.update(a=0, b='1')
""")

def test_any(self):
self.Check("""
from typing import Dict
def do_something(d: Dict) -> None:
d['a'] = 0
d['b'] = d['b'].split(':')
""")


if __name__ == "__main__":
test_base.main()

0 comments on commit 32c2620

Please sign in to comment.