python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.22k stars 2.78k forks source link

Crash on older versions of Python for a slice in a type subscript #17679

Open VictorSizov opened 1 month ago

VictorSizov commented 1 month ago

Crash Report

Validating of my python module causes "INTERNAL ERROR"

Traceback

version: 1.11.1 Traceback (most recent call last): File "/home/sizov/Documents/root/venv/bin/mypy", line 8, in sys.exit(console_entry()) File "mypy/build.py", line 2069, in wrap_context File "mypy/build.py", line 2176, in parse_file File "mypy/build.py", line 841, in parse_file File "mypy/parse.py", line 27, in parse File "mypy/fastparse.py", line 234, in parse File "mypy/fastparse.py", line 401, in visit File "mypy/fastparse.py", line 860, in visit_Module File "mypy/fastparse.py", line 475, in translate_stmt_list File "mypy/fastparse.py", line 401, in visit File "mypy/fastparse.py", line 873, in visit_FunctionDef File "mypy/fastparse.py", line 890, in do_func_def File "mypy/fastparse.py", line 1072, in transform_args File "mypy/fastparse.py", line 1115, in make_argument File "mypy/fastparse.py", line 1877, in visit File "mypy/fastparse.py", line 2086, in visit_Subscript File "mypy/fastparse.py", line 1877, in visit File "mypy/fastparse.py", line 2065, in visit_Subscript AttributeError: 'NoneType' object has no attribute 'col_offset' parser.py: : note: use --pdb to drop into pdb

To Reproduce

write stand-alone example to disk as python file:


from typing import Dict, Optional, Union, List

def copy_attrs(src: Dict[str, Union[str, List[str]]], attr_filter: Optional[List[:str]] = None):
    # копируем только str/int и списки str/int
    def check_add_attr(dest, key, val):
        if isinstance(val, list):
            if not val:
                raise Exception("Empty index")
            val_res = list()
            for sub_val in val:
                if isinstance(sub_val, str):
                    sub_val = sub_val.lower()
                elif not isinstance(sub_val, int):
                    return
                val_res.append(sub_val)
            dest[key] = val_res
            return
        elif isinstance(val, str):
            val = val.lower()
        elif not isinstance(val, int):
            return
        dest[key] = val

    dest = dict()
    for key, val in list(src.items()):
        if attr_filter and key not in attr_filter:
            continue
        check_add_attr(dest, key, val)
        del src[key]
    return dest

and call "mypy \<stand-alone example>"

My Environment

AlexWaygood commented 1 month ago

I'm struggling to reproduce this on mypy-playground or locally. Do you have any configuration files lying around, e.g. pyproject.toml or mypy.ini?

svisser commented 1 month ago

I can reproduce it with Python 3.8.10, but not with a more recent version, like Python 3.11.0.

JelleZijlstra commented 1 month ago

Thanks, I can reproduce this too on 3.8:

% .venv/bin/python3.8 -m mypy -c 'from typing import List; x: List[:str]' --show-traceback
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.11.1
Traceback (most recent call last):
  File "/Users/jelle/Library/Application Support/uv/python/cpython-3.8.19-macos-aarch64-none/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/Users/jelle/Library/Application Support/uv/python/cpython-3.8.19-macos-aarch64-none/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/Users/jelle/py/tmp/.venv/lib/python3.8/site-packages/mypy/__main__.py", line 37, in <module>
    console_entry()
  File "mypy/build.py", line 2069, in wrap_context
  File "mypy/build.py", line 2176, in parse_file
  File "mypy/build.py", line 841, in parse_file
  File "mypy/parse.py", line 27, in parse
  File "mypy/fastparse.py", line 234, in parse
  File "mypy/fastparse.py", line 401, in visit
  File "mypy/fastparse.py", line 860, in visit_Module
  File "mypy/fastparse.py", line 475, in translate_stmt_list
  File "mypy/fastparse.py", line 401, in visit
  File "mypy/fastparse.py", line 1241, in visit_AnnAssign
  File "mypy/fastparse.py", line 1877, in visit
  File "mypy/fastparse.py", line 2065, in visit_Subscript
AttributeError: 'NoneType' object has no attribute 'col_offset'
: note: use --pdb to drop into pdb

@VictorSizov the bug in your code is that you have List[:str] instead of List[str]. Fixing that should make the crash go away.

However, of course this is still a bug in mypy; mypy should report an error here instead of crashing.