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 on older versions of Python for a slice in a type subscript #17679

Closed
VictorSizov opened this issue Aug 15, 2024 · 4 comments
Closed

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

VictorSizov opened this issue Aug 15, 2024 · 4 comments
Labels

Comments

@VictorSizov
Copy link

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

  • Mypy version used: 1.11.1
  • Mypy command-line flags: --show-traceback
  • Python version used: 3.10.12, 3.8.19
  • Operating system and version: Ubuntu 22.04.4 LTS
@AlexWaygood
Copy link
Member

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
Copy link

svisser commented Aug 15, 2024

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

@JelleZijlstra
Copy link
Member

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.

@JelleZijlstra JelleZijlstra changed the title Validating of my python module causes "INTERNAL ERROR" Crash on older versions of Python for a slice in a type subscript Aug 15, 2024
@hauntsaninja
Copy link
Collaborator

mypy has dropped support for running with Python 3.8 and this isn't an issue when running mypy with Python 3.9 or newer

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Dec 31, 2024
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

5 participants