Skip to content

Option to disallow omitting type parameters of generic types #3637

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 4 commits into from
Jul 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def type_check_only(sources: List[BuildSource], bin_dir: str, options: Options)
options=options)


disallow_any_options = ['unimported', 'expr', 'unannotated', 'decorated', 'explicit']
disallow_any_options = ['unimported', 'expr', 'unannotated', 'decorated', 'explicit', 'generics']


def disallow_any_argument_type(raw_options: str) -> List[str]:
Expand Down
2 changes: 2 additions & 0 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
GENERIC_INSTANCE_VAR_CLASS_ACCESS = 'Access to generic instance variables via class is ambiguous'
CANNOT_ISINSTANCE_TYPEDDICT = 'Cannot use isinstance() with a TypedDict type'
CANNOT_ISINSTANCE_NEWTYPE = 'Cannot use isinstance() with a NewType type'
BARE_GENERIC = 'Missing type parameters for generic type'
IMPLICIT_GENERIC_ANY_BUILTIN = 'Implicit generic "Any". Use \'{}\' and specify generic parameters'

ARG_CONSTRUCTOR_NAMES = {
ARG_POS: "Arg",
Expand Down
29 changes: 22 additions & 7 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,20 @@
from mypy.errors import Errors, report_internal_error
from mypy.messages import CANNOT_ASSIGN_TO_TYPE, MessageBuilder
from mypy.types import (
NoneTyp, CallableType, Overloaded, Instance, Type, TypeVarType, AnyType,
FunctionLike, UnboundType, TypeList, TypeVarDef, TypeType,
TupleType, UnionType, StarType, EllipsisType, function_type, TypedDictType,
FunctionLike, UnboundType, TypeVarDef, TypeType, TupleType, UnionType, StarType, function_type,
TypedDictType, NoneTyp, CallableType, Overloaded, Instance, Type, TypeVarType, AnyType,
TypeTranslator,
)
from mypy.nodes import implicit_module_attrs
from mypy.typeanal import (
TypeAnalyser, TypeAnalyserPass3, analyze_type_alias, no_subscript_builtin_alias,
TypeVariableQuery, TypeVarList, remove_dups, has_any_from_unimported_type,
check_for_explicit_any
check_for_explicit_any, collect_any_types,
)
from mypy.exprtotype import expr_to_unanalyzed_type, TypeTranslationError
from mypy.sametypes import is_same_type
from mypy.options import Options
from mypy import experiments
from mypy import experiments, messages
from mypy.plugin import Plugin
from mypy import join

Expand Down Expand Up @@ -231,7 +230,7 @@ class SemanticAnalyzer(NodeVisitor):
loop_depth = 0 # Depth of breakable loops
cur_mod_id = '' # Current module id (or None) (phase 2)
is_stub_file = False # Are we analyzing a stub file?
is_typeshed_stub_file = False # Are we analyzing a typeshed stub file?
is_typeshed_stub_file = False # Are we analyzing a typeshed stub file?
imports = None # type: Set[str] # Imported modules (during phase 2 analysis)
errors = None # type: Errors # Keeps track of generated errors
plugin = None # type: Plugin # Mypy plugin for special casing of library features
Expand Down Expand Up @@ -1535,6 +1534,8 @@ def type_analyzer(self, *,
tvar_scope,
self.fail,
self.plugin,
self.options,
self.is_typeshed_stub_file,
aliasing=aliasing,
allow_tuple_literal=allow_tuple_literal,
allow_unnormalized=self.is_stub_file)
Expand Down Expand Up @@ -1574,6 +1575,8 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
self.tvar_scope,
self.fail,
self.plugin,
self.options,
self.is_typeshed_stub_file,
allow_unnormalized=True)
if res and (not isinstance(res, Instance) or res.args):
# TODO: What if this gets reassigned?
Expand Down Expand Up @@ -3199,6 +3202,8 @@ def visit_index_expr(self, expr: IndexExpr) -> None:
self.tvar_scope,
self.fail,
self.plugin,
self.options,
self.is_typeshed_stub_file,
allow_unnormalized=self.is_stub_file)
expr.analyzed = TypeAliasExpr(res, fallback=self.alias_fallback(res),
in_runtime=True)
Expand Down Expand Up @@ -3886,6 +3891,7 @@ def __init__(self, modules: Dict[str, MypyFile], errors: Errors) -> None:
def visit_file(self, file_node: MypyFile, fnam: str, options: Options) -> None:
self.errors.set_file(fnam, file_node.fullname())
self.options = options
self.is_typeshed_file = self.errors.is_typeshed_file(fnam)
with experiments.strict_optional_set(options.strict_optional):
self.accept(file_node)

