Skip to content

Type error in mypy?  #2222

Closed
Closed

Description

@elazarg

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?

Activity

  1. elazarg commented on Oct 6, 2016

    @elazarg
    ContributorAuthor

    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.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")
    
  2. rwbarton commented on Oct 6, 2016

    @rwbarton
    Contributor

    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.

  3. elazarg commented on Oct 6, 2016

    @elazarg
    ContributorAuthor

    So, what should happen in the code?

  4. rwbarton commented on Oct 6, 2016

    @rwbarton
    Contributor

    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.

  5. elazarg commented on Oct 6, 2016

    @elazarg
    ContributorAuthor

    Ok that makes sense

  6. gvanrossum commented on Oct 13, 2016

    @gvanrossum
    Member

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

  7. elazarg commented on Oct 13, 2016

    @elazarg
    ContributorAuthor

    There is, and it is fixed in #2238.

  8. gvanrossum commented on Oct 20, 2016

    @gvanrossum
    Member

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

  9. added this to the milestone on Oct 20, 2016
  10. rwbarton commented on Oct 20, 2016

    @rwbarton
    Contributor

    Plus it boosts your commit count :)

  11. elazarg commented on Oct 25, 2016

    @elazarg
    ContributorAuthor

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

  12. removed this from the milestone on Mar 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions