Skip to content

Fixed bug that results in incorrect type narrowing when a value whose…#10176

Closed
erictraut wants to merge 1 commit intomainfrom
issue-10171
Closed

Fixed bug that results in incorrect type narrowing when a value whose…#10176
erictraut wants to merge 1 commit intomainfrom
issue-10171

Conversation

@erictraut
Copy link
Copy Markdown
Collaborator

… type is a non-callable (and non-final) class is passed as the second argument to an isinstance call and the first argument is a callable type. In this case, a subclass could provide a compatible __call__ method, so the type shouldn't be narrowed to Never. This addresses #10171.

… type is a non-callable (and non-final) class is passed as the second argument to an `isinstance` call and the first argument is a callable type. In this case, a subclass could provide a compatible `__call__` method, so the type shouldn't be narrowed to `Never`. This addresses #10171.
@github-actions
Copy link
Copy Markdown
Contributor

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

pytest-robotframework (https://github.com/detachhead/pytest-robotframework)
+   /tmp/mypy_primer/projects/pytest-robotframework/pytest_robotframework/__init__.py:221:20 - error: Type "_KeywordDecorator" is not assignable to return type "() -> Unknown"
+     Type "_KeywordDecorator" is not assignable to type "() -> Unknown" (reportReturnType)
- 300 errors, 17 warnings, 0 informations 
+ 301 errors, 17 warnings, 0 informations 

ibis (https://github.com/ibis-project/ibis)
+   /tmp/mypy_primer/projects/ibis/ibis/expr/types/relations.py:3978:29 - error: Cannot access attribute "setdefault" for class "Mapping[Unknown, Unknown]"
+     Attribute "setdefault" is unknown (reportAttributeAccessIssue)
- 7594 errors, 117 warnings, 0 informations 
+ 7595 errors, 117 warnings, 0 informations 

sympy (https://github.com/sympy/sympy)
-   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_matchpy_connector.py:120:70 - error: Argument of type "list[Unequality | Integer]" cannot be assigned to parameter "conditions_nonfalse" of type "List[Expr]" in function "add"
+   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_matchpy_connector.py:120:70 - error: Argument of type "list[Unequality | Unknown | Equality | Relational]" cannot be assigned to parameter "conditions_nonfalse" of type "List[Expr]" in function "add"
-     Type "Unequality | Integer" is not assignable to type "Expr"
+     Type "Unequality | Unknown | Equality | Relational" is not assignable to type "Expr"
-       "Unequality" is not assignable to "Expr"
+       "Relational" is not assignable to "Expr"
-     Type "Unequality | Integer" is not assignable to type "Expr"
+     Type "Unequality | Unknown | Equality | Relational" is not assignable to type "Expr"
-       "Unequality" is not assignable to "Expr"
+       "Relational" is not assignable to "Expr"
-     Type "Unequality | Integer" is not assignable to type "Expr"
+     Type "Unequality | Unknown | Equality | Relational" is not assignable to type "Expr"
-       "Unequality" is not assignable to "Expr"
+       "Relational" is not assignable to "Expr"
-     Type "Unequality | Integer" is not assignable to type "Expr"
+     Type "Unequality | Unknown | Equality | Relational" is not assignable to type "Expr"
-       "Unequality" is not assignable to "Expr" (reportArgumentType)
+       "Relational" is not assignable to "Expr" (reportArgumentType)

core (https://github.com/home-assistant/core)
-   /tmp/mypy_primer/projects/core/homeassistant/components/siren/__init__.py:72:13 - error: Operator "not in" not supported for types "int | str" and "() -> (list[int | str] | dict[int, str] | None)"
+   /tmp/mypy_primer/projects/core/homeassistant/components/siren/__init__.py:72:13 - error: Operator "not in" not supported for types "int | str" and "(() -> (list[int | str] | dict[int, str] | None)) | dict[Unknown, Unknown]"
-   /tmp/mypy_primer/projects/core/homeassistant/components/siren/__init__.py:84:39 - error: "Never" is not iterable (reportGeneralTypeIssues)
- 67445 errors, 289 warnings, 0 informations 
+ 67444 errors, 289 warnings, 0 informations 

steam.py (https://github.com/Gobot1234/steam.py)
+   /tmp/mypy_primer/projects/steam.py/steam/ext/commands/commands.py:525:72 - error: Type of "__class__" is partially unknown
+     Type of "__class__" is "type[ConverterBase[Any]] | type[ConverterBase[Unknown]]" (reportUnknownMemberType)
- 8519 errors, 94 warnings, 0 informations 
+ 8520 errors, 94 warnings, 0 informations 

antidote (https://github.com/Finistere/antidote)
-   /tmp/mypy_primer/projects/antidote/src/antidote/core/_debug.py:151:81 - warning: Unnecessary "# type: ignore" comment (reportUnnecessaryTypeIgnoreComment)
+   /tmp/mypy_primer/projects/antidote/src/antidote/core/_debug.py:146:25 - error: Type of "w" is partially unknown
+     Type of "w" is "Unknown | ((...) -> Any)" (reportUnknownVariableType)
+   /tmp/mypy_primer/projects/antidote/src/antidote/core/_debug.py:146:30 - error: Type of "wired" is partially unknown
+     Type of "wired" is "Sequence[Unknown] | Sequence[(...) -> Any]" (reportUnknownMemberType)
+   /tmp/mypy_primer/projects/antidote/src/antidote/core/_inject.py:108:17 - error: Type of "__func__" is partially unknown
+     Type of "__func__" is "((type[Unknown], ...) -> Unknown) | ((...) -> Unknown) | ((type[F@rewire], ...) -> Unknown)" (reportUnknownMemberType)
+   /tmp/mypy_primer/projects/antidote/src/antidote/core/_inject.py:108:17 - error: Argument type is partially unknown
+     Argument corresponds to parameter "wrapper" in function "rewrap"
+     Argument type is "((type[Unknown], ...) -> Unknown) | ((...) -> Unknown) | ((type[F@rewire], ...) -> Unknown)" (reportUnknownArgumentType)
+   /tmp/mypy_primer/projects/antidote/src/antidote/core/_inject.py:285:13 - error: Type of "wrapper" is partially unknown
+     Type of "wrapper" is "((type[Unknown], ...) -> Unknown) | ((...) -> Unknown) | ((type[Any], ...) -> Unknown) | ((...) -> Any)" (reportUnknownVariableType)
+   /tmp/mypy_primer/projects/antidote/src/antidote/core/_inject.py:286:17 - error: Type of "__func__" is partially unknown
+     Type of "__func__" is "((type[Unknown], ...) -> Unknown) | ((...) -> Unknown) | ((type[Any], ...) -> Unknown)" (reportUnknownMemberType)
+   /tmp/mypy_primer/projects/antidote/src/antidote/core/_inject.py:286:17 - error: Argument type is partially unknown
+     Argument corresponds to parameter "wrapped" in function "wrap"
+     Argument type is "((type[Unknown], ...) -> Unknown) | ((...) -> Unknown) | ((type[Any], ...) -> Unknown) | ((...) -> Any)" (reportUnknownArgumentType)
+   /tmp/mypy_primer/projects/antidote/src/antidote/core/_inject.py:293:37 - error: Argument type is partially unknown
+     Argument corresponds to parameter "f" in function "__init__"
+     Argument type is "((type[Unknown], ...) -> Unknown) | ((...) -> Unknown) | ((type[Any], ...) -> Unknown) | ((...) -> Any)" (reportUnknownArgumentType)
+   /tmp/mypy_primer/projects/antidote/src/antidote/core/_inject.py:295:36 - error: Argument type is partially unknown
+     Argument corresponds to parameter "f" in function "__init__"
+     Argument type is "((type[Unknown], ...) -> Unknown) | ((...) -> Unknown) | ((type[Any], ...) -> Unknown) | ((...) -> Any)" (reportUnknownArgumentType)
+   /tmp/mypy_primer/projects/antidote/src/antidote/core/_inject.py:296:20 - error: Return type, "((type[Unknown], ...) -> Unknown) | ((...) -> Unknown) | ((type[Any], ...) -> Unknown) | ((...) -> Any)", is partially unknown (reportUnknownVariableType)
- 745 errors, 78 warnings, 0 informations 
+ 755 errors, 77 warnings, 0 informations 

kopf (https://github.com/nolar/kopf)
-   /tmp/mypy_primer/projects/kopf/kopf/_core/engines/admission.py:267:20 - error: Object of type "Never" cannot be used with "async with" because it does not correctly implement __aenter__ (reportGeneralTypeIssues)
-   /tmp/mypy_primer/projects/kopf/kopf/_core/engines/admission.py:267:20 - error: Object of type "Never" cannot be used with "with" because it does not correctly implement __aexit__ (reportGeneralTypeIssues)
- 1167 errors, 19 warnings, 0 informations 
+ 1165 errors, 19 warnings, 0 informations 

Tanjun (https://github.com/FasterSpeeding/Tanjun)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/annotations.py:451:33 - warning: Argument type is partially unknown
+     Argument corresponds to parameter "_str_converters" in function "__init__"
+     Argument type is "tuple[(str, ...) -> (Coroutine[Any, Any, Any] | Any)] | Sequence[Unknown] | Sequence[(str, ...) -> (Coroutine[Any, Any, Any] | Any)]" (reportUnknownArgumentType)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/annotations.py:1538:80 - warning: Argument type is partially unknown
+     Argument corresponds to parameter "converter" in function "__init__"
+     Argument type is "((str, ...) -> (Coroutine[Any, Any, _T@__getitem__] | _T@__getitem__)) | Unknown" (reportUnknownArgumentType)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/annotations.py:1538:80 - warning: Argument type is partially unknown
+     Argument corresponds to parameter "other_converters" in function "__init__"
+     Argument type is "((str, ...) -> (Coroutine[Any, Any, _T@__getitem__] | _T@__getitem__)) | Unknown" (reportUnknownArgumentType)
-   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/menu.py:176:24 - error: Unnecessary "cast" call; type is already "_MessageCallbackSigT@decorator" (reportUnnecessaryCast)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/menu.py:174:13 - warning: Type of "wrapped_command" is partially unknown
+     Type of "wrapped_command" is "MenuCommand[Unknown, Unknown] | MessageCommand[Unknown] | SlashCommand[Unknown] | MenuCommand[_MessageCallbackSigT@decorator, Any] | MessageCommand[_MessageCallbackSigT@decorator] | SlashCommand[_MessageCallbackSigT@decorator]" (reportUnknownVariableType)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/menu.py:176:60 - warning: Type of "callback" is partially unknown
+     Type of "callback" is "Unknown | _MessageCallbackSigT@decorator" (reportUnknownMemberType)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/menu.py:191:30 - warning: Argument type is partially unknown
+     Argument corresponds to parameter "_wrapped_command" in function "__init__"
+     Argument type is "MenuCommand[Unknown, Unknown] | MessageCommand[Unknown] | SlashCommand[Unknown] | MenuCommand[_MessageCallbackSigT@decorator, Any] | MessageCommand[_MessageCallbackSigT@decorator] | SlashCommand[_MessageCallbackSigT@decorator] | None" (reportUnknownArgumentType)
-   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/menu.py:303:24 - error: Unnecessary "cast" call; type is already "_UserCallbackSigT@decorator" (reportUnnecessaryCast)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/menu.py:301:13 - warning: Type of "wrapped_command" is partially unknown
+     Type of "wrapped_command" is "MenuCommand[Unknown, Unknown] | MessageCommand[Unknown] | SlashCommand[Unknown] | MenuCommand[_UserCallbackSigT@decorator, Any] | MessageCommand[_UserCallbackSigT@decorator] | SlashCommand[_UserCallbackSigT@decorator]" (reportUnknownVariableType)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/menu.py:303:57 - warning: Type of "callback" is partially unknown
+     Type of "callback" is "Unknown | _UserCallbackSigT@decorator" (reportUnknownMemberType)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/menu.py:318:30 - warning: Argument type is partially unknown
+     Argument corresponds to parameter "_wrapped_command" in function "__init__"
+     Argument type is "MenuCommand[Unknown, Unknown] | MessageCommand[Unknown] | SlashCommand[Unknown] | MenuCommand[_UserCallbackSigT@decorator, Any] | MessageCommand[_UserCallbackSigT@decorator] | SlashCommand[_UserCallbackSigT@decorator] | None" (reportUnknownArgumentType)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/menu.py:522:57 - warning: Type of "callback" is partially unknown
+     Type of "callback" is "Unknown | _UserCallbackSigT@__init__ | _MessageCallbackSigT@__init__" (reportUnknownMemberType)
-   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/message.py:109:24 - error: Unnecessary "cast" call; type is already "_MessageCallbackSigT@decorator" (reportUnnecessaryCast)
-   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/message.py:166:24 - error: Unnecessary "cast" call; type is already "_MessageCallbackSigT@decorator" (reportUnnecessaryCast)
-   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/message.py:236:24 - error: Unnecessary "cast" call; type is already "_MessageCallbackSigT@MessageCommand" (reportUnnecessaryCast)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/message.py:107:13 - warning: Type of "wrapped_command" is partially unknown
+     Type of "wrapped_command" is "MenuCommand[_MessageCallbackSigT@decorator, Any] | MessageCommand[_MessageCallbackSigT@decorator] | SlashCommand[_MessageCallbackSigT@decorator] | MenuCommand[Unknown, Unknown] | MessageCommand[Unknown] | SlashCommand[Unknown]" (reportUnknownVariableType)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/message.py:109:60 - warning: Type of "callback" is partially unknown
+     Type of "callback" is "_MessageCallbackSigT@decorator | Unknown" (reportUnknownMemberType)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/message.py:115:91 - warning: Argument type is partially unknown
+     Argument corresponds to parameter "_wrapped_command" in function "__init__"
+     Argument type is "MenuCommand[_MessageCallbackSigT@decorator, Any] | MessageCommand[_MessageCallbackSigT@decorator] | SlashCommand[_MessageCallbackSigT@decorator] | MenuCommand[Unknown, Unknown] | MessageCommand[Unknown] | SlashCommand[Unknown] | None" (reportUnknownArgumentType)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/message.py:164:13 - warning: Type of "wrapped_command" is partially unknown
+     Type of "wrapped_command" is "MenuCommand[_MessageCallbackSigT@decorator, Any] | MessageCommand[_MessageCallbackSigT@decorator] | SlashCommand[_MessageCallbackSigT@decorator] | MenuCommand[Unknown, Unknown] | MessageCommand[Unknown] | SlashCommand[Unknown]" (reportUnknownVariableType)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/message.py:166:60 - warning: Type of "callback" is partially unknown
+     Type of "callback" is "_MessageCallbackSigT@decorator | Unknown" (reportUnknownMemberType)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/message.py:172:106 - warning: Argument type is partially unknown
+     Argument corresponds to parameter "_wrapped_command" in function "__init__"
+     Argument type is "MenuCommand[_MessageCallbackSigT@decorator, Any] | MessageCommand[_MessageCallbackSigT@decorator] | SlashCommand[_MessageCallbackSigT@decorator] | MenuCommand[Unknown, Unknown] | MessageCommand[Unknown] | SlashCommand[Unknown] | None" (reportUnknownArgumentType)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/message.py:236:60 - warning: Type of "callback" is partially unknown
+     Type of "callback" is "_MessageCallbackSigT@MessageCommand | Unknown" (reportUnknownMemberType)
-   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/slash.py:381:24 - error: Unnecessary "cast" call; type is already "_SlashCallbackSigT@decorator" (reportUnnecessaryCast)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/slash.py:379:13 - warning: Type of "wrapped_command" is partially unknown
+     Type of "wrapped_command" is "MenuCommand[Unknown, Unknown] | MessageCommand[Unknown] | SlashCommand[Unknown] | MenuCommand[_SlashCallbackSigT@decorator, Any] | MessageCommand[_SlashCallbackSigT@decorator] | SlashCommand[_SlashCallbackSigT@decorator]" (reportUnknownVariableType)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/slash.py:381:58 - warning: Type of "callback" is partially unknown
+     Type of "callback" is "Unknown | _SlashCallbackSigT@decorator" (reportUnknownMemberType)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/slash.py:398:30 - warning: Argument type is partially unknown
+     Argument corresponds to parameter "_wrapped_command" in function "__init__"
+     Argument type is "MenuCommand[Unknown, Unknown] | MessageCommand[Unknown] | SlashCommand[Unknown] | MenuCommand[_SlashCallbackSigT@decorator, Any] | MessageCommand[_SlashCallbackSigT@decorator] | SlashCommand[_SlashCallbackSigT@decorator] | None" (reportUnknownArgumentType)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/slash.py:819:84 - error: Unnecessary "# pyright: ignore" rule: "reportUnnecessaryIsInstance" (reportUnnecessaryTypeIgnoreComment)
-   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/slash.py:1644:24 - error: Unnecessary "cast" call; type is already "_SlashCallbackSigT@SlashCommand" (reportUnnecessaryCast)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/slash.py:1644:58 - warning: Type of "callback" is partially unknown
+     Type of "callback" is "Unknown | _SlashCallbackSigT@SlashCommand" (reportUnknownMemberType)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/slash.py:1779:31 - warning: Argument type is partially unknown
+     Argument corresponds to parameter "iterable" in function "__init__"
+     Argument type is "Sequence[((float, ...) -> (Coroutine[Any, Any, Any] | Any)) | ((int, ...) -> (Coroutine[Any, Any, Any] | Any)) | ((str, ...) -> (Coroutine[Any, Any, Any] | Any))] | Sequence[Unknown]" (reportUnknownArgumentType)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/commands/slash.py:1786:88 - error: Unnecessary "# pyright: ignore" rule: "reportUnnecessaryIsInstance" (reportUnnecessaryTypeIgnoreComment)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/parsing.py:1552:17 - warning: Type of "converter" is partially unknown
+     Type of "converter" is "((str, ...) -> (Coroutine[Any, Any, Any] | Any)) | Unknown" (reportUnknownVariableType)
+   /tmp/mypy_primer/projects/Tanjun/tanjun/parsing.py:1553:37 - warning: Argument type is partially unknown
+     Argument corresponds to parameter "converter" in function "_add_converter"
+     Argument type is "((str, ...) -> (Coroutine[Any, Any, Any] | Any)) | Unknown" (reportUnknownArgumentType)

... (truncated 7 lines) ...

trio (https://github.com/python-trio/trio)
-   /tmp/mypy_primer/projects/trio/src/trio/_util.py:84:12 - error: Unnecessary isinstance call; "(*PosArgsT@coroutine_or_error) -> Awaitable[RetT@coroutine_or_error]" is never an instance of "Coroutine[Unknown, Unknown, Unknown]" (reportUnnecessaryIsInstance)
- 4228 errors, 48 warnings, 0 informations 
+ 4227 errors, 48 warnings, 0 informations 

hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
+   /tmp/mypy_primer/projects/hydra-zen/src/hydra_zen/wrapper/_implementations.py:247:9 - error: Type of "_pre_call_iterable" is partially unknown
+     Type of "_pre_call_iterable" is "tuple[((Any) -> Any) | None] | Iterable[Unknown] | Iterable[(Any) -> Any]" (reportUnknownMemberType)
+   /tmp/mypy_primer/projects/hydra-zen/src/hydra_zen/wrapper/_implementations.py:252:13 - error: Type of "_f" is partially unknown
+     Type of "_f" is "((Any) -> Any) | Unknown | None" (reportUnknownVariableType)
+   /tmp/mypy_primer/projects/hydra-zen/src/hydra_zen/wrapper/_implementations.py:252:19 - error: Type of "_pre_call_iterable" is partially unknown
+     Type of "_pre_call_iterable" is "tuple[((Any) -> Any) | None] | Iterable[Unknown] | Iterable[(Any) -> Any]" (reportUnknownMemberType)
+   /tmp/mypy_primer/projects/hydra-zen/src/hydra_zen/wrapper/_implementations.py:273:76 - error: Argument type is partially unknown
+     Argument corresponds to parameter "x" in function "_flat_call"
+     Argument type is "Iterable[Unknown] | Iterable[(Any) -> Any]" (reportUnknownArgumentType)
+   /tmp/mypy_primer/projects/hydra-zen/src/hydra_zen/wrapper/_implementations.py:322:13 - error: Type of "_f" is partially unknown
+     Type of "_f" is "((Any) -> Any) | Unknown | None" (reportUnknownVariableType)
+   /tmp/mypy_primer/projects/hydra-zen/src/hydra_zen/wrapper/_implementations.py:322:19 - error: Type of "_pre_call_iterable" is partially unknown
+     Type of "_pre_call_iterable" is "tuple[((Any) -> Any) | None] | Iterable[Unknown] | Iterable[(Any) -> Any]" (reportUnknownMemberType)
+   /tmp/mypy_primer/projects/hydra-zen/src/hydra_zen/wrapper/_implementations.py:1735:24 - error: Type of "get" is partially unknown
+     Type of "get" is "Overload[(key: str | None, /) -> (str | None), (key: str | None, /, default: str | _T@get | None) -> (str | _T@get | None)] | Overload[(key: Unknown, /) -> (Unknown | None), (key: Unknown, /, default: Unknown | _T@get) -> (Unknown | _T@get)]" (reportUnknownMemberType)
+   /tmp/mypy_primer/projects/hydra-zen/src/hydra_zen/wrapper/_implementations.py:1735:24 - error: Return type of lambda, "str | Unknown | None", is partially unknown (reportUnknownLambdaType)
+   /tmp/mypy_primer/projects/hydra-zen/src/hydra_zen/wrapper/_implementations.py:1742:13 - error: Type of "new_group" is partially unknown
+     Type of "new_group" is "str | Unknown | None" (reportUnknownVariableType)
- 173 errors, 13 warnings, 183 informations 
+ 182 errors, 13 warnings, 183 informations 

@erictraut erictraut closed this Mar 26, 2025
@erictraut erictraut deleted the issue-10171 branch March 26, 2025 17:52
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.

1 participant