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

"incompatible default for argument" error message shows that types do match #4603

Closed
kamahen opened this issue Feb 20, 2018 · 1 comment
Closed

Comments

@kamahen
Copy link
Contributor

kamahen commented Feb 20, 2018

mypy --python-version=3.6 --strict-optional --check-untyped-defs --disallow-incomplete-defs --warn-incomplete-stub --warn-redundant-casts --warn-no-return --warn-return-any --no-incremental /tmp/xx.py 
/tmp/xx.py:24: error: Incompatible default for argument "_DISPATCH" (default has type "Dict[int, Callable[[Base, Ctx], AstNode]]", argument has type "Dict[int, Callable[[Base, Ctx], AstNode]]")

Note that the two types in the error message are identical.

When I did reveal_type(_DISPATCH), I got:

Revealed type is 'builtins.dict[builtins.int*, def (node: lib2to3.pytree.Base, ctx: xx.Ctx) -> xx.AstNode]'

Here's the source (I'm sure it can be reduced further):

from lib2to3 import pytree
from lib2to3.pgen2 import token
from typing import Callable, Dict, FrozenSet, List, Optional, Sequence, Text

class AstNode:
    pass


class Ctx:
    pass


def cvt_token_name(node: pytree.Base, ctx: Ctx) -> AstNode:
    """Handle token.NAME."""
    return AstNode()


_DISPATCH = {
    token.NAME: cvt_token_name,
}


def cvt(node: pytree.Base,
        ctx: Ctx,
        _DISPATCH: Dict[int, Callable[[pytree.Base, Ctx], AstNode]] = _DISPATCH
        ) -> AstNode:
    return _DISPATCH[node.type](node, ctx)

@JukkaL
Copy link
Collaborator

JukkaL commented Feb 21, 2018

Here is a simplified repro:

from typing import List, Callable

def f(a: str) -> None: pass
l = [f]

def g(x: List[Callable[[str], None]]) -> None: pass
g(l)

The reason for the complaint is that the callable in l has the argument name (a) specified but the argument x of g has only an argument type, no name. The error message is very confusing and should indicate that the missing callable argument name is the issue (#4530).

Potential workarounds:

  • Use a covariant container type in cvt / g, as suggested by mypy.
  • Add an explicit type annotation for _DISPATCH / l.
  • Include argument names in the function signature (using mypy_extensions.Arg).

Closing as duplicate of #4530.

@JukkaL JukkaL closed this as completed Feb 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants