Skip to content

Commit

Permalink
typeanal: error for unbound ParamSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja committed Sep 6, 2020
1 parent 5535d13 commit b3774c2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
8 changes: 6 additions & 2 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
CallableType, NoneType, ErasedType, DeletedType, TypeList, TypeVarDef, SyntheticTypeVisitor,
StarType, PartialType, EllipsisType, UninhabitedType, TypeType,
CallableArgument, TypeQuery, union_items, TypeOfAny, LiteralType, RawExpressionType,
PlaceholderType, Overloaded, get_proper_type, TypeAliasType, TypeVarLikeDef
PlaceholderType, Overloaded, get_proper_type, TypeAliasType, TypeVarLikeDef, ParamSpecDef
)

from mypy.nodes import (
TypeInfo, Context, SymbolTableNode, Var, Expression,
nongen_builtins, check_arg_names, check_arg_kinds, ARG_POS, ARG_NAMED,
ARG_OPT, ARG_NAMED_OPT, ARG_STAR, ARG_STAR2, TypeVarExpr, TypeVarLikeExpr,
ARG_OPT, ARG_NAMED_OPT, ARG_STAR, ARG_STAR2, TypeVarExpr, TypeVarLikeExpr, ParamSpecExpr,
TypeAlias, PlaceholderNode, SYMBOL_FUNCBASE_TYPES, Decorator, MypyFile
)
from mypy.typetraverser import TypeTraverserVisitor
Expand Down Expand Up @@ -200,6 +200,10 @@ def visit_unbound_type_nonoptional(self, t: UnboundType, defining_literal: bool)
self.fail(no_subscript_builtin_alias(fullname,
propose_alt=not self.defining_alias), t)
tvar_def = self.tvar_scope.get_binding(sym)
if isinstance(sym.node, ParamSpecExpr):
if tvar_def is None:
self.fail('ParamSpec "{}" is unbound'.format(t.name), t)
return AnyType(TypeOfAny.from_error)
if isinstance(sym.node, TypeVarExpr) and tvar_def is not None and self.defining_alias:
self.fail('Can\'t use bound type variable "{}"'
' to define generic alias'.format(t.name), t)
Expand Down
1 change: 1 addition & 0 deletions test-data/unit/check-parameter-specification.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ from typing import Callable, List
from typing_extensions import ParamSpec, Concatenate
P = ParamSpec('P')

x: P # E: ParamSpec "P" is unbound
def foo1(x: Callable[P, int]) -> Callable[P, str]: ...
def foo2(x: P) -> P: ... # E: Invalid location for ParamSpec "P"
# TODO(shantanu): uncomment once we have support for Concatenate
Expand Down

0 comments on commit b3774c2

Please sign in to comment.