pylint-dev / pylint

It's not just a linter that annoys you!
https://pylint.readthedocs.io/en/latest/
GNU General Public License v2.0
5.26k stars 1.12k forks source link

Crash: Pylint crashes on TypeVar(Literal[])) #9062

Closed decision-dev closed 1 year ago

decision-dev commented 1 year ago

When parsing the following file:

"""
crashes pylint
"""

from typing import Literal, TypeVar

RangeAttrs = TypeVar(Literal['start', 'delta', 'end'])

pylint crashed with a AstroidError and with the following stacktrace:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/astroid/inference_tip.py", line 33, in _inference_tip_cached
    result = _cache[func, node]
KeyError: (<function infer_typing_typevar_or_newtype at 0x7f61a80e21f0>, <Call l.7 at 0x7f61a78cf610>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/astroid/builder.py", line 181, in _data_build
    node, parser_module = _parse_string(data, type_comments=True)
  File "/usr/local/lib/python3.9/dist-packages/astroid/builder.py", line 484, in _parse_string
    parsed = parser_module.parse(data + "\n", type_comments=type_comments)
  File "/usr/local/lib/python3.9/dist-packages/astroid/_ast.py", line 50, in parse
    return parse_func(string)
  File "/usr/lib/python3.9/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 10
    class Literal['start', 'delta', 'end'](metaclass=Meta):
                 ^
SyntaxError: invalid syntax

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/pylint/checkers/utils.py", line 1390, in safe_infer
    value = next(infer_gen)
  File "/usr/local/lib/python3.9/dist-packages/astroid/nodes/node_ng.py", line 161, in infer
    results = list(self._explicit_inference(self, context, **kwargs))
  File "/usr/local/lib/python3.9/dist-packages/astroid/inference_tip.py", line 40, in _inference_tip_cached
    result = _cache[func, node] = list(func(*args, **kwargs))
  File "/usr/local/lib/python3.9/dist-packages/astroid/brain/brain_typing.py", line 140, in infer_typing_typevar_or_newtype
    node = extract_node(TYPING_TYPE_TEMPLATE.format(typename))
  File "/usr/local/lib/python3.9/dist-packages/astroid/builder.py", line 450, in extract_node
    tree = parse(code, module_name=module_name)
  File "/usr/local/lib/python3.9/dist-packages/astroid/builder.py", line 307, in parse
    return builder.string_build(code, modname=module_name, path=path)
  File "/usr/local/lib/python3.9/dist-packages/astroid/builder.py", line 151, in string_build
    module, builder = self._data_build(data, modname, path)
  File "/usr/local/lib/python3.9/dist-packages/astroid/builder.py", line 183, in _data_build
    raise AstroidSyntaxError(
astroid.exceptions.AstroidSyntaxError: Parsing Python code failed:
invalid syntax (<unknown>, line 10)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/pylint/lint/pylinter.py", line 811, in _lint_file
    check_astroid_module(module)
  File "/usr/local/lib/python3.9/dist-packages/pylint/lint/pylinter.py", line 1085, in check_astroid_module
    retval = self._check_astroid_module(
  File "/usr/local/lib/python3.9/dist-packages/pylint/lint/pylinter.py", line 1135, in _check_astroid_module
    walker.walk(node)
  File "/usr/local/lib/python3.9/dist-packages/pylint/utils/ast_walker.py", line 94, in walk
    self.walk(child)
  File "/usr/local/lib/python3.9/dist-packages/pylint/utils/ast_walker.py", line 94, in walk
    self.walk(child)
  File "/usr/local/lib/python3.9/dist-packages/pylint/utils/ast_walker.py", line 91, in walk
    callback(astroid)
  File "/usr/local/lib/python3.9/dist-packages/pylint/checkers/base/name_checker/checker.py", line 416, in visit_assignname
    inferred_assign_type = utils.safe_infer(assign_type.value)
  File "/usr/local/lib/python3.9/dist-packages/pylint/checkers/utils.py", line 1394, in safe_infer
    raise AstroidError from e
astroid.exceptions.AstroidError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/pylint/lint/pylinter.py", line 775, in _lint_files
    self._lint_file(fileitem, module, check_astroid_module)
  File "/usr/local/lib/python3.9/dist-packages/pylint/lint/pylinter.py", line 813, in _lint_file
    raise astroid.AstroidError from e
astroid.exceptions.AstroidError
jacobtylerwalls commented 1 year ago

Hi there 👋 :

This is a duplicate of #8802, fixed in astroid 2.15.7, which is up on PyPI and compatible with pylint 2.17.5. We're scheduling a patch release of pylint (2.17.6) to enforce this higher version of astroid going forward.