-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add --strict-bool
flag to prohib treating bool
as int
#17487
base: master
Are you sure you want to change the base?
Conversation
Shouldn't this be done at the level of typeshed stubs? It seems like changing typeshed is probably better (and faster) than additional branching in every subtype check. Alternatively it would probably also possible to change the mro during semantic analysis. |
This comment has been minimized.
This comment has been minimized.
You can't have conditional stubs with
The check is a constant time check. Compared to all other machinery the performance impact is insignificant.
This could also affect other parts of the equasion like: attribute access, This is the safest bet. |
Diff from mypy_primer, showing the effect of this PR on open source code: websockets (https://github.com/aaugustin/websockets)
+ src/websockets/sync/server.py:456: error: Argument 3 to "setsockopt" of "socket" has incompatible type "bool"; expected "int | Buffer" [arg-type]
+ src/websockets/sync/client.py:263: error: Argument 3 to "setsockopt" of "socket" has incompatible type "bool"; expected "int | Buffer" [arg-type]
psycopg (https://github.com/psycopg/psycopg)
+ tests/_test_connection.py:61: error: Incompatible types in assignment (expression has type "bool", target has type "int") [assignment]
+ tests/_test_connection.py:62: error: Incompatible types in assignment (expression has type "bool", target has type "int") [assignment]
pip (https://github.com/pypa/pip)
+ src/pip/_internal/operations/install/wheel.py:618: error: Argument "quiet" to "compile_file" has incompatible type "bool"; expected "int" [arg-type]
bokeh (https://github.com/bokeh/bokeh)
+ src/bokeh/util/package.py:117:14: error: Argument 1 to "exit" has incompatible type "bool"; expected "str | int | None" [arg-type]
pywin32 (https://github.com/mhammond/pywin32)
+ adodbapi/adodbapi.py:44:15: error: Incompatible types in assignment (expression has type "bool", variable has type "int") [assignment]
boostedblob (https://github.com/hauntsaninja/boostedblob)
+ boostedblob/path.py:569: error: Incompatible return value type (got "bool", expected "int") [return-value]
+ boostedblob/path.py:570: error: Incompatible return value type (got "bool", expected "int") [return-value]
+ boostedblob/path.py:575: error: Incompatible return value type (got "bool", expected "int") [return-value]
pylint (https://github.com/pycqa/pylint)
+ pylint/lint/base_options.py:79: error: Dict entry 0 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:116: error: Dict entry 0 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:145: error: Dict entry 0 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:258: error: Dict entry 2 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:259: error: Dict entry 3 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:314: error: Dict entry 2 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:326: error: Dict entry 1 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:339: error: Dict entry 1 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:364: error: Dict entry 2 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:398: error: Dict entry 0 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:411: error: Dict entry 0 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:421: error: Dict entry 0 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:442: error: Dict entry 4 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:452: error: Dict entry 4 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:472: error: Dict entry 4 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:483: error: Dict entry 4 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:494: error: Dict entry 4 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:504: error: Dict entry 4 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:514: error: Dict entry 4 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:524: error: Dict entry 4 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:534: error: Dict entry 4 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:547: error: Dict entry 4 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:560: error: Dict entry 4 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:572: error: Dict entry 4 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:583: error: Dict entry 4 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:594: error: Dict entry 3 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/base_options.py:605: error: Dict entry 4 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/checkers/symilar.py:767: error: Dict entry 0 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/checkers/symilar.py:776: error: Dict entry 0 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/checkers/symilar.py:785: error: Dict entry 0 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/checkers/symilar.py:794: error: Dict entry 0 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/lint/pylinter.py:82: error: Argument 1 to "TextIOWrapper" has incompatible type "BinaryIO"; expected "_WrappedBuffer" [arg-type]
+ pylint/lint/pylinter.py:82: note: Following member(s) of "BinaryIO" have conflicts:
+ pylint/lint/pylinter.py:82: note: Expected:
+ pylint/lint/pylinter.py:82: note: def isatty(self) -> int
+ pylint/lint/pylinter.py:82: note: Got:
+ pylint/lint/pylinter.py:82: note: def isatty(self) -> bool
+ pylint/pyreverse/main.py:128: error: Dict entry 2 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/pyreverse/main.py:137: error: Dict entry 2 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/pyreverse/main.py:156: error: Dict entry 2 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
+ pylint/pyreverse/main.py:164: error: Dict entry 1 has incompatible type "str": "bool"; expected "str": "int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None" [dict-item]
... (truncated 17 lines) ...
mongo-python-driver (https://github.com/mongodb/mongo-python-driver)
+ pymongo/synchronous/pool.py:837: error: Argument 3 to "setsockopt" of "socket" has incompatible type "bool"; expected "int | Buffer" [arg-type]
+ pymongo/asynchronous/pool.py:839: error: Argument 3 to "setsockopt" of "socket" has incompatible type "bool"; expected "int | Buffer" [arg-type]
rich (https://github.com/Textualize/rich)
+ rich/style.py:152: error: Argument 1 to "sum" has incompatible type "tuple[bool, Literal[False, 2], Literal[False, 4], Literal[False, 8], Literal[False, 16], Literal[False, 32], Literal[False, 64], Literal[False, 128], Literal[False, 256], Literal[False, 512], Literal[False, 1024], Literal[False, 2048], Literal[False, 4096]]"; expected "Iterable[bool]" [arg-type]
+ rich/scope.py:76: error: List item 5 has incompatible type "bool"; expected "str | int | None" [list-item]
+ rich/scope.py:76: error: List item 6 has incompatible type "bool"; expected "str | int | None" [list-item]
+ rich/progress.py:1370: error: Argument 1 to "TextIOWrapper" has incompatible type "_Reader"; expected "_WrappedBuffer" [arg-type]
+ rich/progress.py:1370: note: Following member(s) of "_Reader" have conflicts:
+ rich/progress.py:1370: note: Expected:
+ rich/progress.py:1370: note: def isatty(self) -> int
+ rich/progress.py:1370: note: Got:
+ rich/progress.py:1370: note: def isatty(self) -> bool
python-chess (https://github.com/niklasf/python-chess)
+ chess/syzygy.py:1071: error: Argument 4 to "setup_pairs" of "Table" has incompatible type "bool"; expected "int" [arg-type]
+ chess/syzygy.py:1074: error: Argument 4 to "setup_pairs" of "Table" has incompatible type "bool"; expected "int" [arg-type]
+ chess/syzygy.py:1106: error: Argument 4 to "setup_pairs" of "Table" has incompatible type "bool"; expected "int" [arg-type]
+ chess/syzygy.py:1109: error: Argument 4 to "setup_pairs" of "Table" has incompatible type "bool"; expected "int" [arg-type]
+ chess/syzygy.py:1270: error: Argument 4 to "setup_pairs" of "Table" has incompatible type "bool"; expected "int" [arg-type]
+ chess/syzygy.py:1306: error: Argument 4 to "setup_pairs" of "Table" has incompatible type "bool"; expected "int" [arg-type]
+ chess/engine.py:2063: error: Argument 2 to "get" of "dict" has incompatible type "bool"; expected "int | str" [arg-type]
+ chess/engine.py:2417: error: Argument 2 to "get" of "dict" has incompatible type "bool"; expected "int | str" [arg-type]
werkzeug (https://github.com/pallets/werkzeug)
+ tests/test_wsgi.py:158: error: Argument 1 to "TextIOWrapper" has incompatible type "BufferedReader"; expected "_WrappedBuffer" [arg-type]
+ tests/test_wsgi.py:158: note: Following member(s) of "BufferedReader" have conflicts:
+ tests/test_wsgi.py:158: note: Expected:
+ tests/test_wsgi.py:158: note: def isatty(self) -> int
+ tests/test_wsgi.py:158: note: Got:
+ tests/test_wsgi.py:158: note: def isatty(self) -> bool
|
There are too many issues to make it enabled by default :( |
Why do you say this? Because of the mypy_primer diff? I don't know if there's official guidance here on how many is too many, but to me it looks like a small number of projects (a dozen or so out of the hundreds tested?) each have a small number of issues to fix, and the fix would be trivial for those projects to make (either alter a type signature to take bool as well, or change those bools to be ints). In my opinion, if this is a good feature (I don't know; maybe Python developers actually love thinking of true and false as subtypes of int, and we would merely be annoying them by making a rule against that — but I don't think of bools that way, and no one I know does*), then it would be fine to make it enabled by default. Or perhaps you mean something different. * For what it's worth, https://docs.python.org/3/library/stdtypes.html#boolean-type-bool says "In many numeric contexts, False and True behave like the integers 0 and 1, respectively. However, relying on this is discouraged; explicitly convert using int() instead." |
I echo @wyattscarpenter thoughts. This is a great feature and I think users of strict mode should have the inherent expectation of a higher standard of maintenance in their code bases as typing features and tools get upgraded. To be blunt, I've seen a lot worse Mypy primer than that one. |
I agree that the mypy-primer hits aren't a blocker to merging this as an off-by-default check. However, it might be worth looking at them to see whether there's anything we can fix in typeshed to make the experience better for users. For example, the werkzeug and rich hits around |
I went with an option (instead of an error code), because the was no specific error for this code.
I reuse existing rules / codes. The only change is a special case for
bool / int
subtyping check with an active flag.Closes #8363