Skip to content
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

Optionally allow redefinition of variable with different type #6197

Merged
merged 56 commits into from
Jan 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
8e49664
Support redefinition of local variables with a different type
JukkaL Sep 20, 2018
3b409fc
Add docstring
JukkaL Sep 20, 2018
71cb8ea
Allow redefinitions of form "x = f(x)"
JukkaL Sep 20, 2018
35f56ae
Fix special cases
JukkaL Sep 20, 2018
2524777
Add test cases
JukkaL Sep 20, 2018
ab70d0d
Fix redefinition with 'break' and 'continue'
JukkaL Sep 20, 2018
525d5f4
Fix issues
JukkaL Sep 20, 2018
2c614a2
Fix some edge cases
JukkaL Sep 20, 2018
bf2f869
Support top-level redefinition through renaming (WIP)
JukkaL Sep 28, 2018
9e2c6b8
Remove obsolete code
JukkaL Sep 28, 2018
2b885da
Cleanup
JukkaL Sep 28, 2018
712669c
Merge transform code into a single class
JukkaL Sep 28, 2018
e0b7df0
Code cleanup
JukkaL Sep 28, 2018
f153809
Fix issues
JukkaL Sep 28, 2018
856d401
Fix issues
JukkaL Sep 28, 2018
865f1dd
Work around type check error
JukkaL Sep 28, 2018
2deff14
Merge branch 'master' into redefine-var
JukkaL Oct 4, 2018
27a1e57
Fix merge issue
JukkaL Oct 4, 2018
f04e37a
Fix lint
JukkaL Oct 4, 2018
1f9f62e
Fix more tests
JukkaL Oct 4, 2018
9ba82a9
Rename semanal_redef -> renaming
JukkaL Oct 4, 2018
fbf052e
Only perform redefinition if variable has been read
JukkaL Oct 9, 2018
1116b47
A few fixes
JukkaL Oct 9, 2018
70a3085
Add hacky workaround for 'self'
JukkaL Oct 9, 2018
cfad560
Merge branch 'master' into redefine-var
JukkaL Oct 9, 2018
b57960b
Remove unused things
JukkaL Oct 10, 2018
580a774
Revert some unnecessary changes
JukkaL Oct 10, 2018
051e695
Merge commit '3bea26f89a66834f42caaa90cf8db44f99d39dcc' into redefine…
JukkaL Dec 9, 2018
77495fb
Merge branch 'master' into redefine-var
JukkaL Dec 9, 2018
dc03143
Fix test case
JukkaL Dec 9, 2018
4ceed87
Merge branch 'master' into redefine-var
JukkaL Dec 19, 2018
ac00e6d
Merge branch 'master' into redefine-var
JukkaL Dec 20, 2018
20fd0f9
Use `if int():` consistently
JukkaL Dec 20, 2018
3843cea
Fix some test cases
JukkaL Dec 20, 2018
5cfaf9c
Add flag for allowing redefinitions
JukkaL Dec 20, 2018
83ba85f
Use the flag
JukkaL Dec 20, 2018
43a0362
Fix tests
JukkaL Dec 20, 2018
59b3413
Fix test cases
JukkaL Dec 20, 2018
7e3eff9
Add test case for turning flag off explicitly
JukkaL Dec 20, 2018
afa1096
Rename flag to --[dis]allow-redefinition (no plural)
JukkaL Dec 20, 2018
916288c
Fix semantic analyzer tests
JukkaL Dec 21, 2018
459187e
Update test case
JukkaL Dec 21, 2018
e165e46
Merge branch 'master' into redefine-var
JukkaL Jan 15, 2019
0a119b3
Merge branch 'master' into redefine-var
JukkaL Jan 15, 2019
fadb322
Merge branch 'master' into redefine-var
JukkaL Jan 17, 2019
76ad071
Fix brokenness from merge
JukkaL Jan 17, 2019
979a17c
Respond to review
JukkaL Jan 18, 2019
ae0043f
Clarify code
JukkaL Jan 18, 2019
512c1ed
Test additional special case
JukkaL Jan 18, 2019
1f7e36d
Add test case
JukkaL Jan 18, 2019
72043ae
Fix with statements
JukkaL Jan 18, 2019
8c30c90
Remove unnecessary check
JukkaL Jan 18, 2019
e390f38
Add comment
JukkaL Jan 18, 2019
bc0ee19
Ensure renaming cannot happen across scopes in class
JukkaL Jan 18, 2019
54b86d8
Remove unnecessary call
JukkaL Jan 18, 2019
496d2dd
The final assignments takes precedence in class when renaming
JukkaL Jan 18, 2019
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
8 changes: 2 additions & 6 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
)
from mypy.nodes import (
TypeInfo, FuncBase, Var, FuncDef, SymbolNode, Context, MypyFile, TypeVarExpr,
ARG_POS, ARG_STAR, ARG_STAR2, Decorator, OverloadedFuncDef, TypeAlias, TempNode
ARG_POS, ARG_STAR, ARG_STAR2, Decorator, OverloadedFuncDef, TypeAlias, TempNode,
is_final_node
)
from mypy.messages import MessageBuilder
from mypy.maptype import map_instance_to_supertype
Expand Down Expand Up @@ -895,8 +896,3 @@ def erase_to_bound(t: Type) -> Type:
if isinstance(t.item, TypeVarType):
return TypeType.make_normalized(t.item.upper_bound)
return t


