-
-
Notifications
You must be signed in to change notification settings - Fork 3k
refactor visit_conditional_expr
to fix ternary behavior (option 2)
#19562
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
Draft
randolf-scholz
wants to merge
4
commits into
python:master
Choose a base branch
from
randolf-scholz:fix_ternary_option2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+178
−57
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
visit_conditional_expr
to fix ternary behaviorvisit_conditional_expr
to fix ternary behavior (option 2)
This comment has been minimized.
This comment has been minimized.
Diff from mypy_primer, showing the effect of this PR on open source code: colour (https://github.com/colour-science/colour)
+ colour/io/luts/lut.py:1366: error: Argument 1 to "tile" has incompatible type "Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str] | None"; expected "_SupportsArray[dtype[signedinteger[_8Bit] | signedinteger[_16Bit] | signedinteger[_32Bit] | signedinteger[_64Bit] | unsignedinteger[_8Bit] | unsignedinteger[_16Bit] | unsignedinteger[_32Bit] | unsignedinteger[_64Bit] | Any]] | _NestedSequence[_SupportsArray[dtype[signedinteger[_8Bit] | signedinteger[_16Bit] | signedinteger[_32Bit] | signedinteger[_64Bit] | unsignedinteger[_8Bit] | unsignedinteger[_16Bit] | unsignedinteger[_32Bit] | unsignedinteger[_64Bit] | Any]]]" [arg-type]
- colour/io/luts/lut.py:1380: error: No overload variant of "pad" matches argument types "ndarray[Any, Any]", "tuple[int, Any | signedinteger[_8Bit] | signedinteger[_16Bit] | signedinteger[_32Bit] | signedinteger[_64Bit] | unsignedinteger[_8Bit] | unsignedinteger[_16Bit] | unsignedinteger[_32Bit] | unsignedinteger[_64Bit]]", "str", "float" [call-overload]
- colour/io/luts/lut.py:1380: note: Possible overload variants:
- colour/io/luts/lut.py:1380: note: def [_ScalarT: generic[Any]] pad(array: _SupportsArray[dtype[_ScalarT]] | _NestedSequence[_SupportsArray[dtype[_ScalarT]]], pad_width: _SupportsArray[dtype[integer[Any]]] | _NestedSequence[_SupportsArray[dtype[integer[Any]]]] | int | _NestedSequence[int], mode: Literal['constant', 'edge', 'linear_ramp', 'maximum', 'mean', 'median', 'minimum', 'reflect', 'symmetric', 'wrap', 'empty'] = ..., *, stat_length: _SupportsArray[dtype[integer[Any]]] | _NestedSequence[_SupportsArray[dtype[integer[Any]]]] | int | _NestedSequence[int] | None = ..., constant_values: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str] = ..., end_values: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str] = ..., reflect_type: Literal['odd', 'even'] = ...) -> ndarray[tuple[Any, ...], dtype[_ScalarT]]
- colour/io/luts/lut.py:1380: note: def pad(array: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str], pad_width: _SupportsArray[dtype[integer[Any]]] | _NestedSequence[_SupportsArray[dtype[integer[Any]]]] | int | _NestedSequence[int], mode: Literal['constant', 'edge', 'linear_ramp', 'maximum', 'mean', 'median', 'minimum', 'reflect', 'symmetric', 'wrap', 'empty'] = ..., *, stat_length: _SupportsArray[dtype[integer[Any]]] | _NestedSequence[_SupportsArray[dtype[integer[Any]]]] | int | _NestedSequence[int] | None = ..., constant_values: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str] = ..., end_values: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str] = ..., reflect_type: Literal['odd', 'even'] = ...) -> ndarray[tuple[Any, ...], dtype[Any]]
- colour/io/luts/lut.py:1380: note: def [_ScalarT: generic[Any]] pad(array: _SupportsArray[dtype[_ScalarT]] | _NestedSequence[_SupportsArray[dtype[_ScalarT]]], pad_width: _SupportsArray[dtype[integer[Any]]] | _NestedSequence[_SupportsArray[dtype[integer[Any]]]] | int | _NestedSequence[int], mode: _ModeFunc, **kwargs: Any) -> ndarray[tuple[Any, ...], dtype[_ScalarT]]
- colour/io/luts/lut.py:1380: note: def pad(array: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str], pad_width: _SupportsArray[dtype[integer[Any]]] | _NestedSequence[_SupportsArray[dtype[integer[Any]]]] | int | _NestedSequence[int], mode: _ModeFunc, **kwargs: Any) -> ndarray[tuple[Any, ...], dtype[Any]]
steam.py (https://github.com/Gobot1234/steam.py)
+ steam/utils.py:883: error: Unused "type: ignore" comment [unused-ignore]
- steam/http.py:100: error: Dict entry 0 has incompatible type "str": "str | None"; expected "str": "str" [dict-item]
- steam/gateway.py:830: error: No overload variant of "wait_for" of "SteamWebSocket" matches argument types "int", "Callable[[UnifiedMsgT], bool]" [call-overload]
+ steam/gateway.py:830: error: No overload variant of "wait_for" of "SteamWebSocket" matches argument types "int", "Callable[[UnifiedMsgT], bool] | Callable[[Any], Any]" [call-overload]
pyodide (https://github.com/pyodide/pyodide)
- pyodide-build/pyodide_build/xbuildenv.py:309: error: List item 0 has incompatible type "list[str | None]"; expected "str | bytes | PathLike[str] | PathLike[bytes]" [list-item]
prefect (https://github.com/PrefectHQ/prefect)
- src/prefect/runner/storage.py:178: error: Argument 1 to "deepcopy" has incompatible type "Union[GitCredentials, Block, dict[str, Any]]"; expected "dict[str, Any]" [arg-type]
+ src/prefect/runner/storage.py:183: error: TypedDict key must be a string literal; expected one of ("username", "access_token") [literal-required]
+ src/prefect/runner/storage.py:185: error: TypedDict key must be a string literal; expected one of ("username", "access_token") [literal-required]
dd-trace-py (https://github.com/DataDog/dd-trace-py)
+ ddtrace/appsec/_iast/_taint_tracking/aspects.py:704: error: Unused "type: ignore" comment [unused-ignore]
cwltool (https://github.com/common-workflow-language/cwltool)
+ cwltool/command_line_tool.py: note: In function "job":
+ cwltool/command_line_tool.py:1077:22: error: Redundant cast to "MutationManager" [redundant-cast]
+ cwltool/command_line_tool.py:1082:22: error: Redundant cast to "MutationManager" [redundant-cast]
+ cwltool/executors.py:156: error: Unused "type: ignore" comment [unused-ignore]
discord.py (https://github.com/Rapptz/discord.py)
+ discord/utils.py:491: error: Unused "type: ignore" comment [unused-ignore]
+ discord/utils.py:607: error: Unused "type: ignore" comment [unused-ignore]
+ discord/message.py:2571: error: Unused "type: ignore" comment [unused-ignore]
+ discord/message.py:2575: error: Unused "type: ignore" comment [unused-ignore]
+ discord/message.py:2579: error: Unused "type: ignore" comment [unused-ignore]
+ discord/client.py:2916: error: Unused "type: ignore" comment [unused-ignore]
+ discord/client.py:2937: error: Unused "type: ignore" comment [unused-ignore]
+ discord/ext/commands/converter.py:1032: error: Unused "type: ignore" comment [unused-ignore]
+ discord/ext/commands/converter.py:1036: error: Unused "type: ignore" comment [unused-ignore]
+ discord/ext/commands/converter.py:1051: error: Unused "type: ignore" comment [unused-ignore]
spark (https://github.com/apache/spark)
+ python/pyspark/sql/connect/client/reattach.py:183: error: Unused "type: ignore" comment [unused-ignore]
+ python/pyspark/sql/connect/client/reattach.py:183: error: No overload variant of "next" matches argument type "None" [call-overload]
+ python/pyspark/sql/connect/client/reattach.py:183: note: Error code "call-overload" not covered by "type: ignore" comment
+ python/pyspark/sql/connect/client/reattach.py:183: note: Possible overload variants:
+ python/pyspark/sql/connect/client/reattach.py:183: note: def [_T] next(SupportsNext[_T], /) -> _T
+ python/pyspark/sql/connect/client/reattach.py:183: note: def [_T, _VT] next(SupportsNext[_T], _VT, /) -> _T | _VT
optuna (https://github.com/optuna/optuna)
+ optuna/visualization/_intermediate_values.py:95: error: Unused "type: ignore" comment [unused-ignore]
core (https://github.com/home-assistant/core)
+ homeassistant/components/withings/sensor.py:767: error: Statement is unreachable [unreachable]
+ homeassistant/components/withings/sensor.py:796: error: Statement is unreachable [unreachable]
+ homeassistant/components/withings/sensor.py:823: error: Statement is unreachable [unreachable]
+ homeassistant/components/withings/calendar.py:48: error: Statement is unreachable [unreachable]
|
This was referenced Aug 1, 2025
TODO: fix |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
TypeVar
variable inference in ternary operators #18817testTernaryOperatorWithDefault
testLambdaTernary{, IndirectAttribute, DoubleIndirectAttribute}
test{List, Set, Dict}ComprehensionTernary
Option ①: #19563
Option ② from #19561 (comment) which additionally copies
MemberExpr
elements to the nested binder, hence allows ternaries likef(x.attr) if x.attr else g(x.attr)
to consider the bound type ofx.attr
.The current implementation of
visit_conditional_expr
seemed to some rather complicated things, I found that if there is no context, we can simply use the union of the types produced when considering the branches context-free as an artificial context that leads to the desired behavior in the unification test cases.