pylint-dev/pylint

Crash from typing import Optional

domist07 opened this issue · 2 comments

Bug description

Pylint crashes when a file contains:

from typing import Optional

Command used

python -m pylint src/

Pylint output

pylint crashed with a ``AstroidError`` and with the following stacktrace:

Traceback (most recent call last):
  File "env\Lib\site-packages\pylint\checkers\imports.py", line 846, in _get_imported_module
    return importnode.do_import_module(modname)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "env\Lib\site-packages\astroid\nodes\_base_nodes.py", line 146, in do_import_module
    return mymodule.import_module(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "env\Lib\site-packages\astroid\nodes\scoped_nodes\scoped_nodes.py", line 527, in import_module
    return AstroidManager().ast_from_module_name(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "env\Lib\site-packages\astroid\manager.py", line 232, in ast_from_module_name
    return self.ast_from_file(found_spec.location, modname, fallback=False)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "env\Lib\site-packages\astroid\manager.py", line 124, in ast_from_file
    return AstroidBuilder(self).file_build(filepath, modname)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "env\Lib\site-packages\astroid\builder.py", line 144, in file_build
    module, builder = self._data_build(data, modname, path)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "env\Lib\site-packages\astroid\builder.py", line 204, in _data_build
    module = builder.visit_module(node, modname, node_file, package)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "env\Lib\site-packages\astroid\rebuilder.py", line 254, in visit_module
    [self.visit(child, newnode) for child in node.body],
     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "env\Lib\site-packages\astroid\rebuilder.py", line 603, in visit
    visit_method = getattr(self, visit_name)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'TreeRebuilder' object has no attribute 'visit_typealias'

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

Traceback (most recent call last):
  File "env\Lib\site-packages\pylint\lint\pylinter.py", line 798, in _lint_file
    check_astroid_module(module)
  File "env\Lib\site-packages\pylint\lint\pylinter.py", line 1067, in check_astroid_module
    retval = self._check_astroid_module(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "env\Lib\site-packages\pylint\lint\pylinter.py", line 1117, in _check_astroid_module
    walker.walk(node)
  File "env\Lib\site-packages\pylint\utils\ast_walker.py", line 94, in walk
    self.walk(child)
  File "env\Lib\site-packages\pylint\utils\ast_walker.py", line 91, in walk
    callback(astroid)
  File "env\Lib\site-packages\pylint\checkers\imports.py", line 541, in visit_importfrom
    imported_module = self._get_imported_module(node, basename)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "env\Lib\site-packages\pylint\checkers\imports.py", line 871, in _get_imported_module
    raise astroid.AstroidError from e
astroid.exceptions.AstroidError

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

Traceback (most recent call last):
  File "env\Lib\site-packages\pylint\lint\pylinter.py", line 763, in _lint_files
    self._lint_file(fileitem, module, check_astroid_module)
  File "env\Lib\site-packages\pylint\lint\pylinter.py", line 800, in _lint_file
    raise astroid.AstroidError from e
astroid.exceptions.AstroidError

Expected behavior

no crash.

Pylint version

pylint 2.16.2
astroid 2.14.2
Python 3.12.2 | packaged by Anaconda, Inc. | (main, Feb 27 2024, 17:28:07) [MSC 
v.1916 64 bit (AMD64)]

OS / Environment

Win 10 Enterprise
10.0.19045

Additional dependencies

No response

Hi @domist07 - there is a rationale for this behaviour here. Since you're using Python 3.12, a Pylint version of 3.x is required.

Oh, thanks. I do not know why my package manager downloads this old version 🤔. With the new version, all works fine 😊