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

from pygments.lexers import DiffLexer unknown #1032

Closed
eddy-geek opened this issue Mar 11, 2021 · 2 comments
Closed

from pygments.lexers import DiffLexer unknown #1032

eddy-geek opened this issue Mar 11, 2021 · 2 comments
Labels
dynamic behavior waiting for upstream Waiting for upstream to release a fix

Comments

@eddy-geek
Copy link

Environment data

  • Language Server version: XXX
  • OS and version: XXX
  • Python version (& distribution if applicable, e.g. Anaconda): XXX

Expected behaviour

Typechecking pygments code (pip install pygments) should work:

from pygments.lexers import DiffLexer
from pygments.formatters import HtmlFormatter

Actual behaviour

prints DiffLexer is unknow import symbol

Code Snippet / Additional information

See the pygments/lexers/init.py:

from pygments.lexers._mapping import LEXERS
__all__ = ['get_lexer_by_name', 'get_lexer_for_filename', 'find_lexer_class',
           'guess_lexer', 'load_lexer_from_file'] + list(LEXERS) + list(COMPAT)

#...

class _automodule(types.ModuleType):
    """Automatically import lexers."""
    #...
oldmod = sys.modules[__name__]
newmod = _automodule(__name__)
newmod.__dict__.update(oldmod.__dict__)
sys.modules[__name__] = newmod
...

I understand you might not want to support the sys.modules[__name__] = newmod hack, but it seems to me that just parsing the __ALL__ line (and using it as a heuristic for what can be imported, I know it can be different) might go a long way here.

@erictraut
Copy link
Contributor

This library is using a dynamic expression to define __ALL__ — one that static type checkers like pylance do not understand. There are a limited number of forms of __ALL__ that static type checkers honor. The list can be found here. The maintainers of pygments will need to stick to this list if they want their library to work with static type checkers.

In the meantime, you can work around the problem by importing the module one level deeper:
from pygments.lexers.diff import DiffLexer

@judej
Copy link
Contributor

judej commented Mar 8, 2022

Closing this in this repo since the work is not here in Pylance.

@judej judej closed this as completed Mar 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dynamic behavior waiting for upstream Waiting for upstream to release a fix
Projects
None yet
Development

No branches or pull requests

4 participants