python / python/mypy

mypyc crashes when using a narrowed type that is not found at compile time

Open
#14,694 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

The following worked with mypy<1.0.

To Reproduce

import not_found_at_compile_time
# in my case: the PEP-517 build dependencies did not list all the runtime dependencies to be also present at build/compile time

def test(x: dict[int, int] | not_found_at_compile_time.A) -> None:
    if isinstance(x, dict):
        test = {key: value for key, value in x.items()}

Expected Behavior

mypyc does not crash.

Actual Behavior

mypyc crashes:

$ mypy test.py
Success: no issues found in 1 source file

$ mypyc test.py
Traceback (most recent call last):
File "mypyc/irbuild/builder.py", line 247, in accept
File "mypy/nodes.py", line 2368, in accept
File "mypyc/irbuild/visitor.py", line 314, in visit_dictionary_comprehension
File "mypyc/irbuild/expression.py", line 995, in transform_dictionary_comprehension
File "mypyc/irbuild/for_helpers.py", line 330, in comprehension_helper
File "mypyc/irbuild/for_helpers.py", line 290, in handle_loop
File "mypyc/irbuild/for_helpers.py", line 101, in for_loop_helper
File "mypyc/irbuild/for_helpers.py", line 467, in make_for_loop_generator
File "mypyc/irbuild/builder.py", line 901, in get_dict_item_type
File "mypyc/irbuild/builder.py", line 893, in get_dict_key_type
File "mypyc/irbuild/builder.py", line 888, in get_dict_base_type
test.py:6: AssertionError: Union[builtins.dict[builtins.int, builtins.int], builtins.dict[Any, Any]]

Your Environment

  • Mypy version used: 1.0.0
  • Python version used: 3.11.1

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 from the issue's test.py example, then inspect mypyc/irbuild/builder.py around get_dict_base_type, get_dict_key_type, and get_dict_item_type, along with the dictionary-comprehension path in mypyc/irbuild/for_helpers.py. Add a regression test covering a narrowed type whose dependency is unavailable at compile time; done means mypyc completes without the AssertionError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.