-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Closed
Description
sys.exit() is defined as:
Lines 224 to 225 in c68bcc7
| # sys.exit() accepts an optional argument of anything printable | |
| def exit(__status: object = ...) -> NoReturn: ... |
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))
...
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:
Lines 1793 to 1794 in c68bcc7
| class SystemExit(BaseException): | |
| code: int |
These should be the same IMO, but what should they be? The interpreter handles uncaught SystemExit exceptions like this:
- If the
codeisNone, exit the process with status zero. - If the
codeis an integer, exit the process with that status. - Otherwise, print the
code(to stderr) and exit the process with status1.
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.
AlexWaygood, JelleZijlstra, hauntsaninja, gadhagod, anpawo and 4 more0x36D76289
Metadata
Metadata
Assignees
Labels
No labels