Skip to content

Commit 8fdc7ef

Browse files
authored
Merge pull request #1200 from google/google_sync
Google sync
2 parents a2c5f97 + 148c42c commit 8fdc7ef

21 files changed

+358
-120
lines changed

CHANGELOG

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
Version 2022.05.05:
2+
3+
Updates:
4+
* Update typeshed pin to commit 354787f from Apr 8.
5+
* Remove --enable-typed-dicts, which has been a no-op since the last release.
6+
* Add a group of feature flags for checking compatibility of signatures for
7+
overriding methods:
8+
* --overriding-default-value-checks
9+
* --overriding-parameter-count-checks
10+
* --overriding-parameter-name-checks
11+
* --overriding-parameter-type-checks
12+
* --overriding-return-type-checks
13+
* Add code structure for gradual migration to argument-by-argument call
14+
matching.
15+
16+
Bug fixes:
17+
* Move a check out of --enable-nested-classes.
18+
* Fix spurious not-callable error for typing.OrderedDict.
19+
120
Version 2022.04.26:
221

322
Updates:

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.26'
2+
__version__ = '2022.05.05'

pytype/abstract/_classes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,7 @@ def get_inner_classes(self):
185185
continue
186186
if value.official_name is None or (
187187
self.official_name and
188-
value.official_name.startswith(f"{self.official_name}.")) or (
189-
not self.ctx.options.enable_nested_classes and value != self):
188+
value.official_name.startswith(f"{self.official_name}.")):
190189
inner_classes.append(value)
191190
return inner_classes
192191

pytype/overlays/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ py_library(
9292
DEPS
9393
.named_tuple
9494
.overlay
95+
.typing_overlay
9596
)
9697

9798
py_library(

pytype/overlays/collections_overlay.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from pytype.overlays import named_tuple
44
from pytype.overlays import overlay
5+
from pytype.overlays import typing_overlay
56

67

78
class CollectionsOverlay(overlay.Overlay):
@@ -27,3 +28,10 @@ def __init__(self, ctx):
2728
"namedtuple": overlay.build(
2829
"namedtuple", named_tuple.NamedTupleBuilder.make),
2930
}
31+
32+
33+
class ABCOverlay(typing_overlay.Redirect):
34+
"""A custom overlay for the 'collections.abc' module."""
35+
36+
def __init__(self, ctx):
37+
super().__init__("collections.abc", {}, ctx)

pytype/overlays/overlay_dict.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"attr": attr_overlay.AttrOverlay,
3333
"chex": chex_overlay.ChexOverlay,
3434
"collections": collections_overlay.CollectionsOverlay,
35+
"collections.abc": collections_overlay.ABCOverlay,
3536
"dataclasses": dataclass_overlay.DataclassOverlay,
3637
"enum": enum_overlay.EnumOverlay,
3738
"flax.struct": flax_overlay.DataclassOverlay,
Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,14 @@
11
"""Implementation of special members of typing_extensions."""
22

3-
from pytype.overlays import overlay
43
from pytype.overlays import typing_overlay
54

65

7-
class TypingExtensionsOverlay(overlay.Overlay):
6+
class TypingExtensionsOverlay(typing_overlay.Redirect):
87
"""A custom overlay for the 'typing_extensions' module."""
98

109
def __init__(self, ctx):
11-
ast = ctx.loader.import_name("typing_extensions")
12-
member_map = {
13-
"runtime": _build("typing.runtime_checkable"),
14-
}
15-
for pyval in ast.aliases + ast.classes + ast.constants + ast.functions:
16-
# Any public typing_extensions members that are not explicitly implemented
17-
# are unsupported.
18-
_, name = pyval.name.rsplit(".", 1)
19-
if name.startswith("_"):
20-
continue
21-
if name in typing_overlay.typing_overlay:
22-
member_map[name] = typing_overlay.typing_overlay[name][0]
23-
elif f"typing.{name}" in ctx.loader.typing:
24-
member_map[name] = _build(f"typing.{name}")
25-
elif name not in member_map:
26-
member_map[name] = _build_not_supported_yet(name, ast)
27-
super().__init__(ctx, "typing_extensions", member_map, ast)
10+
aliases = {"runtime": "typing.runtime_checkable"}
11+
super().__init__("typing_extensions", aliases, ctx)
2812

2913
def _convert_member(self, name, member, subst=None):
3014
var = super()._convert_member(name, member, subst)
@@ -35,11 +19,3 @@ def _convert_member(self, name, member, subst=None):
3519
# on an abstract value.
3620
val.module = "typing"
3721
return var
38-
39-
40-
def _build(name):
41-
return lambda ctx: ctx.convert.name_to_value(name)
42-
43-
44-
def _build_not_supported_yet(name, ast):
45-
return lambda ctx: typing_overlay.not_supported_yet(name, ctx, ast=ast)

pytype/overlays/typing_overlay.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,38 @@ def _convert_member(
6161
return super()._convert_member(name, builder, subst)
6262

6363

64+
class Redirect(overlay.Overlay):
65+
"""Base class for overlays that redirect to typing."""
66+
67+
def __init__(self, module_name, aliases, ctx):
68+
ast = ctx.loader.import_name(module_name)
69+
member_map = {k: _build(v) for k, v in aliases.items()}
70+
for pyval in ast.aliases + ast.classes + ast.constants + ast.functions:
71+
# Any public members that are not explicitly implemented are unsupported.
72+
_, name = pyval.name.rsplit(".", 1)
73+
if name.startswith("_"):
74+
continue
75+
if name in typing_overlay:
76+
member_map[name] = typing_overlay[name][0]
77+
elif f"typing.{name}" in ctx.loader.typing:
78+
member_map[name] = _build(f"typing.{name}")
79+
elif name not in member_map:
80+
member_map[name] = _build_not_supported_yet(name, ast)
81+
super().__init__(ctx, module_name, member_map, ast)
82+
83+
84+
def _build(name):
85+
def resolve(ctx):
86+
ast = ctx.loader.typing
87+
pytd_type = pytd.ToType(ast.Lookup(name), True, True, True)
88+
return ctx.convert.constant_to_value(pytd_type)
89+
return resolve
90+
91+
92+
def _build_not_supported_yet(name, ast):
93+
return lambda ctx: not_supported_yet(name, ctx, ast=ast)
94+
95+
6496
class Union(abstract.AnnotationClass):
6597
"""Implementation of typing.Union[...]."""
6698

pytype/pytd/serialize_ast.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,11 @@ def _LookupClassReferences(serializable_ast, module_map, self_name):
202202
class_lookup = visitors.LookupExternalTypes(module_map, self_name=self_name)
203203
raw_ast = serializable_ast.ast
204204

205+
decorators = {d.type.name for c in raw_ast.classes for d in c.decorators} # pylint: disable=g-complex-comprehension
206+
205207
for node in (serializable_ast.class_type_nodes or ()):
206208
try:
209+
class_lookup.allow_functions = node.name in decorators
207210
if node is not class_lookup.VisitClassType(node):
208211
serializable_ast = serializable_ast.Replace(class_type_nodes=None)
209212
break

pytype/pytd/visitors.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ def __init__(self, allow_singletons):
250250
self._in_alias = False
251251
self._in_literal = 0
252252
self.allow_singletons = allow_singletons
253+
self.allow_functions = False
253254

254255
def EnterAlias(self, _):
255256
assert not self._in_alias
@@ -267,7 +268,7 @@ def LeaveLiteral(self, _):
267268

268269
def to_type(self, t):
269270
allow_constants = self._in_alias or self._in_literal
270-
allow_functions = self._in_alias
271+
allow_functions = self._in_alias or self.allow_functions
271272
return pytd.ToType(
272273
t, allow_constants=allow_constants, allow_functions=allow_functions,
273274
allow_singletons=self.allow_singletons)

0 commit comments

Comments
 (0)