Skip to content

crash when using isinstance againt variadic tuple #15978

Closed
@mehdigmira

Description

@mehdigmira

Crash Report

mypy crashes when using isinstance againt a class that is a variadic tuple

Traceback

test.py:15: 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.6.0+dev.d7b24514d7301f86031b7d1e2215cf8c2476bec0
Traceback (most recent call last):
  File "/home/vscode/.local/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/main.py", line 99, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/main.py", line 178, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/build.py", line 189, in build
    result = _build(
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/build.py", line 262, in _build
    graph = dispatch(sources, manager, stdout)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/build.py", line 2938, in dispatch
    process_graph(graph, manager)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/build.py", line 3336, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/build.py", line 3437, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/build.py", line 2306, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/checker.py", line 475, in check_first_pass
    self.accept(d)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/checker.py", line 585, in accept
    stmt.accept(self)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/nodes.py", line 790, in accept
    return visitor.visit_func_def(self)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/checker.py", line 994, in visit_func_def
    self._visit_func_def(defn)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/checker.py", line 998, in _visit_func_def
    self.check_func_item(defn, name=defn.name)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/checker.py", line 1071, in check_func_item
    self.check_func_def(defn, typ, name, allow_empty)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/checker.py", line 1281, in check_func_def
    self.accept(item.body)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/checker.py", line 585, in accept
    stmt.accept(self)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/nodes.py", line 1226, in accept
    return visitor.visit_block(self)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/checker.py", line 2754, in visit_block
    self.accept(s)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/checker.py", line 585, in accept
    stmt.accept(self)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/nodes.py", line 1246, in accept
    return visitor.visit_expression_stmt(self)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/checker.py", line 4276, in visit_expression_stmt
    expr_type = self.expr_checker.accept(s.expr, allow_none_return=True, always_allow_any=True)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/checkexpr.py", line 5362, in accept
    typ = self.visit_call_expr(node, allow_none_return=True)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/checkexpr.py", line 452, in visit_call_expr
    return self.visit_call_expr_inner(e, allow_none_return=allow_none_return)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/checkexpr.py", line 581, in visit_call_expr_inner
    ret_type = self.check_call_expr_with_callee_type(
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/checkexpr.py", line 1420, in check_call_expr_with_callee_type
    ret_type, callee_type = self.check_call(
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/checkexpr.py", line 1514, in check_call
    return self.check_callable_call(
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/checkexpr.py", line 1702, in check_callable_call
    arg_types = self.infer_arg_types_in_context(callee, args, arg_kinds, formal_to_actual)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/checkexpr.py", line 1874, in infer_arg_types_in_context
    res[ai] = self.accept(args[ai], arg_type)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/checkexpr.py", line 5370, in accept
    typ = node.accept(self)
          ^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/nodes.py", line 1809, in accept
    return visitor.visit_name_expr(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/checkexpr.py", line 336, in visit_name_expr
    result = self.analyze_ref_expr(e)
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/checkexpr.py", line 368, in analyze_ref_expr
    result = type_object_type(node, self.named_type)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/checkmember.py", line 1293, in type_object_type
    return type_object_type_from_function(t, info, method.info, fallback, is_new)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/typeops.py", line 157, in type_object_type_from_function
    signature = cast(FunctionLike, map_type_from_supertype(signature, info, def_info))
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/typeops.py", line 239, in map_type_from_supertype
    inst_type = map_instance_to_supertype(inst_type, super_info)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/maptype.py", line 29, in map_instance_to_supertype
    tuple_type = expand_type(instance.type.tuple_type, env)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/expandtype.py", line 71, in expand_type
    return typ.accept(ExpandTypeVisitor(env))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/types.py", line 2382, in accept
    return visitor.visit_tuple_type(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/expandtype.py", line 409, in visit_tuple_type
    items = self.expand_types_with_unpack(t.items)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/expandtype.py", line 397, in expand_types_with_unpack
    unpacked_items = self.expand_unpack(item)
                     ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/expandtype.py", line 292, in expand_unpack
    raise RuntimeError(f"Invalid type replacement to expand: {repl}")
RuntimeError: Invalid type replacement to expand: Unpack[TP`1]
test.py:15: : note: use --pdb to drop into pdb

To Reproduce

from __future__ import annotations

from typing import Tuple, Unpack  # noqa

from typing_extensions import TypeVarTuple

TP = TypeVarTuple("TP")


class A(Tuple[Unpack[TP]]):  # noqa
    ...


def test(d: A[int, str]):
    isinstance(d, A)

Your Environment

  • Mypy version used: 1.6.0+dev.d7b24514d7301f86031b7d1e2215cf8c2476bec0
  • Mypy command-line flags: --enable-incomplete-feature=TypeVarTuple --enable-incomplete-feature=Unpack
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.11.4
  • Operating system and version: MacOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions