SonarCloud reports "Remove this identity check: it will always be False" (S5727) erroneously

I’m using sonarcloud.io. The affected code snippet is:

        if args.exec is not None:
            status = os.system(args.exec)
        elif args.command is not None:
            status = os.system(" ".join(args.command))
        # The return value of os.system is not simply the exit code of the process
        # see: https://mail.python.org/pipermail/python-list/2003-May/207712.html
        if status is None:
            sys.exit(0)
        if status % 256 == 0:
            sys.exit(status//256)
        sys.exit(status % 256)

SonarCloud is reporting that if status is None is invalid because it believes that status can never be None. However, as noted in the comments, it can be None as returned from os.system.

Hi @Philip_Colmer,

Thank you for reporting this problem.

We currently retrieve the return type from Typeshed, which is used by most type checkers (Mypy, pycharm, etc…) so we will have to patch this repository.

However before creating a ticket I would like to understand in which context os.system can return None. Could you give an example? Is it specific to an operating system? The link you shared says

Please note that wait, system and the like have quite a tendency to return None
if the command ran to completion without problem

However when I look at python documentation and the implementation of os.system in CPython it looks like the function can only return numeric values. Maybe I am missing something.

Nicolas

Hi @Nicolas_Harraudeau

That code was contributed to my project by an external contributor. Since they had linked to the original article, I took it on faith that this was correct information. Reading followup replies to the thread got me to this page:

https://mail.python.org/pipermail/python-list/2003-May/221321.html

which cites an example of popen returning None as the exit status. The next post in the thread is please help me understand os.system() result which says:

Note that POSIX does not specify the return value of the C system()
function, so the return value of the python function is system
dependent.

I hope that helps.

Philip

Hi @Philip_Colmer,

Thanks for the update. Typeshed correctly marks the return type of popen(...).close() as Optional[int], i.e. possibly None.

Regarding os.system, other people in the thread you mention are also surprised by the claim that None could be returned. The fact that the return value is “system dependent” does not mean that None could be returned. I understand it as “the same returned values might have different meanings on different operating systems”.

For now I’ll suppose that Typeshed is right as I couldn’t find any command returning None with os.system.

Nicolas