typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

sys.exit and SystemExit

Open Akuli opened this issue 3 years ago • 0 comments

sys.exit() is defined as:

https://github.com/python/typeshed/blob/c68bcc7e69249956f358da7f47028d8fd1f7443e/stdlib/sys.pyi#L224-L225

The argument ends up in the .code attribute of a SystemExit exception.

>>> import sys
>>> try: sys.exit
... except SystemExit as e: print(repr(e.code))
KeyboardInterrupt
>>> try: sys.exit()
... except SystemExit as e: print(repr(e.code))
... 
None
>>> try: sys.exit("lol")
... except SystemExit as e: print(repr(e.code))
... 
'lol'
>>> try: sys.exit({"foo": "bar"})
... except SystemExit as e: print(repr(e.code))
... 
{'foo': 'bar'}

But that's typed as:

https://github.com/python/typeshed/blob/c68bcc7e69249956f358da7f47028d8fd1f7443e/stdlib/builtins.pyi#L1793-L1794

These should be the same IMO, but what should they be? The interpreter handles uncaught SystemExit exceptions like this:

  • If the code is None, exit the process with status zero.
  • If the code is an integer, exit the process with that status.
  • Otherwise, print the code (to stderr) and exit the process with status 1.

I'd suggest using str | int | None, where we use str because we don't have any way to express "anything reasonably printable" without saying "any object". This way you can use each of the interpreter's behaviors as intended, and if you accidentally pass in e.g. 0.0, the type checker will warn you instead of making you wonder why your process printed 0.0 and failed.

Akuli avatar Aug 09 '22 19:08 Akuli