pythonvisual-studio-codesuppress-warningspylance

VSCode Pylance false-positive (?) reaction on ImportError


I am trying to write cross-platform app in python. I want to use uvloop (only if possible), using this code:

import asyncio

try:
    import uvloop
    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
except ImportError:
    pass

However, pylance shows message that "Import "uvloop" could not be resolved" in Windows (because uvloop is not available for this OS, so that's okay). It's not a big deal, everything works, but this message is annoying. I can set ignore rule, but I want to keep other unresolved import warnings, because in other cases they are pretty useful. How can I tell Pylance, that in this specific case I'm okay with it?


Solution

  • Set an inline ignore statement:

    import asyncio
    
    
    try:
        import uvloop # type: ignore
        asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
    except ImportError:
        pass
    

    It'd be better to be more specific with the rule you want to ignore. The above will ignore all type checking errors.

    https://github.com/microsoft/pylance-release/issues/196#issuecomment-668099106