Skip to content

Commit f892ff9

Browse files
authored
Merge pull request #1194 from google/google_sync
Google sync
2 parents efc1fd9 + 43fae0f commit f892ff9

File tree

13 files changed

+90
-31
lines changed

13 files changed

+90
-31
lines changed

CHANGELOG

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
Version 2022.04.26:
2+
3+
Updates:
4+
* Enable --enable-typed-dicts by default.
5+
6+
Bug fixes:
7+
* Preserve the full name of imported TypedDicts.
8+
* Stop dropping TypedDict annotations from local assignments.
9+
* Don't mark fields of anonymous namedtuple parents as ClassVars.
10+
* Instantiate imported TypedDicts to typed_dict.TypedDict, not Instance.
11+
* Fix more typeshed circular dependency issues.
12+
113
Version 2022.04.22:
214

315
Updates:

docs/support.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ of pytype.
1313
* [Typing](#typing)
1414
* [Third-Party Libraries](#third-party-libraries)
1515

16-
<!-- Added by: rechen, at: 2022-04-06T14:32-07:00 -->
16+
<!-- Added by: rechen, at: 2022-04-22T15:56-07:00 -->
1717

1818
<!--te-->
1919

@@ -66,7 +66,7 @@ Feature
6666
[PEP 563 -- Postponed Evaluation of Annotations][563] | 3.7 | ✅ |
6767
[PEP 585 -- Type Hinting Generics in Standard Collections][585] | 3.9 | ✅ |
6868
[PEP 586 -- Literal Types][586] | 3.8 | ✅ |
69-
[PEP 589 -- TypedDict][589] | 3.8 | ✅ | Requires `--enable-typed-dicts` flag externally
69+
[PEP 589 -- TypedDict][589] | 3.8 | ✅ |
7070
[PEP 591 -- Adding a Final Qualifier to Typing][591] | 3.8 | ✅ |
7171
[PEP 593 -- Flexible Function and Variable Annotations][593] | 3.9 | ✅ |
7272
[PEP 604 -- Allow Writing Union Types as X \| Y][604] | 3.10 | ❌ | [#785][union-pipe]

pytype/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# pylint: skip-file
2-
__version__ = '2022.04.22'
2+
__version__ = '2022.04.26'

pytype/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def add_basic_options(o):
160160
"Enable exhaustive checking of function parameter types."),
161161
("--enable-nested-classes", False,
162162
"Enable support for nested classes in .py files."),
163-
("--enable-typed-dicts", False,
163+
("--enable-typed-dicts", True,
164164
"Enable support for TypedDicts."),
165165
("--strict-primitive-comparisons", False,
166166
"Emit errors for comparisons between incompatible primitive types."),

pytype/convert.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,10 @@ def _constant_to_value(self, pyval, subst, get_node):
824824
instance = self._create_new_unknown_value("type")
825825
else:
826826
mycls = self.constant_to_value(cls, subst, self.ctx.root_node)
827-
instance = abstract.Instance(mycls, self.ctx)
827+
if isinstance(mycls, typed_dict.TypedDictClass):
828+
instance = mycls.instantiate_value(self.ctx.root_node, None)
829+
else:
830+
instance = abstract.Instance(mycls, self.ctx)
828831
log.info("New pytd instance for %s: %r", cls.name, instance)
829832
self._convert_cache[key] = instance
830833
return self._convert_cache[key]

pytype/load_pytd.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,7 @@ def resolve_builtin_types(self, mod_ast, *, lookup_ast=None):
312312
visitors.ExpandCompatibleBuiltins(self.builtins_ast))
313313
return mod_ast
314314

315-
def resolve_external_types(self, mod_ast, module_map, aliases, *,
316-
mod_name=None):
315+
def resolve_external_types(self, mod_ast, module_map, aliases, *, mod_name):
317316
name = mod_name or mod_ast.name
318317
try:
319318
mod_ast = mod_ast.Visit(visitors.LookupExternalTypes(
@@ -616,10 +615,15 @@ def finish_and_verify_ast(self, mod_ast):
616615
if mod_ast:
617616
try:
618617
self._resolver.verify(mod_ast)
619-
except BadDependencyError:
618+
except (BadDependencyError, visitors.ContainerError):
620619
# In the case of a circular import, an external type may be left
621-
# unresolved. As long as the module containing the unresolved type does
622-
# not also contain a circular import, an extra lookup should resolve it.
620+
# unresolved, so we re-resolve lookups in this module and its direct
621+
# dependencies. Technically speaking, we should re-resolve all
622+
# transitive imports, but lookups are expensive.
623+
dependencies = self._resolver.collect_dependencies(mod_ast)
624+
for k in dependencies:
625+
self._modules[k].ast = self._resolve_external_types(
626+
self._modules[k].ast)
623627
mod_ast = self._resolve_external_types(mod_ast)
624628
self._resolver.verify(mod_ast)
625629
return mod_ast

pytype/output.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -863,13 +863,19 @@ def add_attributes_from(instance):
863863
if not any(x.name == "typing.NamedTuple" for x in bases):
864864
bases.append(pytd.NamedType("typing.NamedTuple"))
865865

866-
if any(isinstance(x, named_tuple.NamedTupleClass)
867-
for x in missing_bases):
866+
has_namedtuple_parent = False
867+
parent_field_names = set()
868+
for x in missing_bases:
869+
if isinstance(x, named_tuple.NamedTupleClass):
870+
has_namedtuple_parent = True
871+
parent_field_names.update(field.name for field in x.props.fields)
872+
if has_namedtuple_parent:
868873
# If inheriting from an anonymous namedtuple, mark all derived class
869874
# constants as ClassVars, otherwise MergeBaseClasses will convert them
870875
# into namedtuple fields.
871-
for c in constants.values():
872-
c.wrap("typing.ClassVar")
876+
for k, c in constants.items():
877+
if k not in parent_field_names:
878+
c.wrap("typing.ClassVar")
873879

874880
final_constants = []
875881
if isinstance(v, named_tuple.NamedTupleClass):

pytype/overlays/typed_dict.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,15 @@ def _new_instance(self, container, node, args):
178178
ret.set_str_item(node, k, v)
179179
return ret
180180

181-
def instantiate(self, node, container=None):
181+
def instantiate_value(self, node, container):
182182
args = function.Args(())
183183
for name, typ in self.props.fields.items():
184184
args.namedargs[name] = self.ctx.join_variables(
185185
node, [t.instantiate(node) for t in typ.data])
186-
return self._new_instance(container, node, args).to_variable(node)
186+
return self._new_instance(container, node, args)
187+
188+
def instantiate(self, node, container=None):
189+
return self.instantiate_value(node, container).to_variable(node)
187190

188191
def make_class(self, *args, **kwargs):
189192
return self._base_cls.make_class(*args, **kwargs)

pytype/pytd/pytd.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -644,10 +644,7 @@ def IsContainer(t):
644644
for p in t.bases:
645645
if isinstance(p, GenericType):
646646
base = p.base_type
647-
# We need to check for Generic and Protocol again here because base may
648-
# not yet have been resolved to a ClassType.
649-
if (base.name in ('typing.Generic', 'typing.Protocol') or
650-
isinstance(base, ClassType) and IsContainer(base.cls)):
647+
if isinstance(base, ClassType) and IsContainer(base.cls):
651648
return True
652649
return False
653650

pytype/pytd/visitors.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,16 +1594,7 @@ def EnterGenericType(self, node):
15941594
raise ContainerError("Name %s must be defined as a TypeVar" % t.name)
15951595
elif not isinstance(node, (pytd.CallableType, pytd.TupleType)):
15961596
actual_param_count = len(node.parameters)
1597-
if actual_param_count and not base_type.cls.template:
1598-
# This AdjustTypeParameters() call is needed because we validate nodes
1599-
# before their type parameters have been adjusted in some circular
1600-
# import cases. The result of this adjustment is not saved because it
1601-
# may not be accurate if the container is only partially resolved, but
1602-
# it's good enough to avoid some spurious container validation errors.
1603-
cls = base_type.cls.Visit(AdjustTypeParameters())
1604-
else:
1605-
cls = base_type.cls
1606-
max_param_count = len(cls.template)
1597+
max_param_count = len(base_type.cls.template)
16071598
if actual_param_count > max_param_count:
16081599
raise ContainerError(
16091600
"Too many parameters on %s: expected %s, got %s" % (

0 commit comments

Comments
 (0)