mypy icon indicating copy to clipboard operation
mypy copied to clipboard

mypy does not recognize os.name

Open amatyukhin0 opened this issue 3 years ago • 1 comments

Bug Report

mypy respects platform conditions using sys.platform variable, but do not recognize os.name.

Related issue: #8166

To Reproduce

Consider this piece of code, assuming we are on Linux:

import os
import signal

if os.name == 'nt':
    signal_type = signal.SIGBREAK
else:
    signal_type = signal.SIGTERM

Expected Behavior

mypy should not raise any error.

Actual Behavior

mypy fails with:

main.py:5: error: Module has no attribute "SIGBREAK"
Found 1 error in 1 file (checked 1 source file)

Note that replacing os.name == 'nt' with sys.platform == 'win32' fixes this issue.

Environment

  • Mypy version used: 0.961
  • Python version used: 3.10
  • Operating system: Linux

amatyukhin0 avatar Jun 21 '22 07:06 amatyukhin0

Similar to https://github.com/python/mypy/issues/8166#issuecomment-1774204090 This is understood by the Pylance language server (and I assume pyright, although I haven't properly tested)

setuptools for example, has this snippet of code, which causes error: Cannot find implementation or library stub for module named "dl" [import-not-found] on Windows:

have_rtld = False
use_stubs = False

if sys.platform == "darwin":
    use_stubs = True
elif os.name != 'nt':
    try:
        import dl # type: ignore[import-not-found] # https://github.com/python/mypy/issues/13002

        use_stubs = have_rtld = hasattr(dl, 'RTLD_NOW')
    except ImportError:
        pass

Avasam avatar Feb 21 '24 23:02 Avasam