Skip to content

Commit 8d6dec4

Browse files
martindemellorchen152
authored andcommitted
Do not call convert.name_to_value() directly in typing overlays.
Calling pytd.ToType first ensures we handle cases like type aliases. PiperOrigin-RevId: 446583723
1 parent d22e763 commit 8d6dec4

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

pytype/overlays/typing_overlay.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ def __init__(self, module_name, aliases, ctx):
8181
super().__init__(ctx, module_name, member_map, ast)
8282

8383

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+
8496
class Union(abstract.AnnotationClass):
8597
"""Implementation of typing.Union[...]."""
8698

@@ -433,14 +445,6 @@ def _build_value(self, node, inner, ellipses):
433445
return self.ctx.convert.merge_values(values)
434446

435447

436-
def _build(name):
437-
return lambda ctx: ctx.convert.name_to_value(name)
438-
439-
440-
def _build_not_supported_yet(name, ast):
441-
return lambda ctx: not_supported_yet(name, ctx, ast=ast)
442-
443-
444448
def not_supported_yet(name, ctx, *, ast=None, details=None):
445449
ast = ast or ctx.loader.typing
446450
full_name = f"{ast.name}.{name}"

pytype/tests/test_collections_abc.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ class Foo(collections.abc.Mapping):
1414
pass
1515
""")
1616

17+
def test_bytestring(self):
18+
"""Check that we handle type aliases."""
19+
self.Check("""
20+
import collections
21+
x: collections.abc.ByteString
22+
""")
23+
1724
def test_callable(self):
1825
ty = self.Infer("""
1926
from collections.abc import Callable

0 commit comments

Comments
 (0)