Skip to content

Commit

Permalink
Fix crash caused by typing.TypeVar lookup failure.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rchen152 committed May 31, 2023
1 parent fced8ae commit dc8fb5d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 8 additions & 0 deletions pytype/load_pytd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,14 @@ class B: ...
a = ast.Lookup("foo.A")
self.assertEqual(a.Lookup("x").type.cls, a.Lookup("foo.A.B"))

def test_alias_typevar(self):
ast = self._import(foo="""
from typing import TypeVar as _TypeVar
T = _TypeVar('T')
""")
self.assertEqual(ast.Lookup("foo.T"),
pytd.TypeParameter(name="T", scope="foo"))


class ImportTypeMacroTest(_LoaderTest):

Expand Down
8 changes: 7 additions & 1 deletion pytype/stubs/builtins/typing.pytd
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Special-cased by the parser: TypeVar, Any, NamedTuple, Optional, Union
__all__ = ... # type: List[str]

# Special-cased by the parser
class Any: ...
TypeVar: Any
Optional: Any
Union: Any

if sys.version_info >= (3, 0):
AnyStr = TypeVar('AnyStr', str, bytes)
else:
Expand Down

0 comments on commit dc8fb5d

Please sign in to comment.