Expand Down Expand Up @@ -4017,8 +4023,17 @@ def visit_type_application(self, e: TypeApplication) -> None:

def analyze(self, type: Optional[Type]) -> None:
if type:
analyzer = TypeAnalyserPass3(self.fail)
analyzer = TypeAnalyserPass3(self.fail, self.options, self.is_typeshed_file)
type.accept(analyzer)
self.check_for_omitted_generics(type)

def check_for_omitted_generics(self, typ: Type) -> None:
if 'generics' not in self.options.disallow_any or self.is_typeshed_file:
return

for t in collect_any_types(typ):
if t.from_omitted_generics:
self.fail(messages.BARE_GENERIC, t)

def fail(self, msg: str, ctx: Context, *, blocker: bool = False) -> None:
self.errors.report(ctx.get_line(), ctx.get_column(), msg)
Expand Down
85 changes: 67 additions & 18 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@
)

from mypy.nodes import (
TVAR, TYPE_ALIAS, UNBOUND_IMPORTED,
TypeInfo, Context, SymbolTableNode, Var, Expression,
IndexExpr, RefExpr, nongen_builtins, check_arg_names, check_arg_kinds,
ARG_POS, ARG_NAMED, ARG_OPT, ARG_NAMED_OPT, ARG_STAR, ARG_STAR2, TypeVarExpr
TVAR, TYPE_ALIAS, UNBOUND_IMPORTED, TypeInfo, Context, SymbolTableNode, Var, Expression,
IndexExpr, RefExpr, nongen_builtins, check_arg_names, check_arg_kinds, ARG_POS, ARG_NAMED,
ARG_OPT, ARG_NAMED_OPT, ARG_STAR, ARG_STAR2, TypeVarExpr
)
from mypy.tvar_scope import TypeVarScope
from mypy.sametypes import is_same_type
from mypy.exprtotype import expr_to_unanalyzed_type, TypeTranslationError
from mypy.subtypes import is_subtype
from mypy.plugin import Plugin, AnalyzerPluginInterface, AnalyzeTypeContext
from mypy import nodes
from mypy import experiments
from mypy import nodes, messages


T = TypeVar('T')
Expand Down Expand Up @@ -58,6 +56,8 @@ def analyze_type_alias(node: Expression,
tvar_scope: TypeVarScope,
fail_func: Callable[[str, Context], None],
plugin: Plugin,
options: Options,
is_typeshed_stub: bool,
allow_unnormalized: bool = False) -> Optional[Type]:
"""Return type if node is valid as a type alias rvalue.

Expand Down Expand Up @@ -100,8 +100,8 @@ def analyze_type_alias(node: Expression,
except TypeTranslationError:
fail_func('Invalid type alias', node)
return None
analyzer = TypeAnalyser(lookup_func, lookup_fqn_func, tvar_scope, fail_func, plugin,
aliasing=True, allow_unnormalized=allow_unnormalized)
analyzer = TypeAnalyser(lookup_func, lookup_fqn_func, tvar_scope, fail_func, plugin, options,
is_typeshed_stub, aliasing=True, allow_unnormalized=allow_unnormalized)
return type.accept(analyzer)


Expand All @@ -124,7 +124,9 @@ def __init__(self,
lookup_fqn_func: Callable[[str], SymbolTableNode],
tvar_scope: TypeVarScope,
fail_func: Callable[[str, Context], None],
plugin: Plugin, *,
plugin: Plugin,
options: Options,
is_typeshed_stub: bool, *,
aliasing: bool = False,
allow_tuple_literal: bool = False,
allow_unnormalized: bool = False) -> None:
Expand All @@ -138,6 +140,8 @@ def __init__(self,
self.nesting_level = 0
self.allow_unnormalized = allow_unnormalized
self.plugin = plugin
self.options = options
self.is_typeshed_stub = is_typeshed_stub

def visit_unbound_type(self, t: UnboundType) -> Type:
if t.optional:
Expand Down Expand Up @@ -172,7 +176,11 @@ def visit_unbound_type(self, t: UnboundType) -> Type:
elif fullname == 'typing.Tuple':
if len(t.args) == 0 and not t.empty_tuple_index:
# Bare 'Tuple' is same as 'tuple'
return self.named_type('builtins.tuple')
if 'generics' in self.options.disallow_any and not self.is_typeshed_stub:
self.fail(messages.BARE_GENERIC, t)
typ = self.named_type('builtins.tuple', line=t.line, column=t.column)
typ.from_generic_builtin = True
return typ
if len(t.args) == 2 and isinstance(t.args[1], EllipsisType):
# Tuple[T, ...] (uniform, variable-length tuple)
instance = self.named_type('builtins.tuple', [self.anal_type(t.args[0])])
Expand All @@ -192,7 +200,8 @@ def visit_unbound_type(self, t: UnboundType) -> Type:
return self.analyze_callable_type(t)
elif fullname == 'typing.Type':
if len(t.args) == 0:
return TypeType(AnyType(), line=t.line)
any_type = AnyType(from_omitted_generics=True, line=t.line, column=t.column)
return TypeType(any_type, line=t.line, column=t.column)
if len(t.args) != 1:
self.fail('Type[...] must have exactly one type argument', t)
item = self.anal_type(t.args[0])
Expand Down Expand Up @@ -221,7 +230,8 @@ def visit_unbound_type(self, t: UnboundType) -> Type:
act_len = len(an_args)
if exp_len > 0 and act_len == 0:
# Interpret bare Alias same as normal generic, i.e., Alias[Any, Any, ...]
return self.replace_alias_tvars(override, all_vars, [AnyType()] * exp_len,
any_type = AnyType(from_omitted_generics=True, line=t.line, column=t.column)
return self.replace_alias_tvars(override, all_vars, [any_type] * exp_len,
t.line, t.column)
if exp_len == 0 and act_len == 0:
return override
Expand Down Expand Up @@ -257,6 +267,7 @@ def visit_unbound_type(self, t: UnboundType) -> Type:
# valid count at this point. Thus we may construct an
# Instance with an invalid number of type arguments.
instance = Instance(info, self.anal_array(t.args), t.line, t.column)
instance.from_generic_builtin = sym.normalized
tup = info.tuple_type
if tup is not None:
# The class has a Tuple[...] base class so it will be
Expand Down Expand Up @@ -397,10 +408,11 @@ def analyze_callable_type(self, t: UnboundType) -> Type:
fallback = self.named_type('builtins.function')
if len(t.args) == 0:
# Callable (bare). Treat as Callable[..., Any].
ret = CallableType([AnyType(), AnyType()],
any_type = AnyType(from_omitted_generics=True, line=t.line, column=t.column)
ret = CallableType([any_type, any_type],
[nodes.ARG_STAR, nodes.ARG_STAR2],
[None, None],
ret_type=AnyType(),
ret_type=any_type,
fallback=fallback,
is_ellipsis_args=True)
elif len(t.args) == 2:
Expand Down Expand Up @@ -555,10 +567,14 @@ def anal_var_defs(self, var_defs: List[TypeVarDef]) -> List[TypeVarDef]:
vd.line))
return a

def named_type(self, fully_qualified_name: str, args: List[Type] = None) -> Instance:
def named_type(self, fully_qualified_name: str,
args: List[Type] = None,
line: int = -1,
column: int = -1) -> Instance:
node = self.lookup_fqn_func(fully_qualified_name)
assert isinstance(node.node, TypeInfo)
return Instance(node.node, args or [])
return Instance(node.node, args or [AnyType()] * len(node.node.defn.type_vars),
line=line, column=column)

def tuple_type(self, items: List[Type]) -> TupleType:
return TupleType(items, fallback=self.named_type('builtins.tuple', [AnyType()]))
Expand All @@ -584,16 +600,29 @@ class TypeAnalyserPass3(TypeVisitor[None]):
to types.
"""

def __init__(self, fail_func: Callable[[str, Context], None]) -> None:
def __init__(self,
fail_func: Callable[[str, Context], None],
options: Options,
is_typeshed_stub: bool) -> None:
self.fail = fail_func
self.options = options
self.is_typeshed_stub = is_typeshed_stub

def visit_instance(self, t: Instance) -> None:
info = t.type
# Check type argument count.
if len(t.args) != len(info.type_vars):
if len(t.args) == 0:
from_builtins = t.type.fullname() in nongen_builtins and not t.from_generic_builtin
if ('generics' in self.options.disallow_any and
not self.is_typeshed_stub and
from_builtins):
alternative = nongen_builtins[t.type.fullname()]
self.fail(messages.IMPLICIT_GENERIC_ANY_BUILTIN.format(alternative), t)
# Insert implicit 'Any' type arguments.
t.args = [AnyType()] * len(info.type_vars)
any_type = AnyType(from_omitted_generics=not from_builtins, line=t.line,
column=t.line)
t.args = [any_type] * len(info.type_vars)
return
# Invalid number of type parameters.
n = len(info.type_vars)
Expand Down Expand Up @@ -809,6 +838,26 @@ def visit_typeddict_type(self, t: TypedDictType) -> bool:
return False


def collect_any_types(t: Type) -> List[AnyType]:
"""Return all inner `AnyType`s of type t"""
return t.accept(CollectAnyTypesQuery())


class CollectAnyTypesQuery(TypeQuery[List[AnyType]]):
def __init__(self) -> None:
super().__init__(self.combine_lists_strategy)

def visit_any(self, t: AnyType) -> List[AnyType]:
return [t]

@classmethod
def combine_lists_strategy(cls, it: Iterable[List[AnyType]]) -> List[AnyType]:
result = [] # type: List[AnyType]
for l in it:
result.extend(l)
return result


def make_optional_type(t: Type) -> Type:
"""Return the type corresponding to Optional[t].

Expand Down
22 changes: 14 additions & 8 deletions mypy/types.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
"""Classes for representing mypy types."""

from abc import abstractmethod
import copy
from abc import abstractmethod
from collections import OrderedDict
from typing import (
Any, TypeVar, Dict, List, Tuple, cast, Generic, Set, Sequence, Optional, Union, Iterable,
NamedTuple, Callable,
Any, TypeVar, Dict, List, Tuple, cast, Generic, Set, Optional, Union, Iterable, NamedTuple,
Callable,
)

import mypy.nodes
from mypy import experiments
from mypy.nodes import (
INVARIANT, SymbolNode,
ARG_POS, ARG_OPT, ARG_STAR, ARG_STAR2, ARG_NAMED, ARG_NAMED_OPT,
INVARIANT, SymbolNode, ARG_POS, ARG_OPT, ARG_STAR, ARG_STAR2, ARG_NAMED, ARG_NAMED_OPT,
)
from mypy.sharedparse import argument_elide_name
from mypy.util import IdMapper
from mypy import experiments


T = TypeVar('T')

Expand Down Expand Up @@ -259,6 +257,7 @@ def __init__(self,
implicit: bool = False,
from_unimported_type: bool = False,
explicit: bool = False,
from_omitted_generics: bool = False,
line: int = -1,
column: int = -1) -> None:
super().__init__(line, column)
Expand All @@ -271,6 +270,8 @@ def __init__(self,
self.from_unimported_type = from_unimported_type
# Does this Any come from an explicit type annotation?
self.explicit = explicit
# Does this type come from omitted generics?
self.from_omitted_generics = from_omitted_generics

def accept(self, visitor: 'TypeVisitor[T]') -> T:
return visitor.visit_any(self)
Expand All @@ -279,15 +280,19 @@ def copy_modified(self,
implicit: bool = _dummy,
from_unimported_type: bool = _dummy,
explicit: bool = _dummy,
from_omitted_generics: bool = _dummy,
) -> 'AnyType':
if implicit is _dummy:
implicit = self.implicit
if from_unimported_type is _dummy:
from_unimported_type = self.from_unimported_type
if explicit is _dummy:
explicit = self.explicit
if from_omitted_generics is _dummy:
from_omitted_generics = self.from_omitted_generics
return AnyType(implicit=implicit, from_unimported_type=from_unimported_type,
explicit=explicit, line=self.line, column=self.column)
explicit=explicit, from_omitted_generics=from_omitted_generics,
line=self.line, column=self.column)

def serialize(self) -> JsonDict:
return {'.class': 'AnyType'}
Expand Down Expand Up @@ -408,6 +413,7 @@ class Instance(Type):
args = None # type: List[Type]
erased = False # True if result of type variable substitution
invalid = False # True if recovered after incorrect number of type arguments error
from_generic_builtin = False # True if created from a generic builtin (e.g. list() or set())

def __init__(self, typ: mypy.nodes.TypeInfo, args: List[Type],
line: int = -1, column: int = -1, erased: bool = False) -> None:
Expand Down
Loading