Skip to content

Commit 91e5aa6

Browse files
committed
Improve the collections_abc overlay.
* Handle typing containers better. * make collections_abc.Set an alias for typing.AbstractSet: https://github.com/python/typeshed/blo[]fc7768c4df6f713e81a0078e72b9805679c25f/stdlib/_collections_abc.pyi#L4 PiperOrigin-RevId: 446856323
1 parent a6b08cd commit 91e5aa6

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

pytype/overlays/collections_overlay.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ class ABCOverlay(typing_overlay.Redirect):
3434
"""A custom overlay for the 'collections.abc' module."""
3535

3636
def __init__(self, ctx):
37-
super().__init__("collections.abc", {}, ctx)
37+
super().__init__("collections.abc", {"Set": "typing.AbstractSet"}, ctx)

pytype/overlays/typing_overlay.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
Param = overlay_utils.Param
2424

2525

26+
def _is_typing_container(cls: pytd.Class):
27+
return pytd.IsContainer(cls) and cls.template
28+
29+
2630
class TypingOverlay(overlay.Overlay):
2731
"""A representation of the 'typing' module that allows custom overlays.
2832
@@ -39,7 +43,7 @@ def __init__(self, ctx):
3943
ast = ctx.loader.typing
4044
for cls in ast.classes:
4145
_, name = cls.name.rsplit(".", 1)
42-
if name not in member_map and pytd.IsContainer(cls) and cls.template:
46+
if name not in member_map and _is_typing_container(cls):
4347
member_map[name] = (overlay.build(name, TypingContainer), None)
4448
super().__init__(ctx, "typing", member_map, ast)
4549

@@ -70,7 +74,7 @@ def __init__(self, module_name, aliases, ctx):
7074
for pyval in ast.aliases + ast.classes + ast.constants + ast.functions:
7175
# Any public members that are not explicitly implemented are unsupported.
7276
_, name = pyval.name.rsplit(".", 1)
73-
if name.startswith("_"):
77+
if name.startswith("_") or name in member_map:
7478
continue
7579
if name in typing_overlay:
7680
member_map[name] = typing_overlay[name][0]
@@ -84,6 +88,9 @@ def __init__(self, module_name, aliases, ctx):
8488
def _build(name):
8589
def resolve(ctx):
8690
ast = ctx.loader.typing
91+
pytd_val = ast.Lookup(name)
92+
if isinstance(pytd_val, pytd.Class) and _is_typing_container(pytd_val):
93+
return TypingContainer(name.rsplit(".", 1)[-1], ctx)
8794
pytd_type = pytd.ToType(ast.Lookup(name), True, True, True)
8895
return ctx.convert.constant_to_value(pytd_type)
8996
return resolve

pytype/tests/test_collections_abc.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ def f() -> Callable[[], float]: ...
5353
x: float
5454
""")
5555

56+
def test_generator(self):
57+
self.Check("""
58+
from collections.abc import Generator
59+
def f() -> Generator[int, None, None]:
60+
yield 0
61+
""")
62+
63+
def test_set(self):
64+
# collections.abc.Set is an alias for typing.AbstractSet.
65+
self.Check("""
66+
from collections.abc import Set
67+
def f() -> Set[int]:
68+
return frozenset([0])
69+
""")
70+
5671

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

0 commit comments

Comments
 (0)