python-lsp / python-lsp-server

Fork of the python-language-server project, maintained by the Spyder IDE team and the community
MIT License
1.76k stars 186 forks source link

Error in Pyflakes plugin: 'NoneType' has no len() #429

Closed elelay closed 10 months ago

elelay commented 10 months ago

Given the file XML.props containing

# :mode=properties:folding=explicit:encoding=Native2Ascii:

Following exceptions occurs:

  File "/usr/lib/python3.11/site-packages/pyflakes/api.py", line 39, in check
    tree = ast.parse(codeString, filename=filename)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/elelay/jedit/XMLGit/XML.props", line 0
SyntaxError: unknown encoding: Native2Ascii

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.11/site-packages/pylsp/config/config.py", line 33, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/pluggy/_manager.py", line 418, in traced_hookexec
    return outcome.get_result()
           ^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/pluggy/_result.py", line 108, in get_result
    raise exc.with_traceback(exc.__traceback__)
  File "/usr/lib/python3.11/site-packages/pluggy/_result.py", line 70, in from_call
    result = func()
             ^^^^^^
  File "/usr/lib/python3.11/site-packages/pluggy/_manager.py", line 415, in <lambda>
    lambda: oldcall(hook_name, hook_impls, caller_kwargs, firstresult)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/pluggy/_callers.py", line 116, in _multicall
    raise exception.with_traceback(exception.__traceback__)
  File "/usr/lib/python3.11/site-packages/pluggy/_callers.py", line 80, in _multicall
    res = hook_impl.function(*args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/pylsp/plugins/pyflakes_lint.py", line 26, in pylsp_lint
    pyflakes_api.check(document.source.encode('utf-8'), document.path, reporter=reporter)
  File "/usr/lib/python3.11/site-packages/pyflakes/api.py", line 41, in check
    reporter.syntaxError(filename, e.args[0], e.lineno, e.offset, e.text)
  File "/usr/lib/python3.11/site-packages/pylsp/plugins/pyflakes_lint.py", line 55, in syntaxError
    'end': {'line': lineno - 1, 'character': offset + len(text)},
                                                      ^^^^^^^^^
TypeError: object of type 'NoneType' has no len()

The second one should be avoided by checking in syntaxError that text is not None.

ccordoba12 commented 10 months ago

Hey @elelay, thanks for reporting. You said:

The second one should be avoided by checking in syntaxError that text is not None.

I agree. Could you send a pull request to fix this?

elelay commented 10 months ago

Thanks for the quick fix, sorry I had no time to get to it...

ccordoba12 commented 10 months ago

No worries, that's fine.