Skip to content

Mypy fails on some type hints #8891

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
klnrdknt opened this issue May 26, 2020 · 6 comments
Closed

Mypy fails on some type hints #8891

klnrdknt opened this issue May 26, 2020 · 6 comments
Labels

Comments

@klnrdknt
Copy link

I do not know why or how, but, e.g. this piece of code is completely fine:

    def __init__(self, servoNumber: int):
        self._servoNumber = servoNumber

I is kind of the top level class that uses the next one I'm gonna show, where mypy seems to fail, which is:

def getLockButtonLabel(servoNumber: int):
    """Return the lock button label on UI start-up.

    Parameters
    ----------
    servoNumber : :obj:`int`
        Servo index :obj:`servo.channel`

    Returns
    -------
    :obj:`String`
        String description depending on lock state.
    """
->    servo = DEVICE.servo(servoNumber)

Mypy says it fails on the line where i pointed the arrow. The initial error is:

poetry run mypy src
somedirectory/_callbacks.py:132: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.rtfd.io/en/latest/common_issues.html#using-a-development-mypy-build
If this issue continues with mypy master, please report a bug at https://github.com/python/mypy/issues
version: 0.770
somedirectory/_callbacks.py:132: : note: please use --show-traceback to print a traceback when reporting a bug
make[1]: *** [Makefile:29: mypy] Error 2

And if I show the traceback

Traceback (most recent call last):
  File "mypy/checkexpr.py", line 3749, in accept
  File "mypy/nodes.py", line 1496, in accept
  File "mypy/checkexpr.py", line 1985, in visit_member_expr
  File "mypy/checkexpr.py", line 2003, in analyze_ordinary_member_access
  File "mypy/checkmember.py", line 126, in analyze_member_access
  File "mypy/checkmember.py", line 143, in _analyze_member_access
  File "mypy/checkmember.py", line 219, in analyze_instance_member_access
  File "mypy/maptype.py", line 20, in map_instance_to_supertype
AttributeError: attribute 'type_vars' of 'TypeInfo' undefined

Either I misunderstand the use of type hinting completely (might actually be the case) or it is a bug and very frustrating. :/ Either way I would really appreciate help. 😢

@JelleZijlstra
Copy link
Member

There's nothing obviously wrong in the code you posted. Maybe another team member is able to figure out from the stack trace what the issue is. If you can minimize this to a self-contained example, that could also help.

@emmatyping
Copy link
Member

Could you try an install of mypy from git? https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build

@cddf
Copy link

cddf commented May 27, 2020

I tested it with mypy from master and it seems to crash at another place:

traceback
version: 0.770+dev.f94fc7e251330f7e3668aea1f062cdbae1ed7232
Traceback (most recent call last):
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/bin/mypy", line 10, in <module>
    sys.exit(console_entry())
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/__main__.py", line 8, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/main.py", line 89, in main
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/build.py", line 180, in build
    result = _build(
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/build.py", line 252, in _build
    graph = dispatch(sources, manager, stdout)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/build.py", line 2626, in dispatch
    process_graph(graph, manager)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/build.py", line 2949, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/build.py", line 3047, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/build.py", line 2107, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/checker.py", line 294, in check_first_pass
    self.accept(d)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/checker.py", line 401, in accept
    stmt.accept(self)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/nodes.py", line 676, in accept
    return visitor.visit_func_def(self)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/checker.py", line 726, in visit_func_def
    self._visit_func_def(defn)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/checker.py", line 730, in _visit_func_def
    self.check_func_item(defn, name=defn.name)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/checker.py", line 792, in check_func_item
    self.check_func_def(defn, typ, name)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/checker.py", line 975, in check_func_def
    self.accept(item.body)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/checker.py", line 401, in accept
    stmt.accept(self)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/nodes.py", line 1004, in accept
    return visitor.visit_block(self)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/checker.py", line 1973, in visit_block
    self.accept(s)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/checker.py", line 401, in accept
    stmt.accept(self)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/nodes.py", line 1062, in accept
    return visitor.visit_assignment_stmt(self)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/checker.py", line 2011, in visit_assignment_stmt
    self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/checker.py", line 2147, in check_assignment
    rvalue_type = self.expr_checker.accept(rvalue)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/checkexpr.py", line 3757, in accept
    typ = node.accept(self)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/nodes.py", line 1544, in accept
    return visitor.visit_call_expr(self)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/checkexpr.py", line 263, in visit_call_expr
    return self.visit_call_expr_inner(e, allow_none_return=allow_none_return)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/checkexpr.py", line 312, in visit_call_expr_inner
    callee_type = get_proper_type(self.accept(e.callee, type_context, always_allow_any=True))
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/checkexpr.py", line 3757, in accept
    typ = node.accept(self)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/nodes.py", line 1496, in accept
    return visitor.visit_member_expr(self)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/checkexpr.py", line 1990, in visit_member_expr
    result = self.analyze_ordinary_member_access(e, is_lvalue)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/checkexpr.py", line 2008, in analyze_ordinary_member_access
    member_type = analyze_member_access(
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/checkmember.py", line 126, in analyze_member_access
    result = _analyze_member_access(name, typ, mx, override_info)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/checkmember.py", line 143, in _analyze_member_access
    return analyze_instance_member_access(name, typ, mx, override_info)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/checkmember.py", line 219, in analyze_instance_member_access
    typ = map_instance_to_supertype(typ, method.info)
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/maptype.py", line 20, in map_instance_to_supertype
    if not superclass.type_vars:
  File "/home/chris/.cache/pypoetry/virtualenvs/nqontrol-8ry3BIFW-py3.8/lib64/python3.8/site-packages/mypy/nodes.py", line 2659, in __getattribute__
    raise AssertionError(object.__getattribute__(self, 'msg'))
AssertionError: FuncBase for non-methods lack info
nqontrol/gui/widgets/servo_section/_callbacks.py:231: : note: use --pdb to drop into pdb

Now the line that causes the exception is:

def callLockRelock(value: bool, servoNumber: int):
→   servo = DEVICE.servo(servoNumber)

EDIT:
The other line seems to be caused by a code change and not by mypy.
Using the current version or mypy from master does not make any difference.

@emmatyping
Copy link
Member

@cddf, is there a repo with the source we could look at? If not, could you describe how DEVICE.servo is defined?

@cddf
Copy link

cddf commented May 27, 2020

@hauntsaninja
Copy link
Collaborator

We fixed issues that could lead to that stacktrace. Try again with mypy 0.980

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants