Skip to content

Segfault/RecursionError when using tuple, one item literal and self-referencing type #15351

Closed
@VojtaOrgon

Description

@VojtaOrgon

Crash Report

When creating self referencing NotFilter = tuple[Literal["not"], "NotFilter"] mypy crashes (segfault on 1.3.0 or max recursion depth on master) if first item in tuple is Literal with one item.

It works for any other type I tried. Or even if Literal has more than one item.

Traceback

It only produced traceback on master build on mypy playground.

And since its recursion problem it has 32k lines so I tried to cut it down a bit.

Traceback (most recent call last):
  File "/layers/google.python.runtime/python/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/layers/google.python.runtime/python/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/__main__.py", line 37, in <module>
    console_entry()
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/main.py", line 95, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/main.py", line 174, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/build.py", line 194, in build
    result = _build(
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/build.py", line 267, in _build
    graph = dispatch(sources, manager, stdout)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/build.py", line 2926, in dispatch
    process_graph(graph, manager)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/build.py", line 3324, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/build.py", line 3425, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/build.py", line 2311, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/checker.py", line 472, in check_first_pass
    self.accept(d)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/checker.py", line 582, in accept
    stmt.accept(self)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/nodes.py", line 1307, in accept
    return visitor.visit_assignment_stmt(self)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/checker.py", line 2732, in visit_assignment_stmt
    self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/checker.py", line 2905, in check_assignment
    rvalue_type = self.check_simple_assignment(lvalue_type, rvalue, context=rvalue)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/checker.py", line 3955, in check_simple_assignment
    rvalue_type = self.expr_checker.accept(
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/checkexpr.py", line 4961, in accept
    typ = node.accept(self)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/nodes.py", line 1960, in accept
    return visitor.visit_index_expr(self)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/checkexpr.py", line 3687, in visit_index_expr
    result = self.visit_index_expr_helper(e)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/checkexpr.py", line 3701, in visit_index_expr_helper
    return self.accept(e.analyzed)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/checkexpr.py", line 4961, in accept
    typ = node.accept(self)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/nodes.py", line 2646, in accept
    return visitor.visit_type_alias_expr(self)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/checkexpr.py", line 4015, in visit_type_alias_expr
    return self.alias_type_in_runtime_context(alias.node, ctx=alias, alias_definition=True)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/checkexpr.py", line 4062, in alias_type_in_runtime_context
    tuple_fallback(item).type.fullname != "builtins.tuple"
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/typeops.py", line 115, in tuple_fallback
    return Instance(info, [join_type_list(items)], extra_attrs=typ.partial_fallback.extra_attrs)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/join.py", line 683, in join_type_list
    joined = join_types(joined, t)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/join.py", line 261, in join_types
    return t.accept(TypeJoinVisitor(s, instance_joiner))
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/types.py", line 2294, in accept
    return visitor.visit_tuple_type(self)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/join.py", line 473, in visit_tuple_type
    return join_types(self.s, mypy.typeops.tuple_fallback(t))

# repeats for a while
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/typeops.py", line 115, in tuple_fallback
    return Instance(info, [join_type_list(items)], extra_attrs=typ.partial_fallback.extra_attrs)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/join.py", line 683, in join_type_list
    joined = join_types(joined, t)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/join.py", line 261, in join_types
    return t.accept(TypeJoinVisitor(s, instance_joiner))
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/types.py", line 2294, in accept
    return visitor.visit_tuple_type(self)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/join.py", line 473, in visit_tuple_type
    return join_types(self.s, mypy.typeops.tuple_fallback(t))

# last iteration
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/typeops.py", line 115, in tuple_fallback
    return Instance(info, [join_type_list(items)], extra_attrs=typ.partial_fallback.extra_attrs)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/join.py", line 683, in join_type_list
    joined = join_types(joined, t)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/join.py", line 227, in join_types
    t = get_proper_type(t)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/types.py", line 2924, in get_proper_type
    typ = typ._expand_once()
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/types.py", line 333, in _expand_once
    new_tp = self.alias.target.accept(InstantiateAliasVisitor(mapping))
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/types.py", line 2294, in accept
    return visitor.visit_tuple_type(self)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/expandtype.py", line 482, in visit_tuple_type
    items = self.expand_types_with_unpack(t.items)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/expandtype.py", line 478, in expand_types_with_unpack
    items.append(item.accept(self))
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/types.py", line 391, in accept
    return visitor.visit_type_alias_type(self)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/expandtype.py", line 529, in visit_type_alias_type
    return t.copy_modified(args=args)
  File "/layers/google.python.pip/pip/lib/python3.10/site-packages/mypy/types.py", line 424, in copy_modified
    return TypeAliasType(
RecursionError: maximum recursion depth exceeded

To Reproduce

from typing import Literal

NotFilter = tuple[Literal["not"], "NotFilter"]

I tried to replace Literal["not"] with other types and there is no problem. It also works if there is more than one item in literal.

Your Environment

I was able to reproduce it on mypy playground for current master and 1.3.0 down to 1.0.0.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions