Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Jul 5, 2024

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

@davidhalter
Copy link

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.

@sobolevn
Copy link
Member Author

sobolevn commented Jul 5, 2024

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.

Copy link
Contributor

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
@sobolevn
Copy link
Member Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants