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

[BUG] Type ignore annotation used on operating system specific code #9242

Closed
ChamNouki opened this issue Aug 1, 2020 · 2 comments
Closed

Comments

@ChamNouki
Copy link

ChamNouki commented Aug 1, 2020

Hello here,

I try to type check the following code using mypy. As there are specific operating system lines of code, I have to tell to mypy to ignore them.

def is_user_admin() -> bool:
    try:
        system = platform.system()

        if system in ['Darwin', 'Linux']:
            return os.getuid() == 0  # type: ignore[attr-defined, no-any-return]

        if system == 'Windows':
            return ctypes.windll.shell32.IsUserAnAdmin() == 1  # type: ignore[attr-defined, no-any-return]

    except AttributeError:
        logger.info(f"Failed to determine if current user is an admin on {system} operating system.")

    raise AdminStateUnknownException

As expected, types are ignored but I have the following error (which seems to be legit too) running the command python -m mypy --strict admin.py

mumgram/admin.py:15: error: unused 'type: ignore' comment
                return os.getuid() == 0  # type: ignore[attr-defined, no-any-return]
                ^
Found 1 error in 1 file (checked 1 source file)

Obviously I need those annotations cause we have developpers using Windows and other using OSX/Linux.

I try to use the --warn-unused-ignore parameter for the command (python -m mypy --strict admin.py --warn-unused-ignore) but unfortunatly that doesn't seems to work and the error is still an error (breaking my CI 😱 !).

I use Python 3.7.7 64-bit and mypy 0.782

@hauntsaninja
Copy link
Collaborator

If you rewrite your checks to use sys.platform, mypy will understand that. (I think there's already an open issue about recognising platform.system as well).

If you're unwilling to do that or unable to make it work, the part about --warn-unused-ignore is basically a dupe of #8823 or #8990. As described in those issues, there are workarounds involving using cast or if TYPECHECKING.

I'm closing since I believe everything here is covered by existing issues, but feel free to reply if you need help!

@ChamNouki
Copy link
Author

ChamNouki commented Aug 3, 2020

Writing my function with sys.platform seems to work perfectly !

def is_user_admin() -> bool:
    try:
        if sys.platform == 'darwin' or sys.platform == 'linux':
            return os.getuid() == 0

        if sys.platform == 'win32' or sys.platform == 'cygwin':
            return ctypes.windll.shell32.IsUserAnAdmin() == 1

    except AttributeError:
        logger.info(f"Failed to determine if current user is an admin on {sys.platform} operating system.")

    raise AdminStateUnknownException

Thanks a lot for your help ! Really like mypy, you're doing a great job ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants