You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Well, I couldn't find where Var.literal_hash is initialized to anything other than None, and returning None in the Var case passes all the tests. So I tried to replace it with something like the following:
I then ran mypy on itself, but it raised many errors. So I ran the test suite, and I got many failures; however, it seems that these failures are due to a more precise typechecking. For example, take testModifyLoopFor4 (simplified):
x=None# type: Union[int, str]x=1if1:
x='a'
The test does not say anything bad in this part, but mypy now complains that x is an int and therefore 'a' cannot be assigned into it. It might be too restrictive, but it is my understanding that this is the desired behavior, since "x=1" rebinds the variable into int (it's valid since int is part of the Union) and then it tries to rebind it to str, but now it is not possible.
I am not 100% sure, since I am not 100% sure how the code works and what is the expected behavior, but it looks like a bug - and if so, there are ~40 "type errors" to fix in the source code (although I suspect none of them is an actual error).
So what is the expected behavior? When and how should types rebind?
mypy/lex.py: note: In member "lex" of class "Lexer":
mypy/lex.py:361: error: Incompatible types in assignment (expression has type "str", variable has type "bytes")
mypy/lex.py:363: error: Argument 1 to "Bom" has incompatible type "int"; expected "str"
mypy/lex.py:364: error: Incompatible types in assignment (expression has type "Union[bytes, str]", variable has type "str")
mypy/lex.py:381: error: Argument 1 to "get" of "dict" has incompatible type "Union[int, str]"; expected "str"
mypy/test/data.py: note: In function "parse_test_data":
mypy/test/data.py:276: error: Incompatible types in assignment (expression has type "str", variable has type None)
mypy/types.py: note: In member "__init__" of class "TypeVarDef":
mypy/types.py:125: error: Incompatible types in assignment (expression has type "TypeVarId", variable has type "int")
mypy/types.py:126: error: Incompatible types in assignment (expression has type "Union[int, TypeVarId]", variable has type "TypeVarId")
mypy/types.py: note: In member "type_object" of class "CallableType":
mypy/types.py:635: error: Incompatible types in assignment (expression has type "Instance", variable has type "TupleType")
mypy/join.py: note: In function "join_simple":
mypy/join.py:41: error: Incompatible types in assignment (expression has type "Type", variable has type "NoneTyp")
mypy/join.py:44: error: Incompatible types in assignment (expression has type "Type", variable has type "UninhabitedType")
mypy/join.py: note: In function "join_types":
mypy/join.py:80: error: Incompatible types in assignment (expression has type "Type", variable has type "UnionType")
mypy/join.py:83: error: Incompatible types in assignment (expression has type "Type", variable has type "NoneTyp")
mypy/meet.py: note: In function "meet_types":
mypy/meet.py:24: error: Incompatible types in assignment (expression has type "Type", variable has type "UnionType")
mypy/meet.py: note: In function "is_overlapping_types":
mypy/meet.py:75: error: Incompatible types in assignment (expression has type "Type", variable has type "TypeVarType")
mypy/meet.py:77: error: Incompatible types in assignment (expression has type "Type", variable has type "TypeVarType")
mypy/binder.py: note: In member "get" of class "ConditionalTypeBinder":
mypy/binder.py:112: error: Incompatible types in assignment (expression has type "Expression", variable has type "NameExpr") # that's an error in the fix...
mypy/checkmember.py: note: In function "analyze_member_access":
mypy/checkmember.py:109: error: Incompatible types in assignment (expression has type "Instance", variable has type "TupleType")
mypy/checkmember.py: note: In function "check_method_type":
mypy/checkmember.py:300: error: Incompatible types in assignment (expression has type "Instance", variable has type "TupleType")
mypy/checkmember.py: note: In function "map_type_from_supertype":
mypy/checkmember.py:487: error: Incompatible types in assignment (expression has type "Instance", variable has type "TupleType")
mypy/checkmember.py:492: error: Argument 1 to "map_instance_to_supertype" has incompatible type "Union[TupleType, Instance]"; expected "Instance"
mypy/checkexpr.py: note: In function "extract_refexpr_names":
mypy/checkexpr.py:73: error: Incompatible types in assignment (expression has type "RefExpr", variable has type "MemberExpr")
mypy/checkexpr.py: note: In member "analyze_ref_expr" of class "ExpressionChecker":
mypy/checkexpr.py:126: error: Incompatible types in assignment (expression has type "NoneTyp", variable has type "PartialType")
mypy/checkexpr.py:132: error: Incompatible types in assignment (expression has type "AnyType", variable has type "PartialType")
mypy/checkexpr.py: note: In member "infer_function_type_arguments_using_context" of class "ExpressionChecker":
mypy/checkexpr.py:461: error: Incompatible types in assignment (expression has type "NoneTyp", variable has type "TypeVarType")
mypy/checkexpr.py: note: In member "visit_tuple_expr" of class "ExpressionChecker":
mypy/checkexpr.py:1418: error: Incompatible types in assignment (expression has type "Type", variable has type "UnionType")
mypy/checkexpr.py: note: In member "infer_lambda_type_using_context" of class "ExpressionChecker":
mypy/checkexpr.py:1564: error: Incompatible types in assignment (expression has type "Type", variable has type "CallableType")
mypy/checkexpr.py: note: In function "overload_arg_similarity":
mypy/checkexpr.py:1961: error: Incompatible types in assignment (expression has type "Type", variable has type "TypeVarType")
mypy/checkexpr.py:1963: error: Incompatible types in assignment (expression has type "Type", variable has type "TypeVarType")
mypy/checkexpr.py:1996: error: Incompatible types in assignment (expression has type "Instance", variable has type "CallableType")
mypy/checkexpr.py:1998: error: Incompatible types in assignment (expression has type "Instance", variable has type "Overloaded")
mypy/checkexpr.py:2000: error: Incompatible types in assignment (expression has type "Instance", variable has type "TupleType")
mypy/checker.py: note: In member "check_assignment" of class "TypeChecker":
mypy/checker.py:1066: error: Incompatible types in assignment (expression has type "Type", variable has type "PartialType")
mypy/checker.py: note: In member "check_multi_assignment" of class "TypeChecker":
mypy/checker.py:1155: error: Incompatible types in assignment (expression has type "Expression", variable has type "StarExpr")
mypy/checker.py: note: In member "visit_decorator" of class "TypeChecker":
mypy/checker.py:1831: error: Incompatible types in assignment (expression has type "Type", variable has type "FunctionLike")
mypy/checker.py: note: In member "is_async_def" of class "TypeChecker":
mypy/checker.py:1982: error: Incompatible types in assignment (expression has type "Type", variable has type "Instance")
mypy/checker.py: note: In function "get_isinstance_type":
mypy/checker.py:2508: error: Incompatible types in assignment (expression has type "Type", variable has type "FunctionLike")
The type of the variable x itself never changes. (What is known about) the type of the value of x can be different at different points in the program, since the value of x can change. But the variable always has type Union[int, str], so it's always legal to assign a str to it.
The Var case of get shouldn't exist. The only caller is in analyze_var_ref but that is dead code. The type of a variable reference is actually refined from the binder elsewhere, in narrow_type_from_binder.
Activity
elazarg commentedon Oct 6, 2016
Error messages about the source:
rwbarton commentedon Oct 6, 2016
The type of the variable
xitself never changes. (What is known about) the type of the value ofxcan be different at different points in the program, since the value ofxcan change. But the variable always has typeUnion[int, str], so it's always legal to assign astrto it.elazarg commentedon Oct 6, 2016
So, what should happen in the code?
rwbarton commentedon Oct 6, 2016
The
Varcase ofgetshouldn't exist. The only caller is inanalyze_var_refbut that is dead code. The type of a variable reference is actually refined from the binder elsewhere, innarrow_type_from_binder.elazarg commentedon Oct 6, 2016
Ok that makes sense
gvanrossum commentedon Oct 13, 2016
Is there an action item here? Can you update the topic to reflect that? Else please close.
elazarg commentedon Oct 13, 2016
There is, and it is fixed in #2238.
gvanrossum commentedon Oct 20, 2016
If you sent a separate PR that just fixed that it would be quicker to review.
rwbarton commentedon Oct 20, 2016
Plus it boosts your commit count :)
elazarg commentedon Oct 25, 2016
It just happens to be required for the PR I actually wanted.