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

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

Open
twoertwein opened this issue Feb 13, 2023 · 2 comments
Open
Labels

Comments

@twoertwein
Copy link

twoertwein commented Feb 13, 2023

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
@twoertwein twoertwein added the bug mypy got something wrong label Feb 13, 2023
@ichard26
Copy link
Collaborator

ichard26 commented Feb 14, 2023

This is the same bug fundamentally as mypyc/mypyc#965.

@twoertwein
Copy link
Author

Feel free to close it here if that makes it easier to manage/prioritize the issues.

I think the above example worked with the pervious mypy release.

To be fair, I somehow like the new "behavior": as it ensures that all dependencies are installed at compile time :) I assume, while previous mypyc version did not crash, they probably compiled less ideal code (key and values were probably Any instead of int).

@AlexWaygood AlexWaygood removed the bug mypy got something wrong label Feb 14, 2023
JukkaL pushed a commit that referenced this issue Feb 16, 2023
An optimization to make iterating over dict.keys(), dict.values() and
dict.items() faster caused mypyc to crash while compiling a Union of
dictionaries. This commit fixes the optimization helpers to properly
handle unions.

irbuild.Builder.get_dict_base_type() now returns list[Instance] with the
union items. In the common case we don't have a union, a single-element
list is returned. And get_dict_key_type() and get_dict_value_type() will
now build a simplified RUnion as needed.

Fixes mypyc/mypyc#965 and probably #14694.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants