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 with TypeVarTuple #15946

Closed
mehdigmira opened this issue Aug 24, 2023 · 2 comments · Fixed by #15961
Closed

Crash with TypeVarTuple #15946

mehdigmira opened this issue Aug 24, 2023 · 2 comments · Fixed by #15961
Labels
crash topic-pep-646 PEP 646 (TypeVarTuple, Unpack)

Comments

@mehdigmira
Copy link

Crash Report

Using current dev version of mypy (1.6.0+dev.4077dc6c4b87b273bfd4552d75faaafa6c016c25), with PEP646 experimental flag, the following code makes mypy crash

Traceback

$ /home/vscode/.local/bin/mypy  --enable-incomplete-feature=TypeVarTuple  --enable-incomplete-feature=Unpack test.py --show-traceback      
test.py:14: error: "A" expects 1 type argument, but 2 given  [type-arg]
test.py:14: 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.4077dc6c4b87b273bfd4552d75faaafa6c016c25
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 3431, in process_stale_scc
    mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/semanal_main.py", line 94, in semantic_analysis_for_scc
    process_functions(graph, scc, patches)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/semanal_main.py", line 252, in process_functions
    process_top_level_function(
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/semanal_main.py", line 291, in process_top_level_function
    deferred, incomplete, progress = semantic_analyze_target(
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/semanal_main.py", line 349, in semantic_analyze_target
    analyzer.refresh_partial(
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/semanal.py", line 603, in refresh_partial
    self.accept(node)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/semanal.py", line 6487, in accept
    node.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/semanal.py", line 833, in visit_func_def
    self.analyze_func_def(defn)
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/semanal.py", line 867, in analyze_func_def
    if self.found_incomplete_ref(tag) or has_placeholder(result):
                                         ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/semanal_shared.py", line 355, in has_placeholder
    return typ.accept(HasPlaceholders())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/types.py", line 1929, in accept
    return visitor.visit_callable_type(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/type_visitor.py", line 505, in visit_callable_type
    args = self.query_types(t.arg_types)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/type_visitor.py", line 557, in query_types
    return any(t.accept(self) for t in types)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/type_visitor.py", line 557, in <genexpr>
    return any(t.accept(self) for t in types)
               ^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/types.py", line 397, in accept
    return visitor.visit_type_alias_type(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/type_visitor.py", line 550, in visit_type_alias_type
    return get_proper_type(t).accept(self)
           ^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/types.py", line 2986, in get_proper_type
    typ = typ._expand_once()
          ^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/types.py", line 339, in _expand_once
    new_tp = self.alias.target.accept(InstantiateAliasVisitor(mapping))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/types.py", line 2352, in accept
    return visitor.visit_tuple_type(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/expandtype.py", line 406, 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 394, in expand_types_with_unpack
    unpacked_items = self.expand_unpack(item)
                     ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/lib/python3.11/site-packages/mypy/expandtype.py", line 291, in expand_unpack
    raise RuntimeError(f"Invalid type replacement to expand: {repl}")
RuntimeError: Invalid type replacement to expand: Unpack[builtins.tuple[Any, ...]]
test.py:14: : note: use --pdb to drop into pdb

To Reproduce

from __future__ import annotations

from typing import Generic, Tuple, reveal_type  # noqa

from typing_extensions import TypeVarTuple, Unpack  # noqa

T = TypeVarTuple("T")


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


def f(w: A[str, int]):
    x, y = w
    reveal_type(x)
    reveal_type(y)

Your Environment

  • Mypy version used: 1.6.0+dev.4077dc6c4b87b273bfd4552d75faaafa6c016c25
  • 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, but using Ubuntu devcontainer image
@AlexWaygood AlexWaygood added the topic-pep-646 PEP 646 (TypeVarTuple, Unpack) label Aug 24, 2023
@sobolevn
Copy link
Member

A bit simplified:

from typing import Tuple
from typing_extensions import TypeVarTuple, Unpack

T = TypeVarTuple("T")

class A(Tuple[Unpack[T]]):
    ...

x: A[str]  # errors here

ilevkivskyi added a commit that referenced this issue Aug 26, 2023
Fixes #15946

Note this actually adds support also for variadic NamedTuples and
variadic TypedDicts. Not that anyone requested this, but since generic
NamedTuples and generic TypedDicts are supported using the same
mechanism (special aliases) as generic tuple types (like `class
A(Tuple[T, S]): ...` in the issue), it looked more risky and arbitrary
to _not_support them.

Btw the implementation is simple, but while I was working on this, I
accidentally found a problem with my general idea of doing certain type
normlaizations in `semanal_typeargs.py`. The problem is that sometimes
we can call `get_proper_type()` during semantic analysis, so all the
code that gets triggered by this (mostly `expand_type()`) can't really
rely on types being normalized. Fortunately, with just few tweaks I
manged to make the code mostly robust to such scenarios (TBH there are
few possible holes left, but this is getting really complex, I think it
is better to release this, and see if people will ever hit such
scenarios, then fix accordingly).
@mehdigmira
Copy link
Author

Opened a new issue that (I think) is linked to the PR that fixes this: this #15978

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash topic-pep-646 PEP 646 (TypeVarTuple, Unpack)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants