Skip to content

Support for functions producing generic functions #3113

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 40 commits into from
Apr 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
679a172
Make it an error to use a class-attribute type var outside a type
sixolet Apr 1, 2017
74e9262
Fix the error message to handle more cases
sixolet Apr 1, 2017
5470c77
oops accidentally changed a file
sixolet Apr 1, 2017
12def8b
Typo fix
sixolet Apr 1, 2017
52782fe
more checking things in tests
sixolet Apr 1, 2017
0184595
Change error message
sixolet Apr 1, 2017
757a6b8
Change error message
sixolet Apr 1, 2017
57bca93
Make TypeQuery more general, handling nonboolean queries.
sixolet Mar 29, 2017
3b8bc23
Remove redundant empty check
sixolet Mar 29, 2017
82da335
Some types in the visitor were missing recursion
sixolet Mar 30, 2017
9f12644
Add ellipsis type handler to TypeQuery
sixolet Mar 30, 2017
8cf4fa6
Typechecks, but gives spurious error. Need to move variable binding?
sixolet Mar 30, 2017
2634277
Small change: Move tvar id generation nearer type analysis
sixolet Mar 30, 2017
30dba10
Even nearer
sixolet Mar 30, 2017
7b4f9f2
Replace the concept of BOUND_TVAR and UNBOUND_TVAR as mutative flags …
sixolet Mar 30, 2017
daf9592
Mid debugging gotta push now
sixolet Mar 30, 2017
826b9cf
Checkpoint. Most tests pass, classvar type aliases not yet
sixolet Mar 31, 2017
4141f94
forgot this file
sixolet Mar 31, 2017
58e3a2a
Fix aliasing to not bind type vars within a callable while aliasing
sixolet Mar 31, 2017
c061a09
removed some now-unused code
sixolet Apr 1, 2017
603013d
my own code review, first pass
sixolet Apr 1, 2017
c6fec63
Use type var query instead of another query fn
sixolet Apr 2, 2017
f46d52a
Tighten code
sixolet Apr 2, 2017
ea21337
fix some types
sixolet Apr 2, 2017
80d62a7
Use same type alias throughout
sixolet Apr 2, 2017
eaf8c0d
Make semanal tests pass. Delete one I think is wrong
sixolet Apr 2, 2017
5009a2b
Generator types for generators
sixolet Apr 2, 2017
7c64464
Lint and cleanups
sixolet Apr 3, 2017
a866d0f
Test for using generic function return as a decorator
sixolet Apr 11, 2017
9d5c630
Merge upstream
sixolet Apr 12, 2017
4683d70
Merge lint
sixolet Apr 12, 2017
cbb3cb0
Merged master; adapted code to use better TypeQuery
sixolet Apr 12, 2017
da0d936
More tests for nested and multi-type-var situations
sixolet Apr 12, 2017
edbecb0
Fixed type variable scoping rules to conform to PEP484 better
sixolet Apr 14, 2017
21d8b13
Move the typevars that are prohibited due to being bound by the class…
sixolet Apr 17, 2017
214aebc
More doc
sixolet Apr 17, 2017
5405646
Jukka comments
sixolet Apr 20, 2017
a25e926
one more
sixolet Apr 20, 2017
4aaab6c
get imports right
sixolet Apr 20, 2017
fd153ad
Merge branch 'master' into second-order-decorator
sixolet Apr 20, 2017
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
5 changes: 2 additions & 3 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
DictionaryComprehension, ComplexExpr, EllipsisExpr, StarExpr, AwaitExpr, YieldExpr,
YieldFromExpr, TypedDictExpr, PromoteExpr, NewTypeExpr, NamedTupleExpr, TypeVarExpr,
TypeAliasExpr, BackquoteExpr, EnumCallExpr,
ARG_POS, ARG_NAMED, ARG_STAR, ARG_STAR2, MODULE_REF,
UNBOUND_TVAR, BOUND_TVAR, LITERAL_TYPE
ARG_POS, ARG_NAMED, ARG_STAR, ARG_STAR2, MODULE_REF, TVAR, LITERAL_TYPE,
)
from mypy import nodes
import mypy.checker
Expand Down Expand Up @@ -1623,7 +1622,7 @@ def replace_tvars_any(self, tp: Type) -> Type:
sym = self.chk.lookup_qualified(arg.name)
except KeyError:
pass
if sym and (sym.kind == UNBOUND_TVAR or sym.kind == BOUND_TVAR):
if sym and (sym.kind == TVAR):
new_args[i] = AnyType()
else:
new_args[i] = self.replace_tvars_any(arg)
Expand Down
22 changes: 6 additions & 16 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ def get_column(self) -> int: pass
GDEF = 1 # type: int
MDEF = 2 # type: int
MODULE_REF = 3 # type: int
# Type variable declared using TypeVar(...) has kind UNBOUND_TVAR. It's not
# valid as a type. A type variable is valid as a type (kind BOUND_TVAR) within
# Type variable declared using TypeVar(...) has kind TVAR. It's not
# valid as a type unless bound in a TypeVarScope. That happens within:
# (1) a generic class that uses the type variable as a type argument or
# (2) a generic function that refers to the type variable in its signature.
UNBOUND_TVAR = 4 # type: int
BOUND_TVAR = 5 # type: int
TVAR = 4 # type: int

TYPE_ALIAS = 6 # type: int
# Placeholder for a name imported via 'from ... import'. Second phase of
# semantic will replace this the actual imported reference. This is
Expand All @@ -65,8 +65,7 @@ def get_column(self) -> int: pass
GDEF: 'Gdef',
MDEF: 'Mdef',
MODULE_REF: 'ModuleRef',
UNBOUND_TVAR: 'UnboundTvar',
BOUND_TVAR: 'Tvar',
TVAR: 'Tvar',
TYPE_ALIAS: 'TypeAlias',
UNBOUND_IMPORTED: 'UnboundImported',
}
Expand Down Expand Up @@ -2211,17 +2210,14 @@ class SymbolTableNode:
# - LDEF: local definition (of any kind)
# - GDEF: global (module-level) definition
# - MDEF: class member definition
# - UNBOUND_TVAR: TypeVar(...) definition, not bound
# - TVAR: type variable in a bound scope (generic function / generic clas)
# - TVAR: TypeVar(...) definition
# - MODULE_REF: reference to a module
# - TYPE_ALIAS: type alias
# - UNBOUND_IMPORTED: temporary kind for imported names
kind = None # type: int
# AST node of definition (FuncDef/Var/TypeInfo/Decorator/TypeVarExpr,
# or None for a bound type variable).
node = None # type: Optional[SymbolNode]
# Type variable definition (for bound type variables only)
tvar_def = None # type: Optional[mypy.types.TypeVarDef]
# Module id (e.g. "foo.bar") or None
mod_id = ''
# If this not None, override the type of the 'node' attribute.
Expand All @@ -2237,13 +2233,11 @@ class SymbolTableNode:

def __init__(self, kind: int, node: Optional[SymbolNode], mod_id: str = None,
typ: 'mypy.types.Type' = None,
tvar_def: 'mypy.types.TypeVarDef' = None,
module_public: bool = True, normalized: bool = False) -> None:
self.kind = kind
self.node = node
self.type_override = typ
self.mod_id = mod_id
self.tvar_def = tvar_def
self.module_public = module_public
self.normalized = normalized

Expand Down Expand Up @@ -2287,8 +2281,6 @@ def serialize(self, prefix: str, name: str) -> JsonDict:
data = {'.class': 'SymbolTableNode',
'kind': node_kinds[self.kind],
} # type: JsonDict
if self.tvar_def:
data['tvar_def'] = self.tvar_def.serialize()
if not self.module_public:
data['module_public'] = False
if self.kind == MODULE_REF:
Expand Down Expand Up @@ -2323,8 +2315,6 @@ def deserialize(cls, data: JsonDict) -> 'SymbolTableNode':
if 'type_override' in data:
typ = mypy.types.deserialize_type(data['type_override'])
stnode = SymbolTableNode(kind, node, typ=typ)
if 'tvar_def' in data:
stnode.tvar_def = mypy.types.TypeVarDef.deserialize(data['tvar_def'])
if 'module_public' in data:
stnode.module_public = data['module_public']
return stnode
Expand Down
Loading