-
We have a function which returns a string for identifying the user, which will return the user's actual name if possible, and their username if not. The function looks like: import getpass
def get_author() -> str:
try:
from win32api import GetUserNameEx
return GetUserNameEx(3)
except Exception:
return getpass.getuser() The
I've tried refactoring the code to: import getpass
import sys
def get_author() -> str:
if sys.platform in ["win32", "cygwin"]:
try:
from win32api import GetUserNameEx
return GetUserNameEx(3)
except Exception:
pass
return getpass.getuser() But we still get the warning on Linux. What is the suggested way to write this code to avoid any warnings from Pyright? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 9 replies
-
Your refactored code is on the right path, but you're using a conditional expression form that type checkers do not recognize. If you change it to As an alternative, you can add a |
Beta Was this translation helpful? Give feedback.
Your refactored code is on the right path, but you're using a conditional expression form that type checkers do not recognize. If you change it to
if sys.platform == "win32" or sys.platform == "cygwin":
, you'll get the effect you're looking for. Type checkers are required by the typing standard to recognize a few specific conditional forms when checking for platforms and Python versions. If you use some alternative form, it won't be recognized.As an alternative, you can add a
# pyright: ignore[reportMissingModuleSource]
to suppress the warning on that line. Or you can add a# pyright: reportMissingModuleSource=false
at the top of the file to suppress it for the entire file.