python / python/mypy

Assertion error: `isinstance(ret, Instance)`

Open
#20,585 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

crash
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Crash Report

I tried running mypy on my codebase and it hit an INTERNAL ERROR.

Traceback

test_bloom_real.py:10: error: "AST" has no attribute "ops"  [attr-defined]
test_bloom_real.py:10: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 1.20.0+dev.131f9d92da58294bb2f273425e8778bd7d5b861f
Traceback (most recent call last):
  File "/home/danzin/venvs/wily_3.14_venv/bin/mypy", line 7, in <module>
    sys.exit(console_entry())
  File "/home/danzin/projects/mypy/mypy/__main__.py", line 15, in console_entry
    main()
  File "/home/danzin/projects/mypy/mypy/main.py", line 135, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/home/danzin/projects/mypy/mypy/main.py", line 219, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/home/danzin/projects/mypy/mypy/build.py", line 347, in build
    result = build_inner(
  File "/home/danzin/projects/mypy/mypy/build.py", line 444, in build_inner
    graph = dispatch(sources, manager, stdout)
  File "/home/danzin/projects/mypy/mypy/build.py", line 3212, in dispatch
    process_graph(graph, manager)
  File "/home/danzin/projects/mypy/mypy/build.py", line 3623, in process_graph
    done, still_working, results = manager.wait_for_done(graph)
  File "/home/danzin/projects/mypy/mypy/build.py", line 1097, in wait_for_done
    process_stale_scc(graph, next_scc, self)
  File "/home/danzin/projects/mypy/mypy/build.py", line 3787, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/home/danzin/projects/mypy/mypy/build.py", line 2577, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/home/danzin/projects/mypy/mypy/checker.py", line 530, in check_first_pass
    self.accept(d)
  File "/home/danzin/projects/mypy/mypy/checker.py", line 641, in accept
    stmt.accept(self)
  File "/home/danzin/projects/mypy/mypy/nodes.py", line 1487, in accept
    return visitor.visit_class_def(self)
  File "/home/danzin/projects/mypy/mypy/checker.py", line 2687, in visit_class_def
    self.accept(defn.defs)
  File "/home/danzin/projects/mypy/mypy/checker.py", line 641, in accept
    stmt.accept(self)
  File "/home/danzin/projects/mypy/mypy/nodes.py", line 1582, in accept
    return visitor.visit_block(self)
  File "/home/danzin/projects/mypy/mypy/checker.py", line 3159, in visit_block
    self.accept(s)
  File "/home/danzin/projects/mypy/mypy/checker.py", line 641, in accept
    stmt.accept(self)
  File "/home/danzin/projects/mypy/mypy/nodes.py", line 1004, in accept
    return visitor.visit_func_def(self)
  File "/home/danzin/projects/mypy/mypy/checker.py", line 1198, in visit_func_def
    self._visit_func_def(defn)
  File "/home/danzin/projects/mypy/mypy/checker.py", line 1202, in _visit_func_def
    self.check_func_item(defn, name=defn.name)
  File "/home/danzin/projects/mypy/mypy/checker.py", line 1237, in check_func_item
    self.check_func_def(defn, typ, name, allow_empty)
  File "/home/danzin/projects/mypy/mypy/checker.py", line 1510, in check_func_def
    self.accept(item.body)
  File "/home/danzin/projects/mypy/mypy/checker.py", line 641, in accept
    stmt.accept(self)
  File "/home/danzin/projects/mypy/mypy/nodes.py", line 1582, in accept
    return visitor.visit_block(self)
  File "/home/danzin/projects/mypy/mypy/checker.py", line 3159, in visit_block
    self.accept(s)
  File "/home/danzin/projects/mypy/mypy/checker.py", line 641, in accept
    stmt.accept(self)
  File "/home/danzin/projects/mypy/mypy/nodes.py", line 1669, in accept
    return visitor.visit_assignment_stmt(self)
  File "/home/danzin/projects/mypy/mypy/checker.py", line 3210, in visit_assignment_stmt
    self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
  File "/home/danzin/projects/mypy/mypy/checker.py", line 3436, in check_assignment
    rvalue_type = self.expr_checker.accept(rvalue, type_context=type_context)
  File "/home/danzin/projects/mypy/mypy/checkexpr.py", line 6087, in accept
    typ = node.accept(self)  # r-value type, when interpreted as a value expression
  File "/home/danzin/projects/mypy/mypy/nodes.py", line 2764, in accept
    return visitor.visit_list_comprehension(self)
  File "/home/danzin/projects/mypy/mypy/checkexpr.py", line 5784, in visit_list_comprehension
    return self.check_generator_or_comprehension(
  File "/home/danzin/projects/mypy/mypy/checkexpr.py", line 5845, in check_generator_or_comprehension
    return self.check_call(constructor, [gen.left_expr], [nodes.ARG_POS], gen)[0]
  File "/home/danzin/projects/mypy/mypy/checkexpr.py", line 1588, in check_call
    return self.check_callable_call(
  File "/home/danzin/projects/mypy/mypy/checkexpr.py", line 1767, in check_callable_call
    callee = self.infer_function_type_arguments(
  File "/home/danzin/projects/mypy/mypy/checkexpr.py", line 2092, in infer_function_type_arguments
    arg_types = self.infer_arg_types_in_context(
  File "/home/danzin/projects/mypy/mypy/checkexpr.py", line 1978, in infer_arg_types_in_context
    res.append(self.accept(arg, ctx))
  File "/home/danzin/projects/mypy/mypy/checkexpr.py", line 6085, in accept
    typ = self.accept_maybe_cache(node, type_context=type_context)
  File "/home/danzin/projects/mypy/mypy/checkexpr.py", line 6120, in accept_maybe_cache
    typ = node.accept(self)
  File "/home/danzin/projects/mypy/mypy/nodes.py", line 2298, in accept
    return visitor.visit_call_expr(self)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/home/danzin/projects/mypy/mypy/checkexpr.py", line 508, in visit_call_expr
    return self.visit_call_expr_inner(e, allow_none_return=allow_none_return)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/danzin/projects/mypy/mypy/checkexpr.py", line 645, in visit_call_expr_inner
    ret_type = self.check_call_expr_with_callee_type(
        callee_type, e, fullname, object_type, member
    )
  File "/home/danzin/projects/mypy/mypy/checkexpr.py", line 1495, in check_call_expr_with_callee_type
    ret_type, callee_type = self.check_call(
                            ~~~~~~~~~~~~~~~^
        callee_type,
        ^^^^^^^^^^^^
    ...<6 lines>...
        object_type=object_type,
        ^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/home/danzin/projects/mypy/mypy/checkexpr.py", line 1588, in check_call
    return self.check_callable_call(
           ~~~~~~~~~~~~~~~~~~~~~~~~^
        callee,
        ^^^^^^^
    ...<6 lines>...
        object_type,
        ^^^^^^^^^^^^
    )
    ^
  File "/home/danzin/projects/mypy/mypy/checkexpr.py", line 1692, in check_callable_call
    and callee.type_object().is_protocol
        ~~~~~~~~~~~~~~~~~~^^
  File "/home/danzin/projects/mypy/mypy/types.py", line 2348, in type_object
    assert isinstance(ret, Instance)
           ~~~~~~~~~~^^^^^^^^^^^^^^^
AssertionError:
test_bloom_real.py:10: : note: use --pdb to drop into pdb

To Reproduce

Just running mypy on the code below shows the issue, it also reproduces easily in the Playground.

import ast

class ComparisonSwapper(ast.NodeTransformer):
    OP_MAP = {
        ast.Lt: ast.GtE,
        ast.GtE: ast.Lt,
    }

    def visit_Compare(self, node: ast.AST) -> ast.AST:
        new_ops = [self.OP_MAP.get(type(op), type(op))() for op in node.ops]
        node.ops = new_ops
        return node

Gist URL: https://gist.github.com/mypy-play/dd0214051d0571c0ac920271a1ac4193
Playground URL: https://mypy-play.net/?mypy=latest&python=3.12&gist=dd0214051d0571c0ac920271a1ac4193

Your Environment

  • Mypy version used: 1.20.0+dev.131f9d92da58294bb2f273425e8778bd7d5b861f
  • Mypy command-line flags: --show-traceback
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: Python 3.15.0a3+ (heads/main-dirty:fca7fec88ce, Jan 13 2026, 12:23:26) [Clang 21.1.2 (2ubuntu6)] (also repros on 3.14 locally, 3.12 on the Playground etc.)
  • Operating system and version: Ubuntu 25.10 (Questing Quokka) x86_64

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the crash with the provided ComparisonSwapper example, then trace the call path through mypy/checkexpr.py to types.py, where type_object() asserts that its result is an Instance. Investigate why the protocol-related call path produces another type and add regression coverage for the example; done means mypy reports the underlying type error without an INTERNAL ERROR or assertion traceback.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.