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

AstroidSyntaxError when parsing NewType #5770

Closed
cassdalton opened this issue Feb 4, 2022 · 2 comments · Fixed by #5846
Closed

AstroidSyntaxError when parsing NewType #5770

cassdalton opened this issue Feb 4, 2022 · 2 comments · Fixed by #5846
Labels
Crash 💥 A bug that makes pylint crash
Milestone

Comments

@cassdalton
Copy link

cassdalton commented Feb 4, 2022

pylint-crash-2022-02-04-15.txt

@Pierre-Sassoulas Pierre-Sassoulas added Crash 💥 A bug that makes pylint crash Needs investigation 🔬 A bug or crash where it's not immediately obvious what is happenning labels Feb 5, 2022
@jacobtylerwalls
Copy link
Member

Source

from typing import NewType

def make_new_type(t):
    new_type = NewType(f'IntRange_{t}', t)
    print(new_type)

Sanity check

% python3 -c 'from a import make_new_type; make_new_type("list")'
<function NewType.<locals>.new_type at 0x100a37040>

Traceback

% pylint a.py
Exception on node <Assign l.4 at 0x106b6d190> in file '/Users/.../pylint/a.py'
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/astroid/inference_tip.py", line 28, in _inference_tip_cached
    result = _cache[func, node]
KeyError: (<function infer_typing_typevar_or_newtype at 0x106654040>, <Call l.4 at 0x106b6d0d0>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/astroid/builder.py", line 181, in _data_build
    node, parser_module = _parse_string(data, type_comments=True)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/astroid/builder.py", line 461, in _parse_string
    parsed = parser_module.parse(data + "\n", type_comments=type_comments)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/astroid/_ast.py", line 45, in parse
    return parse_func(string)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 10
    class f'IntRange_{t}(metaclass=Meta):
                                         ^
SyntaxError: EOL while scanning string literal

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

Traceback (most recent call last):
  File "/Users/.../pylint/pylint/utils/ast_walker.py", line 72, in walk
    callback(astroid)
  File "/Users/.../pylint/pylint/extensions/redefined_variable_type.py", line 110, in visit_assign
    _type = node_type(node.value)
  File "/Users/.../pylint/pylint/checkers/utils.py", line 1325, in node_type
    for var_type in node.infer():
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/astroid/nodes/node_ng.py", line 140, in infer
    results = list(self._explicit_inference(self, context, **kwargs))
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/wrapt/wrappers.py", line 566, in __call__
    return self._self_wrapper(self.__wrapped__, self._self_instance,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/astroid/inference_tip.py", line 30, in _inference_tip_cached
    result = _cache[func, node] = list(func(*args, **kwargs))
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/astroid/brain/brain_typing.py", line 132, in infer_typing_typevar_or_newtype
    node = extract_node(TYPING_TYPE_TEMPLATE.format(typename))
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/astroid/builder.py", line 438, in extract_node
    tree = parse(code, module_name=module_name)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/astroid/builder.py", line 296, in parse
    return builder.string_build(code, modname=module_name, path=path)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/astroid/builder.py", line 155, in string_build
    module = self._data_build(data, modname, path)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/astroid/builder.py", line 183, in _data_build
    raise AstroidSyntaxError(
astroid.exceptions.AstroidSyntaxError: Parsing Python code failed:
EOL while scanning string literal (<unknown>, line 10)
************* Module a
a.py:1:0: F0002: a.py: Fatal error while checking 'a.py'. Please open an issue in our bug tracker so we address this. There is a pre-filled template that you can use in '/Users/.../Library/Caches/pylint/pylint-crash-2022-02-18-22.txt'. (astroid-error)

------------------------------------------------------------------
Your code has been rated at 0.00/10 (previous run: 0.00/10, +0.00)

Version

pylint 2.13.0-dev0
astroid 2.9.2
Python 3.9.10 (v3.9.10:f2f3f53782, Jan 13 2022, 16:55:45) 
[Clang 13.0.0 (clang-1300.0.29.30)]

@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.13.0 milestone Feb 19, 2022
@Pierre-Sassoulas Pierre-Sassoulas removed the Needs investigation 🔬 A bug or crash where it's not immediately obvious what is happenning label Feb 19, 2022
@DanielNoord
Copy link
Collaborator

@jacobtylerwalls Would you be willing to already open a PR with a test for this? This is the last issue for 2.13 without an already opened PR 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Crash 💥 A bug that makes pylint crash
Projects
None yet
4 participants