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

AttributeError on Python 3.10 #1372

Closed
jaraco opened this issue Aug 12, 2021 · 3 comments · Fixed by #1374
Closed

AttributeError on Python 3.10 #1372

jaraco opened this issue Aug 12, 2021 · 3 comments · Fixed by #1374
Assignees
Milestone

Comments

@jaraco
Copy link
Contributor

jaraco commented Aug 12, 2021

Checking a file with a syntax error on Python 3.10rc1 causes flake8 to crash with an AttributeError:

$ cat > foo.py
bad python:
^D
pmxbot main $ python3.10 -m pip-run flake8 -- -m flake8 foo.py
Collecting flake8
  Using cached flake8-3.9.2-py2.py3-none-any.whl (73 kB)
Collecting pycodestyle<2.8.0,>=2.7.0
  Using cached pycodestyle-2.7.0-py2.py3-none-any.whl (41 kB)
Collecting mccabe<0.7.0,>=0.6.0
  Using cached mccabe-0.6.1-py2.py3-none-any.whl (8.6 kB)
Collecting pyflakes<2.4.0,>=2.3.0
  Using cached pyflakes-2.3.1-py2.py3-none-any.whl (68 kB)
Installing collected packages: pyflakes, pycodestyle, mccabe, flake8
Successfully installed flake8-3.9.2 mccabe-0.6.1 pycodestyle-2.7.0 pyflakes-2.3.1
Traceback (most recent call last):
  File "/Users/jaraco/.local/pipx/venvs/flake8/lib/python3.10/site-packages/flake8/checker.py", line 478, in run_ast_checks
    ast = self.processor.build_ast()
  File "/Users/jaraco/.local/pipx/venvs/flake8/lib/python3.10/site-packages/flake8/processor.py", line 225, in build_ast
    return ast.parse("".join(self.lines))
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 1
    bad python:
    ^^^^^^^^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/jaraco/.local/bin/flake8", line 8, in <module>
    sys.exit(main())
  File "/Users/jaraco/.local/pipx/venvs/flake8/lib/python3.10/site-packages/flake8/main/cli.py", line 22, in main
    app.run(argv)
  File "/Users/jaraco/.local/pipx/venvs/flake8/lib/python3.10/site-packages/flake8/main/application.py", line 363, in run
    self._run(argv)
  File "/Users/jaraco/.local/pipx/venvs/flake8/lib/python3.10/site-packages/flake8/main/application.py", line 351, in _run
    self.run_checks()
  File "/Users/jaraco/.local/pipx/venvs/flake8/lib/python3.10/site-packages/flake8/main/application.py", line 264, in run_checks
    self.file_checker_manager.run()
  File "/Users/jaraco/.local/pipx/venvs/flake8/lib/python3.10/site-packages/flake8/checker.py", line 323, in run
    self.run_serial()
  File "/Users/jaraco/.local/pipx/venvs/flake8/lib/python3.10/site-packages/flake8/checker.py", line 307, in run_serial
    checker.run_checks()
  File "/Users/jaraco/.local/pipx/venvs/flake8/lib/python3.10/site-packages/flake8/checker.py", line 589, in run_checks
    self.run_ast_checks()
  File "/Users/jaraco/.local/pipx/venvs/flake8/lib/python3.10/site-packages/flake8/checker.py", line 480, in run_ast_checks
    row, column = self._extract_syntax_information(e)
  File "/Users/jaraco/.local/pipx/venvs/flake8/lib/python3.10/site-packages/flake8/checker.py", line 465, in _extract_syntax_information
    lines = physical_line.rstrip("\n").split("\n")
AttributeError: 'int' object has no attribute 'rstrip'

Checking the same file on Python 3.9 passes just fine:

pmxbot main $ python3.9 -m pip-run -q flake8 -- -m flake8 foo.py
WARNING: You are using pip version 21.2.2; however, version 21.2.3 is available.
You should consider upgrading via the '/Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9 -m pip install --upgrade pip' command.
foo.py:1:5: E999 SyntaxError: invalid syntax
@jaraco
Copy link
Contributor Author

jaraco commented Aug 12, 2021

This error is a particular nuisance because my editor (SublimeText) runs the linter every few seconds even when most of the time the syntax isn't yet correct (due to work in progress).

@sigmavirus24 sigmavirus24 self-assigned this Aug 13, 2021
@sigmavirus24
Copy link
Member

Python 3.9.6 (default, Jul 16 2021, 00:00:00)
[GCC 11.1.1 20210531 (Red Hat 11.1.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ast
>>> contents = """
... bad python:
... """
>>> err = None
>>> try:
...   ast.parse(contents)
... except SyntaxError as e:
...   err = e
...
>>> err
SyntaxError('invalid syntax', ('<unknown>', 2, 5, 'bad python:\n'))
>>> err.args[1][1:3]
(2, 5)
>>>
Python 3.10.0rc1 (default, Aug 13 2021, 09:23:08) [GCC 11.1.1 20210531 (Red Hat 11.1.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ast
>>> contents = """
... bad python:
... """
>>> err = None
>>> try:
...   ast.parse(contents)
... except SyntaxError as e:
...   err = e
...
>>> err
SyntaxError('invalid syntax. Perhaps you forgot a comma?', ('<unknown>', 2, 1, 'bad python:\n', 2, 11))
>>> err.args
('invalid syntax. Perhaps you forgot a comma?', ('<unknown>', 2, 1, 'bad python:\n', 2, 11))
>>> len(err.args)
2
>>> len(err.args[1])
6
>>> err.args[1][1:3]
(2, 1)
>>>

The core developers added in extra informatoin in the token info. I'm working on a fix

@jaraco
Copy link
Contributor Author

jaraco commented Aug 15, 2021

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants