10000 Merge TypeVarDef and TypeVarType by hauntsaninja · Pull Request #9951 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Merge TypeVarDef and TypeVarType #9951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'upstream/master' into typevar2
  • Loading branch information
hauntsaninja committed Aug 2, 2021
commit 4e82ce9e24ad56cbb2bd708b7de30d26f49b5110
9 changes: 4 additions & 5 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3757,7 +3757,7 @@ def check_generator_or_comprehension(self, gen: GeneratorExpr,
# Infer the type of the list comprehension by using a synthetic generic
# callable type.
tvdef = TypeVarType('T', 'T', -1, [], self.object_type())
tv_list = [tvdef] # type: List[Type]
tv_list: List[Type] = [tvdef]
constructor = CallableType(
tv_list,
[nodes.ARG_POS],
Expand Down Expand Up @@ -4462,10 +4462,9 @@ def merge_typevars_in_callables_by_name(

Returns both the new list of callables and a list of all distinct TypeVarType objects used.
"""

output = [] # type: List[CallableType]
unique_typevars = {} # type: Dict[str, TypeVarType]
variables = [] # type: List[TypeVarType]
output: List[CallableType] = []
unique_typevars: Dict[str, TypeVarType] = {}
variables: List[TypeVarType] = []

for target in callables:
if target.is_generic():
Expand Down
9 changes: 4 additions & 5 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,11 +921,10 @@ def deserialize(cls, data: JsonDict) -> 'Var':

class ClassDef(Statement):
"""Class definition"""

name = None # type: str # Name of the class without module prefix
fullname = None # type: Bogus[str] # Fully qualified name of the class
defs = None # type: Block
type_vars = None # type: List[mypy.types.TypeVarType]
name: str # Name of the class without module prefix
fullname: Bogus[str] = None # type: ignore # Fully qualified name of the class
defs: "Block"
type_vars: List["mypy.types.TypeVarType"]
# Base class expressions (not semantically analyzed -- can be arbitrary expressions)
base_type_exprs: List[Expression]
# Special base classes like Generic[...] get moved here during semantic analysis
Expand Down
2 changes: 1 addition & 1 deletion mypy/plugins/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
add_method, _get_decorator_bool_argument, deserialize_and_fixup_type,
)
from mypy.typeops import map_type_from_supertype
from mypy.types import Type, Instance, NoneType, TypeVarType, get_proper_type
from mypy.types import Type, Instance, NoneType, TypeVarType, CallableType, get_proper_type
from mypy.server.trigger import make_wildcard_trigger

# The set of decorators that generate dataclasses.
Expand Down
4 changes: 2 additions & 2 deletions mypy/plugins/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
)
from mypy.plugins.common import try_getting_str_literals
from mypy.types import (
Type, Instance, AnyType, TypeOfAny, CallableType, NoneType, TypedDictType,
TypeVarType, TypeVarType, TPDICT_FB_NAMES, get_proper_type, LiteralType
FunctionLike, Type, Instance, AnyType, TypeOfAny, CallableType, NoneType, TypedDictType,
TypeVarType, TPDICT_FB_NAMES, get_proper_type, LiteralType
)
from mypy.subtypes import is_subtype
from mypy.typeops import make_simplified_union
Expand Down
2 changes: 1 addition & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,7 @@ class Foo(Bar, Generic[T]): ...
# grained incremental mode.
defn.removed_base_type_exprs.append(defn.base_type_exprs[i])
del base_ty ED4F pe_exprs[i]
tvar_defs = [] # type: List[TypeVarType]
tvar_defs: List[TypeVarType] = []
for name, tvar_expr in declared_tvars:
tvar_def = self.tvar_scope.bind_new(name, tvar_expr)
assert isinstance(tvar_def, TypeVarType), (
Expand Down
4 changes: 2 additions & 2 deletions mypy/server/astmerge.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@
from mypy.traverser import TraverserVisitor
from mypy.types import (
Type, SyntheticTypeVisitor, Instance, AnyType, NoneType, CallableType, ErasedType, DeletedType,
TupleType, TypeType, TypedDictType, UnboundType, UninhabitedType, UnionType,
TupleType, TypeType, TypeVarType, TypedDictType, UnboundType, UninhabitedType, UnionType,
Overloaded, TypeVarType, TypeList, CallableArgument, EllipsisType, StarType, LiteralType,
RawExpressionType, PartialType, PlaceholderType, TypeAliasType
RawExpressionType, PartialType, PlaceholderType, TypeAliasType, TypeGuardType
)
from mypy.util import get_prefix, replace_object_state
from mypy.typestate import TypeState
Expand Down
5 changes: 2 additions & 3 deletions mypy/test/testtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
from mypy.indirection import TypeIndirectionVisitor
from mypy.types import (
UnboundType, AnyType, CallableType, TupleType, TypeVarType, Type, Instance, NoneType,
Overloaded, TypeType, UnionType, UninhabitedType, TypeVarId, TypeOfAny,
LiteralType, get_proper_type
Overloaded, TypeType, UnionType, UninhabitedType, TypeVarId, TypeOfAny, get_proper_type
)
from mypy.nodes import ARG_POS, ARG_OPT, ARG_STAR, ARG_STAR2, CONTRAVARIANT, INVARIANT, COVARIANT
from mypy.subtypes import is_subtype, is_more_precise, is_proper_subtype
Expand Down Expand Up @@ -495,7 +494,7 @@ def callable(self, vars: List[str], *a: Type) -> CallableType:
argument types a1, ... an and return type r and type arguments
vars.
"""
tv = [] # type: List[TypeVarType]
tv: List[TypeVarType] = []
n = -1
for v in vars:
tv.append(TypeVarType(v, v, n, [], self.fx.o))
Expand Down
6 changes: 3 additions & 3 deletions mypy/test/typefixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from typing import List, Optional, Tuple

from mypy.types import (
Type, AnyType, NoneType, Instance, CallableType, TypeVarType, TypeType,
UninhabitedType, TypeOfAny, TypeAliasType, UnionType
Type, TypeVarType, AnyType, NoneType, Instance, CallableType, TypeVarType, TypeType,
UninhabitedType, TypeOfAny, TypeAliasType, UnionType, LiteralType
)
from mypy.nodes import (
TypeInfo, ClassDef, Block, ARG_POS, ARG_OPT, ARG_STAR, SymbolTable,
Expand Down Expand Up @@ -223,7 +223,7 @@ def make_type_info(self, name: str,
module_name = '__main__'

if typevars:
v = [] # type: List[TypeVarType]
v: List[TypeVarType] = []
for id, n in enumerate(typevars, 1):
if variances:
variance = variances[id - 1]
Expand Down
6 changes: 3 additions & 3 deletions mypy/tvar_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self,
prohibited: Type variables that aren't strictly in scope exactly,
but can't be bound because they're part of an outer class's scope.
"""
self.scope = {} # type: Dict[str, TypeVarLikeType]
self.scope: Dict[str, TypeVarLikeType] = {}
self.parent = parent
self.func_id = 0
self.class_id = 0
Expand Down Expand Up @@ -63,7 +63,7 @@ def bind_new(self, name: str, tvar_expr: TypeVarLikeExpr) -> TypeVarLikeType:
self.func_id -= 1
i = self.func_id
if isinstance(tvar_expr, TypeVarExpr):
tvar_def = TypeVarType(
tvar_def: TypeVarLikeType = TypeVarType(
name,
tvar_expr.fullname,
i,
Expand All @@ -72,7 +72,7 @@ def bind_new(self, name: str, tvar_expr: TypeVarLikeExpr) -> TypeVarLikeType:
variance=tvar_expr.variance,
line=tvar_expr.line,
column=tvar_expr.column
) # type: TypeVarLikeType
)
elif isinstance(tvar_expr, ParamSpecExpr):
tvar_def = ParamSpecType(
name,
Expand Down
6 changes: 3 additions & 3 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from mypy.options import Options
from mypy.types import (
Type, UnboundType, TypeVarType, TupleType, TypedDictType, UnionType, Instance, AnyType,
CallableType, NoneType, ErasedType, DeletedType, TypeList, SyntheticTypeVisitor,
StarType, PartialType, EllipsisType, UninhabitedType, TypeType,
CallableType, NoneType, ErasedType, DeletedType, TypeList, TypeVarType, SyntheticTypeVisitor,
StarType, PartialType, EllipsisType, UninhabitedType, TypeType, TypeGuardType, TypeVarLikeType,
CallableArgument, TypeQuery, union_items, TypeOfAny, LiteralType, RawExpressionType,
PlaceholderType, Overloaded, get_proper_type, TypeAliasType, TypeVarLikeType, ParamSpecType
)
Expand Down Expand Up @@ -938,7 +938,7 @@ def bind_function_type_variables(
# Do not define a new type variable if already defined in scope.
typevars = [(name, tvar) for name, tvar in typevars
if not self.is_defined_type_var(name, defn)]
defs = [] # type: List[TypeVarLikeType]
defs: List[TypeVarLikeType] = []
for name, tvar in typevars:
if not self.tvar_scope.allow_binding(tvar.fullname):
self.fail('Type variable "{}" is bound by an outer class'.format(name), defn)
Expand Down
4 changes: 2 additions & 2 deletions mypy/typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def class_callable(init_type: CallableType, info: TypeInfo, type_type: Instance,
special_sig: Optional[str],
is_new: bool, orig_self_type: Optional[Type] = None) -> CallableType:
"""Create a type object type based on the signature of __init__."""
variables = [] # type: List[TypeVarLikeType]
variables: List[TypeVarLikeType] = []
variables.extend(info.defn.type_vars)
variables.extend(init_type.variables)

Expand Down Expand Up @@ -228,7 +228,7 @@ class B(A): pass
return cast(F, func)
self_param_type = get_proper_type(func.arg_types[0])

variables = [] # type: Sequence[TypeVarLikeType]
variables: Sequence[TypeVarLikeType] = []
if func.variables and supported_self_type(self_param_type):
if original_type is None:
# TODO: type check method override (see #7861).
Expand Down
1 change: 0 additions & 1 deletion mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,6 @@ def copy_modified(self, *,
def has_readable_member(self, name: str) -> bool:
return self.type.has_readable_member(name)


class FunctionLike(ProperType):
"""Abstract base class for function types."""

Expand Down
2 changes: 1 addition & 1 deletion mypy/typevars.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def fill_typevars(typ: TypeInfo) -> Union[Instance, TupleType]:

For a generic G type with parameters T1, .., Tn, return G[T1, ..., Tn].
"""
tvs = [] # type: List[Type]
tvs: List[Type] = []
# TODO: why do we need to keep both typ.type_vars and typ.defn.type_vars?
for i in range(len(typ.defn.type_vars)):
tv = typ.defn.type_vars[i]
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.
0