10000 Fix crash caused by typing.TypeVar lookup failure. · google/pytype@dc8fb5d · GitHub
[go: up one dir, main page]

Skip to content

Commit dc8fb5d

Browse files
committed
Fix crash caused by typing.TypeVar lookup failure.
I previously fixed the pyi parser's handling of `from typing import TypeVar as _TypeVar`, but I neglected to check that the parsed ast could then be successfully resolved by load_pytd. Context: python/typeshed#10201. PiperOrigin-RevId: 535500076
1 parent fced8ae commit dc8fb5d

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

pytype/load_pytd_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,14 @@ class B: ...
637637
a = ast.Lookup("foo.A")
638638
self.assertEqual(a.Lookup("x").type.cls, a.Lookup("foo.A.B"))
639639

640+
def test_alias_typevar(self):
641+
ast = self._import(foo="""
642+
from typing import TypeVar as _TypeVar
643+
T = _TypeVar('T')
644+
""")
645+
self.assertEqual(ast.Lookup("foo.T"),
646+
pytd.TypeParameter(name="T", scope="foo"))
647+
640648

641649
class ImportTypeMacroTest(_LoaderTest):
642650

pytype/stubs/builtins/typing.pytd

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
# Special-cased by the parser: TypeVar, Any, NamedTuple, Optional, Union
21
__all__ = ... # type: List[str]
2+
3+
# Special-cased by the parser
4+
class Any: ...
5+
TypeVar: Any
6+
Optional: Any
7+
Union: Any
8+
39
if sys.version_info >= (3, 0):
410
AnyStr = TypeVar('AnyStr', str, bytes)
511
else:

0 commit comments

Comments
 (0)
0