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

Crash following a class-level import #7045

Closed
njsmith opened this issue Jun 22, 2019 · 2 comments · Fixed by #12023
Closed

Crash following a class-level import #7045

njsmith opened this issue Jun 22, 2019 · 2 comments · Fixed by #12023

Comments

@njsmith
Copy link

njsmith commented Jun 22, 2019

Sample code:

# a.py
def meth(self, x: int) -> int:
    return 2 * x

# b.py
class Foo:
    from a import meth

foo = Foo()
print(foo.meth(10))

Using latest mypy master (last release also crashes, though with a different traceback):

~/src/mypy$ mypy /tmp/b.py --show-traceback
/tmp/b.py:5: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.rtfd.io/en/latest/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 0.720+dev.95ac93fdec0f0ae5a9b9ebe2519f7c8eaf1feeb1
Traceback (most recent call last):
  File "/home/njs/.user-python3.7/bin/mypy", line 10, in <module>
    sys.exit(console_entry())
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/__main__.py", line 8, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/main.py", line 83, in main
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/build.py", line 164, in build
    result = _build(sources, options, alt_lib_path, flush_errors, fscache, stdout, stderr)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/build.py", line 224, in _build
    graph = dispatch(sources, manager, stdout)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/build.py", line 2567, in dispatch
    process_graph(graph, manager)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/build.py", line 2880, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/build.py", line 2987, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/build.py", line 2096, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/checker.py", line 281, in check_first_pass
    self.accept(d)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/checker.py", line 392, in accept
    stmt.accept(self)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/nodes.py", line 993, in accept
    return visitor.visit_expression_stmt(self)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/checker.py", line 2739, in visit_expression_stmt
    self.expr_checker.accept(s.expr, allow_none_return=True, always_allow_any=True)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/checkexpr.py", line 3255, in accept
    typ = self.visit_call_expr(node, allow_none_return=True)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/checkexpr.py", line 234, in visit_call_expr
    return self.visit_call_expr_inner(e, allow_none_return=allow_none_return)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/checkexpr.py", line 304, in visit_call_expr_inner
    ret_type = self.check_call_expr_with_callee_type(callee_type, e, fullname, object_type)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/checkexpr.py", line 648, in check_call_expr_with_callee_type
    object_type=object_type)[0]
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/checkexpr.py", line 685, in check_call
    object_type)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/checkexpr.py", line 770, in check_callable_call
    callee, args, arg_kinds, formal_to_actual)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/checkexpr.py", line 864, in infer_arg_types_in_context
    res[ai] = self.accept(args[ai], callee.arg_types[i])
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/checkexpr.py", line 3259, in accept
    typ = node.accept(self)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/nodes.py", line 1518, in accept
    return visitor.visit_call_expr(self)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/checkexpr.py", line 234, in visit_call_expr
    return self.visit_call_expr_inner(e, allow_none_return=allow_none_return)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/checkexpr.py", line 279, in visit_call_expr_inner
    callee_type = self.accept(e.callee, type_context, always_allow_any=True)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/checkexpr.py", line 3259, in accept
    typ = node.accept(self)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/nodes.py", line 1470, in accept
    return visitor.visit_member_expr(self)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/checkexpr.py", line 1751, in visit_member_expr
    result = self.analyze_ordinary_member_access(e, is_lvalue)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/checkexpr.py", line 1766, in analyze_ordinary_member_access
    in_literal_context=self.is_literal_context())
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/checkmember.py", line 103, in analyze_member_access
    result = _analyze_member_access(name, typ, mx, override_info)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/checkmember.py", line 117, in _analyze_member_access
    return analyze_instance_member_access(name, typ, mx, override_info)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/checkmember.py", line 187, in analyze_instance_member_access
    typ = map_instance_to_supertype(typ, method.info)
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/maptype.py", line 20, in map_instance_to_supertype
    if not superclass.type_vars:
  File "/home/njs/.user-python3.7/lib/python3.7/site-packages/mypy/nodes.py", line 2600, in __getattribute__
    raise AssertionError(object.__getattribute__(self, 'msg'))
AssertionError: FuncBase for non-methods lack info
/tmp/b.py:5: : note: use --pdb to drop into pdb

(Yes this is silly. I have a class where some methods are hand-written and some are output by a code generator, so the idea is that a.py is generated and then b.py assembles the generated and hand-written bits together into a class.)

@ilevkivskyi
Copy link
Member

This still crashes on master. The situation is quite rare however, so setting priority to normal.

@VMinute
Copy link

VMinute commented Jan 28, 2021

Ok, I experienced the same issue and found a workaround.
Instead of using from directly inside class declaration, I added a member to the class, so b.py changes to something like this:

import a

class Foo:
    meth = a.meth

foo = Foo()
print(foo.meth(10))

In this way I can declare functions for the same class across multiple modules and keep mypy happy.
This adds imports and not so nice looking boilerplate code, but if you need to split class code across multiple sources files and use mypy this seem a decent solution.

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

Successfully merging a pull request may close this issue.

3 participants