Skip to content

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

Closed

Description

@ChamNouki

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

Activity

  1. hauntsaninja commented on Aug 1, 2020

    @hauntsaninja
    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!

  2. ChamNouki commented on Aug 3, 2020

    @ChamNouki
    Author

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions