mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Add `--strict-bool` flag to prohib treating `bool` as `int`

Open sobolevn opened this issue 1 year ago • 8 comments

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 https://github.com/python/mypy/issues/8363

sobolevn avatar Jul 05 '24 12:07 sobolevn

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.

davidhalter avatar Jul 05 '24 12:07 davidhalter

Diff from mypy_primer, showing the effect of this PR on open source code:

pip (https://github.com/pypa/pip)
+ src/pip/_internal/operations/install/wheel.py:612: error: Argument "quiet" to "compile_file" has incompatible type "bool"; expected "int"  [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:777: 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:786: 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:795: 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:804: 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:83: error: Argument 1 to "TextIOWrapper" has incompatible type "BinaryIO"; expected "_WrappedBuffer"  [arg-type]
+ pylint/lint/pylinter.py:83: note: Following member(s) of "BinaryIO" have conflicts:
+ pylint/lint/pylinter.py:83: note:     Expected:
+ pylint/lint/pylinter.py:83: note:         def isatty(self) -> int
+ pylint/lint/pylinter.py:83: note:     Got:
+ pylint/lint/pylinter.py:83: 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) ...

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]

mongo-python-driver (https://github.com/mongodb/mongo-python-driver)
+ pymongo/synchronous/pool.py:835: error: Argument 3 to "setsockopt" of "socket" has incompatible type "bool"; expected "int | Buffer"  [arg-type]
+ pymongo/asynchronous/pool.py:837: 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

websockets (https://github.com/aaugustin/websockets)
+ src/websockets/sync/server.py:451: 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]

github-actions[bot] avatar Jul 05 '24 12:07 github-actions[bot]

Shouldn't this be done at the level of typeshed stubs?

You can't have conditional stubs with --strict-bool and --no-strict-bool options.

(and faster) than additional branching in every subtype check

The check is a constant time check. Compared to all other machinery the performance impact is insignificant.

Alternatively it would probably also possible to change the mro during semantic analysis.

This could also affect other parts of the equasion like: attribute access, super() calls, etc.

This is the safest bet.

sobolevn avatar Jul 05 '24 12:07 sobolevn

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

github-actions[bot] avatar Jul 23 '24 09:07 github-actions[bot]

There are too many issues to make it enabled by default :(

sobolevn avatar Jul 23 '24 09:07 sobolevn

@sobolevn

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."

wyattscarpenter avatar Oct 09 '24 04:10 wyattscarpenter

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.

gandhis1 avatar Oct 09 '24 12:10 gandhis1

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 isatty look like something could be fixed somewhere.

JelleZijlstra avatar Oct 09 '24 12:10 JelleZijlstra

@sobolevn, @JelleZijlstra, et al: typeshed has progressed since last year; the current mypy_primer diff for adding strict-bool to strict is about the same size, but seems (per my limited investigation) to be things caught in user code, rather than typshed . I checked it here: https://github.com/wyattscarpenter/mypy/pull/6/ . Feel free to use the merge resolution I made in order to bring the PR up to the present, if you wish — it also fixes a couple typos.

(I also understand better now what is meant by "enabled by default" (ie: enabled without any flags), and investigated that in https://github.com/wyattscarpenter/mypy/pull/7 )

wyattscarpenter avatar May 09 '25 03:05 wyattscarpenter