Skip to content

Conversation

@huonw
Copy link

@huonw huonw commented Oct 7, 2024

Fixed #17892

This implements a new error code optional-non-truthy that catches bugs like

def bug(x: None | str):
    if x:
        print(f"the str value is {x}") 
    else:
        print("the value is None") # oops! x could also be `""`

This is heavily inspired by the checks done in check_for_truthy_type for truthy-iterable, truthy-bool etc.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

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

check-jsonschema (https://github.com/python-jsonschema/check-jsonschema)
+ src/check_jsonschema/cli/parse_result.py:49: error: Generator has incompatible item type "int"; expected "bool"  [misc]

sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/util/http_date.py: note: In function "rfc1123_to_epoch":
+ sphinx/util/http_date.py:47:16: error: Unsupported operand types for - ("float" and "int")  [operator]

koda-validate (https://github.com/keithasaurus/koda-validate)
+ koda_validate/serialization/json_schema.py:95: error: Unsupported operand types for + ("list[Predicate[str]]" and "list[PredicateAsync[str]]")  [operator]
+ koda_validate/serialization/json_schema.py:105: error: Unsupported operand types for + ("list[Predicate[bytes]]" and "list[PredicateAsync[bytes]]")  [operator]
+ koda_validate/serialization/json_schema.py:115: error: Unsupported operand types for + ("list[Predicate[int]]" and "list[PredicateAsync[int]]")  [operator]
+ koda_validate/serialization/json_schema.py:125: error: Unsupported operand types for + ("list[Predicate[Decimal]]" and "list[PredicateAsync[Decimal]]")  [operator]
+ koda_validate/serialization/json_schema.py:134: error: Unsupported operand types for + ("list[Predicate[float]]" and "list[PredicateAsync[float]]")  [operator]
+ koda_validate/serialization/json_schema.py:143: error: Unsupported operand types for + ("list[Predicate[date]]" and "list[PredicateAsync[date]]")  [operator]
+ koda_validate/serialization/json_schema.py:152: error: Unsupported operand types for + ("list[Predicate[datetime]]" and "list[PredicateAsync[datetime]]")  [operator]
+ koda_validate/serialization/json_schema.py:169: error: Unsupported operand types for + ("list[Predicate[bool]]" and "list[PredicateAsync[bool]]")  [operator]
+ koda_validate/serialization/json_schema.py:178: error: Unsupported operand types for + ("list[Predicate[UUID]]" and "list[PredicateAsync[UUID]]")  [operator]
+ koda_validate/serialization/json_schema.py:294: error: Unsupported operand types for + ("list[Predicate[dict[Any, Any]]]" and "list[PredicateAsync[dict[Any, Any]]]")  [operator]

streamlit (https://github.com/streamlit/streamlit)
+ lib/streamlit/runtime/pages_manager.py: note: In member "get_initial_active_script" of class "PagesStrategyV1":
+ lib/streamlit/runtime/pages_manager.py:98:21: error: Argument 1 to "filter" has incompatible type "Callable[[Any], Optional[bool]]"; expected "Callable[[PageInfo], TypeGuard[Optional[PageInfo]]]"  [arg-type]
+ lib/streamlit/runtime/pages_manager.py: note: In member "get_page_script" of class "PagesStrategyV2":
+ lib/streamlit/runtime/pages_manager.py:180:21: error: Argument 1 to "filter" has incompatible type "Callable[[Any], Optional[bool]]"; expected "Callable[[PageInfo], TypeGuard[Optional[PageInfo]]]"  [arg-type]

rich (https://github.com/Textualize/rich)
+ rich/_ratio.py:50: error: Generator has incompatible item type "int"; expected "bool"  [misc]
+ rich/style.py:170: error: Argument 1 to "sum" has incompatible type "tuple[Literal[1, 0], Literal[2, 0], Literal[4, 0], Literal[8, 0], Literal[16, 0], Literal[32, 0], Literal[64, 0], Literal[128, 0], Literal[256, 0], Literal[512, 0], Literal[1024, 0], Literal[2048, 0], Literal[4096, 0]]"; expected "Iterable[bool]"  [arg-type]
+ rich/segment.py:381: error: Generator has incompatible item type "int"; expected "bool"  [misc]

freqtrade (https://github.com/freqtrade/freqtrade)
+ freqtrade/persistence/trade_model.py:775: error: List comprehension has incompatible type List[float]; expected List[bool]  [misc]
+ freqtrade/wallets.py:108: error: Generator has incompatible item type "float"; expected "bool"  [misc]
+ freqtrade/plugins/protections/low_profit_pairs.py:63: error: Generator has incompatible item type "float"; expected "bool"  [misc]

core (https://github.com/home-assistant/core)
+ homeassistant/components/homekit/__init__.py:1099: error: Unsupported operand types for in ("str | None" and "tuple[BinarySensorDeviceClass, SensorDeviceClass]")  [operator]

werkzeug (https://github.com/pallets/werkzeug)
+ src/werkzeug/routing/exceptions.py:94: error: List item 0 has incompatible type "float"; expected "bool"  [list-item]
+ src/werkzeug/routing/exceptions.py:101: error: List item 1 has incompatible type "float"; expected "bool"  [list-item]
+ src/werkzeug/routing/exceptions.py:102: error: Unsupported operand types for * ("float" and "bool")  [operator]

dulwich (https://github.com/dulwich/dulwich)
- dulwich/client.py:2254: error: Unused "type: ignore" comment  [unused-ignore]

@huonw
Copy link
Author

huonw commented Oct 24, 2024

Hm, it seems like this might be causing functions like + and sum to behave strangely. Will have to look later.

@huonw
Copy link
Author

huonw commented Oct 24, 2024

Hm, looks like they're spurious errors: #18026

@huonw
Copy link
Author

huonw commented Oct 31, 2024

Yep, with further analysis (in #18026) this change is blocked on having a mechanism to avoid those spurious errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Opt-in error for if x: when x: None | int or None | str etc.

1 participant