def is_final_node(node: Optional[SymbolNode]) -> bool:
"""Check whether `node` corresponds to a final attribute."""
return isinstance(node, (Var, FuncDef, OverloadedFuncDef, Decorator)) and node.is_final
4 changes: 4 additions & 0 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,10 @@ def add_invertible_flag(flag: str,
help="Suppress toplevel errors caused by missing annotations",
group=strictness_group)

add_invertible_flag('--allow-redefinition', default=False, strict_flag=False,
help="Allow unconditional variable redefinition with a new type",
group=strictness_group)

incremental_group = parser.add_argument_group(
title='Incremental mode',
description="Adjust how mypy incrementally type checks and caches modules. "
Expand Down
5 changes: 3 additions & 2 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
ReturnStmt, NameExpr, Var, CONTRAVARIANT, COVARIANT, SymbolNode,
CallExpr
)
from mypy.util import unmangle
from mypy import message_registry

MYPY = False
Expand Down Expand Up @@ -952,7 +953,7 @@ def cant_assign_to_final(self, name: str, attr_assign: bool, ctx: Context) -> No
Pass `attr_assign=True` if the assignment assigns to an attribute.
"""
kind = "attribute" if attr_assign else "name"
self.fail('Cannot assign to final {} "{}"'.format(kind, name), ctx)
self.fail('Cannot assign to final {} "{}"'.format(kind, unmangle(name)), ctx)

def protocol_members_cant_be_final(self, ctx: Context) -> None:
self.fail("Protocol member cannot be final", ctx)
Expand Down Expand Up @@ -1064,7 +1065,7 @@ def unimported_type_becomes_any(self, prefix: str, typ: Type, ctx: Context) -> N
ctx)

def need_annotation_for_var(self, node: SymbolNode, context: Context) -> None:
self.fail("Need type annotation for '{}'".format(node.name()), context)
self.fail("Need type annotation for '{}'".format(unmangle(node.name())), context)

def explicit_any(self, ctx: Context) -> None:
self.fail('Explicit "Any" is not allowed', ctx)
Expand Down
14 changes: 11 additions & 3 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -924,15 +924,18 @@ def accept(self, visitor: StatementVisitor[T]) -> T:


class AssignmentStmt(Statement):
"""Assignment statement
"""Assignment statement.

The same node class is used for single assignment, multiple assignment
(e.g. x, y = z) and chained assignment (e.g. x = y = z), assignments
that define new names, and assignments with explicit types (# type).
that define new names, and assignments with explicit types ("# type: t"
or "x: t [= ...]").

An lvalue can be NameExpr, TupleExpr, ListExpr, MemberExpr, IndexExpr.
An lvalue can be NameExpr, TupleExpr, ListExpr, MemberExpr, or IndexExpr.
"""

lvalues = None # type: List[Lvalue]
# This is a TempNode if and only if no rvalue (x: t).
rvalue = None # type: Expression
# Declared type in a comment, may be None.
type = None # type: Optional[mypy.types.Type]
Expand Down Expand Up @@ -2968,3 +2971,8 @@ def is_class_var(expr: NameExpr) -> bool:
if isinstance(expr.node, Var):
return expr.node.is_classvar
return False


def is_final_node(node: Optional[SymbolNode]) -> bool:
"""Check whether `node` corresponds to a final attribute."""
return isinstance(node, (Var, FuncDef, OverloadedFuncDef, Decorator)) and node.is_final
5 changes: 5 additions & 0 deletions mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class BuildType:
PER_MODULE_OPTIONS = {
# Please keep this list sorted
"allow_untyped_globals",
"allow_redefinition",
"always_false",
"always_true",
"check_untyped_defs",
Expand Down Expand Up @@ -149,6 +150,10 @@ def __init__(self) -> None:
# Suppress toplevel errors caused by missing annotations
self.allow_untyped_globals = False

# Allow variable to be redefined with an arbitrary type in the same block
# and the same nesting level as the initialization
self.allow_redefinition = False

# Variable names considered True
self.always_true = [] # type: List[str]

Expand Down
7 changes: 5 additions & 2 deletions mypy/plugins/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Overloaded, UnionType, FunctionLike
)
from mypy.typevars import fill_typevars
from mypy.util import unmangle
from mypy.server.trigger import make_wildcard_trigger

MYPY = False
Expand Down Expand Up @@ -376,9 +377,10 @@ def _attribute_from_auto_attrib(ctx: 'mypy.plugin.ClassDefContext',
rvalue: Expression,
stmt: AssignmentStmt) -> Attribute:
"""Return an Attribute for a new type assignment."""
name = unmangle(lhs.name)
# `x: int` (without equal sign) assigns rvalue to TempNode(AnyType())
has_rhs = not isinstance(rvalue, TempNode)
return Attribute(lhs.name, ctx.cls.info, has_rhs, True, kw_only, Converter(), stmt)
return Attribute(name, ctx.cls.info, has_rhs, True, kw_only, Converter(), stmt)


def _attribute_from_attrib_maker(ctx: 'mypy.plugin.ClassDefContext',
Expand Down Expand Up @@ -443,7 +445,8 @@ def _attribute_from_attrib_maker(ctx: 'mypy.plugin.ClassDefContext',
converter = convert
converter_info = _parse_converter(ctx, converter)

return Attribute(lhs.name, ctx.cls.info, attr_has_default, init, kw_only, converter_info, stmt)
name = unmangle(lhs.name)
return Attribute(name, ctx.cls.info, attr_has_default, init, kw_only, converter_info, stmt)


def _parse_converter(ctx: 'mypy.plugin.ClassDefContext',
Expand Down
Loading