Skip to content

Commit

Permalink
Merge pull request #1031 from google/google_sync
Browse files Browse the repository at this point in the history
Google sync
  • Loading branch information
rchen152 committed Oct 14, 2021
2 parents 54e6b6e + c0dd119 commit aee09b6
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 18 deletions.
1 change: 0 additions & 1 deletion pytype/pyi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ py_library(
.types
pytype.utils
pytype.pytd.pytd_for_parser
pytype.pytd.parse.parse
)

py_library(
Expand Down
13 changes: 7 additions & 6 deletions pytype/pyi/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def _maybe_resolve_alias(alias, name_to_class, name_to_constant):
return value.Replace(name=alias.name)


def pytd_literal(parameters: List[Any]) -> pytd_node.Node:
def pytd_literal(parameters: List[Any]) -> pytd.Type:
"""Create a pytd.Literal."""
literal_parameters = []
for p in parameters:
Expand Down Expand Up @@ -175,7 +175,7 @@ def _convert_annotated(x):
raise ParseError(f"Cannot convert metadata {x}")


def pytd_annotated(parameters: List[Any]) -> pytd_node.Node:
def pytd_annotated(parameters: List[Any]) -> pytd.Type:
"""Create a pytd.Annotated."""
if len(parameters) < 2:
raise ParseError(
Expand Down Expand Up @@ -482,7 +482,7 @@ def _is_empty_tuple(self, t):
def _is_heterogeneous_tuple(self, t):
return isinstance(t, pytd.TupleType)

def _parameterized_type(self, base_type, parameters):
def _parameterized_type(self, base_type: Any, parameters):
"""Return a parameterized type."""
if self._matches_named_type(base_type, _LITERAL_TYPES):
return pytd_literal(parameters)
Expand Down Expand Up @@ -528,7 +528,7 @@ def _parameterized_type(self, base_type, parameters):
assert parameters
return pytd.GenericType(base_type=base_type, parameters=parameters)

def resolve_type(self, name: Union[str, pytd_node.Node]) -> pytd_node.Node:
def resolve_type(self, name: Union[str, pytd_node.Node]) -> pytd.Type:
"""Return the fully resolved name for an alias.
Args:
Expand All @@ -540,7 +540,8 @@ def resolve_type(self, name: Union[str, pytd_node.Node]) -> pytd_node.Node:
if isinstance(name, (pytd.GenericType, pytd.AnythingType)):
return name
if isinstance(name, pytd.NamedType):
name = name.name # pytype: disable=attribute-error
name = name.name
assert isinstance(name, str)
if name == "nothing":
return pytd.NothingType()
base_type = self.type_map.get(name)
Expand All @@ -554,7 +555,7 @@ def new_type(
self,
name: Union[str, pytd_node.Node],
parameters: Optional[List[pytd_node.Node]] = None
) -> pytd_node.Node:
) -> pytd.Type:
"""Return the AST for a type.
Args:
Expand Down
3 changes: 1 addition & 2 deletions pytype/pyi/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from pytype.pytd import pytd
from pytype.pytd import visitors
from pytype.pytd.codegen import function as pytd_function
from pytype.pytd.parse import node as pytd_node

from typed_ast import ast3

Expand Down Expand Up @@ -134,7 +133,7 @@ def from_function(cls, function: ast3.AST, is_async: bool) -> "NameAndSig":
def _pytd_signature(
function: ast3.AST,
is_async: bool,
exceptions: Optional[List[pytd_node.Node]] = None
exceptions: Optional[List[pytd.Type]] = None
) -> pytd.Signature:
"""Construct a pytd signature from an ast.FunctionDef node."""
name = function.name
Expand Down
1 change: 0 additions & 1 deletion pytype/pytd/codegen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ py_library(
pytdgen.py
DEPS
pytype.pytd.pytd_for_codegen
pytype.pytd.parse.parse
)

py_library(
Expand Down
7 changes: 3 additions & 4 deletions pytype/pytd/codegen/pytdgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import Tuple

from pytype.pytd import pytd
from pytype.pytd.parse import node as pytd_node


_STRING_TYPES = ("str", "bytes", "unicode")
Expand All @@ -13,7 +12,7 @@
Parameters = Tuple[pytd.Type, ...]


def pytd_list(typ: str) -> pytd_node.Node:
def pytd_list(typ: str) -> pytd.Type:
if typ:
return pytd.GenericType(
pytd.NamedType("typing.List"), (pytd.NamedType(typ),))
Expand Down Expand Up @@ -41,14 +40,14 @@ def heterogeneous_tuple(
return pytd.TupleType(base_type=base_type, parameters=parameters)


def pytd_type(value: pytd.Type) -> pytd_node.Node:
def pytd_type(value: pytd.Type) -> pytd.Type:
return pytd.GenericType(pytd.NamedType("type"), (value,))


def pytd_callable(
base_type: pytd.NamedType,
parameters: Parameters
) -> pytd_node.Node:
) -> pytd.Type:
"""Create a pytd.CallableType."""
if isinstance(parameters[0], list):
if len(parameters) > 2:
Expand Down
17 changes: 15 additions & 2 deletions pytype/pytd/pytd.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import collections
import itertools

from typing import Any, Optional, Tuple, Union
import typing
from typing import Any, Optional, Tuple, TypeVar, Union

import attr

Expand All @@ -26,10 +27,22 @@
# Alias node.Node for convenience.
Node = node.Node

_TypeT = TypeVar('_TypeT', bound='Type')


# Each type class below should inherit from this mixin.
class Type:
"""Each type class below should inherit from this mixin."""
name: Optional[str]

# We type-annotate many things as pytd.Type when we'd really want them to be
# Intersection[pytd.Type, pytd.parse.node.Node], so we need to copy the type
# signature of Node.Visit here.
if typing.TYPE_CHECKING:

def Visit(self: _TypeT, visitor, *args, **kwargs) -> _TypeT:
del visitor, args, kwargs # unused
return self

__slots__ = ()


Expand Down
2 changes: 0 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ exclude =
**/test_*.py
**/*_test_*.py
pytype/pyi/classdef.py
pytype/pyi/definitions.py
pytype/pyi/function.py
pytype/pyi/parser.py
pytype/pyi/visitor.py
pytype/tools/merge_pyi/test_data/
pytype/tools/xref/testdata/

0 comments on commit aee09b6

Please sign in to comment.