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

Pylint crashes with an invalid TypeVar definition #8802

Closed
yilei opened this issue Jun 26, 2023 · 2 comments · Fixed by pylint-dev/astroid#2239
Closed

Pylint crashes with an invalid TypeVar definition #8802

yilei opened this issue Jun 26, 2023 · 2 comments · Fixed by pylint-dev/astroid#2239
Labels
Crash 💥 A bug that makes pylint crash Needs astroid update Needs an astroid update (probably a release too) before being mergable Needs PR This issue is accepted, sufficiently specified and now needs an implementation typing
Milestone

Comments

@yilei
Copy link
Contributor

yilei commented Jun 26, 2023

Bug description

Example code:

from typing import TypeVar

MyType = TypeVar('My.Type')

Configuration

No response

Command used

pylint a.py

Pylint output

************* Module a
a.py:4:0: C0305: Trailing newlines (trailing-newlines)
a.py:1:0: C0114: Missing module docstring (missing-module-docstring)
Exception on node <AssignName.MyType l.3 at 0x7f9f93ce5360> in file 'a.py'
Traceback (most recent call last):
  File "astroid/inference_tip.py", line 33, in _inference_tip_cached
    result = _cache[func, node]
KeyError: (<function infer_typing_typevar_or_newtype at 0x7f9f9449c820>, <Call l.3 at 0x7f9f93ce55a0>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "astroid/builder.py", line 181, in _data_build
    node, parser_module = _parse_string(data, type_comments=True)
  File "astroid/builder.py", line 484, in _parse_string
    parsed = parser_module.parse(data + "\n", type_comments=type_comments)
  File "astroid/_ast.py", line 50, in parse
    return parse_func(string)
  File "ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 10
    class My.Type(metaclass=Meta):
            ^
SyntaxError: invalid syntax

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

Traceback (most recent call last):
  File "pylint/checkers/utils.py", line 1371, in safe_infer
    value = next(infer_gen)
  File "astroid/nodes/node_ng.py", line 161, in infer
    results = list(self._explicit_inference(self, context, **kwargs))
  File "astroid/inference_tip.py", line 40, in _inference_tip_cached
    result = _cache[func, node] = list(func(*args, **kwargs))
  File "astroid/brain/brain_typing.py", line 140, in infer_typing_typevar_or_newtype
    node = extract_node(TYPING_TYPE_TEMPLATE.format(typename))
  File "astroid/builder.py", line 450, in extract_node
    tree = parse(code, module_name=module_name)
  File "astroid/builder.py", line 307, in parse
    return builder.string_build(code, modname=module_name, path=path)
  File "astroid/builder.py", line 151, in string_build
    module, builder = self._data_build(data, modname, path)
  File "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 "pylint/utils/ast_walker.py", line 91, in walk
    callback(astroid)
  File "pylint/checkers/base/name_checker/checker.py", line 416, in visit_assignname
    inferred_assign_type = utils.safe_infer(assign_type.value)
  File "pylint/checkers/utils.py", line 1375, in safe_infer
    raise AstroidError from e
astroid.exceptions.AstroidError
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 '~/.cache/pylint/pylint-crash-2023-06-26-13-55-46.txt'. (astroid-error)

-----------------------------------
Your code has been rated at 0.00/10

Expected behavior

It should not crash.

Pylint version

pylint 2.17.4
astroid 2.15.5
Python 3.10.8 (main, Mar  9 2023, 10:11:52) [GCC 12.2.0]

OS / Environment

No response

Additional dependencies

No response

@yilei yilei added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jun 26, 2023
@jacobtylerwalls jacobtylerwalls added Crash 💥 A bug that makes pylint crash typing Needs astroid update Needs an astroid update (probably a release too) before being mergable Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Jun 26, 2023
@jacobtylerwalls
Copy link
Member

Thanks for the report!

@mbyrnepr2
Copy link
Member

I've pushed a potential fix for this. I think it isn't an invalid definition per se. Python and mypy are ok with it and in the case of mypy, it points out that the argument name does not match the variable name, which is what Pylint should do also.
Perhaps your debug output is because of the older version of Pylint - I haven't looked into that.

@jacobtylerwalls jacobtylerwalls added this to the 2.17.5 milestone Jul 6, 2023
mbyrnepr2 added a commit to mbyrnepr2/astroid that referenced this issue Jul 13, 2023
mbyrnepr2 added a commit to pylint-dev/astroid that referenced this issue Jul 17, 2023
github-actions bot pushed a commit to pylint-dev/astroid that referenced this issue Jul 17, 2023
Pierre-Sassoulas pushed a commit to pylint-dev/astroid that referenced this issue Jul 17, 2023
…peVar` call. (#2254)

* Fix a crash when inferring a `typing.TypeVar` call. (#2239)

Closes pylint-dev/pylint#8802

(cherry picked from commit 89dfb48)

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Mark Byrne <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@jacobtylerwalls jacobtylerwalls modified the milestones: 2.17.5, 2.17.6 Jul 26, 2023
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 Needs astroid update Needs an astroid update (probably a release too) before being mergable Needs PR This issue is accepted, sufficiently specified and now needs an implementation typing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants