Here’s a code block with all the syntax warnings (slightly edited):
So, what’s literally always incorrect here?
- Literal divided by zero (currently not a warning):
Equivalent to:1 / 0raise ZeroDivisionError("division by zero") - Return that’s overwritten in a finally block (warning in a different place):
Equivalent to:try: return 1 # <- warn here finally: return 2 # <- not heretry: pass finally: return 2 - Assert with parentheses:
Equivalent to:assert (x, "msg")pass - Invalid operations on displays:
Equivalent to:{}() {1}[1] "a"["b"]raise TypeError("'dict' object is not callable") raise TypeError("'set' object is not subscriptable") raise TypeError("string indices must be integers, not 'str'")
What should display a warning anyway?
- Ambiguous syntax:
Equivalent to:x = [y + 0x1for y in [1]]x = [y + 0x1f or y in [1]] - Strict equality (only works with small numbers):
Equivalent to:x = y is 1x = y == 1 and isinstance(y, int) - Single backslashes (only works with invalid escape sequences):
Equivalent to:x = "\99" x = f"\{1}"x = "\\99" x = f"\\{1}"
And what’s more controversial:
- Supressing exceptions in finally:
Equivalent to:try: input() # Ctrl+C finally: return 1try: input() # Ctrl+C except: pass return 1