8000 Type error in mypy? · Issue #2222 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Type error in mypy? #2222

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

Closed
elazarg opened this issue Oct 6, 2016 · 10 comments
Closed

Type error in mypy? #2222

elazarg opened this issue Oct 6, 2016 · 10 comments

Comments

@elazarg
Copy link
Contributor
elazarg commented Oct 6, 2016

During #2221 (which I have little hope to be actually accepted) I ran into this function in binder.py:

    def get(self, expr: Union[Expression, Var]) -> Type:
        return self._get(expr.literal_hash)

The Var case comes from analyze_var_ref() in checkexpr.py.

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:

    def get(self, expr: Union[Expression, Var]) -> Type:
        if isinstance(expr, Var):
            new_expr = NameExpr(expr.name())
        else:
            new_expr = expr
        return self._get(new_expr.literal_hash)

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 = 1

if 1:
    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?

@elazarg
Copy link
Contributor Author
elazarg commented Oct 6, 2016

Error messages about the source:

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.
8000
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")

@rwbarton
Copy link
Contributor
rwbarton commented Oct 6, 2016

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.

@elazarg
Copy link
Contributor Author
elazarg commented Oct 6, 2016

So, what should happen in the code?

@rwbarton
Copy link
Contributor
rwbarton commented Oct 6, 2016

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.

@elazarg
Copy link
Contributor Author
elazarg commented Oct 6, 2016

Ok that makes sense

@gvanrossum
Copy link
Member

Is there an action item here? Can you update the topic to reflect that? Else please close.

@elazarg
Copy link
Contributor Author
elazarg commented Oct 13, 2016

There is, and it is fixed in #2238.

@gvanrossum
Copy link
Member

If you sent a separate PR that just fixed that it would be quicker to review.

@gvanrossum gvanrossum added this to the 0.5 milestone Oct 20, 2016
@rwbarton
Copy link
Contributor

Plus it boosts your commit count :)

@elazarg
Copy link
Contributor Author
elazarg commented Oct 25, 2016

It just happens to be required for the PR I actually wanted.

@gvanrossum gvanrossum removed this from the 0.5 milestone Mar 29, 2017
@elazarg elazarg closed this as completed Apr 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants
0