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

Improved type narrowing for issubclass in the negative ("else") cas… #9704

Merged
merged 1 commit into from
Jan 14, 2025

Conversation

erictraut
Copy link
Collaborator

…e when the subject type is type or Any. This addresses #9656.

…e when the subject type is `type` or `Any`. This addresses #9656.
Copy link
Contributor

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

steam.py (https://github.com/Gobot1234/steam.py)
-   /tmp/mypy_primer/projects/steam.py/steam/ext/commands/commands.py:553:30 - error: Expected 0 positional arguments (reportCallIssue)
+   /tmp/mypy_primer/projects/steam.py/steam/ext/commands/commands.py:558:24 - error: Type of "__class__" is partially unknown
+     Type of "__class__" is "type[BasicConverter[Any]] | type[function] | type[Unknown]" (reportUnknownMemberType)

sympy (https://github.com/sympy/sympy)
-   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_matchpy_connector.py:120:18 - error: Argument of type "Equality | Unknown" cannot be assigned to parameter "expr" of type "Expr" in function "add"
+   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_matchpy_connector.py:120:18 - error: Argument of type "Equality | Unknown | Relational | Unequality | Integer" cannot be assigned to parameter "expr" of type "Expr" in function "add"
-     Type "Equality | Unknown" is not assignable to type "Expr"
+     Type "Equality | Unknown | Relational | Unequality | Integer" is not assignable to type "Expr"
-       "Equality" is not assignable to "Expr" (reportArgumentType)
+       "Relational" is not assignable to "Expr" (reportArgumentType)
-   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_matchpy_connector.py:123:18 - error: Argument of type "Equality | Unknown" cannot be assigned to parameter "expr" of type "Expr" in function "add"
+   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_matchpy_connector.py:123:18 - error: Argument of type "Equality | Unknown | Relational | Unequality | Integer" cannot be assigned to parameter "expr" of type "Expr" in function "add"
-     Type "Equality | Unknown" is not assignable to type "Expr"
+     Type "Equality | Unknown | Relational | Unequality | Integer" is not assignable to type "Expr"
-       "Equality" is not assignable to "Expr" (reportArgumentType)
+       "Relational" is not assignable to "Expr" (reportArgumentType)
-   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_matchpy_connector.py:126:9 - error: Argument of type "Equality | Unknown" cannot be assigned to parameter "expr" of type "Expr" in function "add"
+   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_matchpy_connector.py:126:9 - error: Argument of type "Equality | Unknown | Relational | Unequality | Integer" cannot be assigned to parameter "expr" of type "Expr" in function "add"
-     Type "Equality | Unknown" is not assignable to type "Expr"
+     Type "Equality | Unknown | Relational | Unequality | Integer" is not assignable to type "Expr"
-       "Equality" is not assignable to "Expr" (reportArgumentType)
+       "Relational" is not assignable to "Expr" (reportArgumentType)
-   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_matchpy_connector.py:132:9 - error: Argument of type "Equality | Unknown" cannot be assigned to parameter "expr" of type "Expr" in function "add"
+   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_matchpy_connector.py:132:9 - error: Argument of type "Equality | Unknown | Relational | Unequality | Integer" cannot be assigned to parameter "expr" of type "Expr" in function "add"
-     Type "Equality | Unknown" is not assignable to type "Expr"
+     Type "Equality | Unknown | Relational | Unequality | Integer" is not assignable to type "Expr"
-       "Equality" is not assignable to "Expr" (reportArgumentType)
+       "Relational" is not assignable to "Expr" (reportArgumentType)
-   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_wester.py:895:12 - error: Operator "/" not supported for types "Equality | Unknown" and "Literal[2]"
+   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_wester.py:895:12 - error: Operator "/" not supported for types "Equality | Unknown | Relational | Unequality | Integer" and "Literal[2]"
+     Operator "/" not supported for types "Equality" and "Literal[2]"
+     Operator "/" not supported for types "Relational" and "Literal[2]"
-     Operator "/" not supported for types "Equality" and "Literal[2]" (reportOperatorIssue)
+     Operator "/" not supported for types "Unequality" and "Literal[2]" (reportOperatorIssue)
+   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_wester.py:1863:12 - error: Cannot access attribute "combsimp" for class "Relational"
+     Attribute "combsimp" is unknown (reportAttributeAccessIssue)
+   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_wester.py:1863:12 - error: Cannot access attribute "combsimp" for class "Unequality"
+     Attribute "combsimp" is unknown (reportAttributeAccessIssue)
+     Attribute "factor" is unknown (reportAttributeAccessIssue)
+   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_wester.py:1911:14 - error: Cannot access attribute "factor" for class "Relational"
+     Attribute "factor" is unknown (reportAttributeAccessIssue)
+   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_wester.py:1911:14 - error: Cannot access attribute "factor" for class "Unequality"
+   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_wester.py:1918:14 - error: Cannot access attribute "combsimp" for class "Relational"
+     Attribute "combsimp" is unknown (reportAttributeAccessIssue)
+   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_wester.py:1918:14 - error: Cannot access attribute "combsimp" for class "Unequality"
+     Attribute "combsimp" is unknown (reportAttributeAccessIssue)
+   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_wester.py:1932:12 - error: Cannot access attribute "combsimp" for class "Relational"
+     Attribute "combsimp" is unknown (reportAttributeAccessIssue)
+   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_wester.py:1932:12 - error: Cannot access attribute "combsimp" for class "Unequality"
+     Attribute "combsimp" is unknown (reportAttributeAccessIssue)
-   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_wester.py:1992:22 - error: Argument of type "Equality | Unknown | NaN | Sum" cannot be assigned to parameter "x" of type "ConvertibleToFloat" in function "__new__"
+   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_wester.py:1992:22 - error: Argument of type "Equality | Unknown | Relational | Unequality | Integer | NaN | Sum" cannot be assigned to parameter "x" of type "ConvertibleToFloat" in function "__new__"
-     Type "Equality | Unknown | NaN | Sum" is not assignable to type "ConvertibleToFloat"
+     Type "Equality | Unknown | Relational | Unequality | Integer | NaN | Sum" is not assignable to type "ConvertibleToFloat"
-       Type "Equality" is not assignable to type "ConvertibleToFloat"
+       Type "Relational" is not assignable to type "ConvertibleToFloat"
-         "Equality" is not assignable to "str"
+         "Relational" is not assignable to "str"
-         "Equality" is incompatible with protocol "Buffer"
+         "Relational" is incompatible with protocol "Buffer"
-         "Equality" is incompatible with protocol "SupportsFloat"
+         "Relational" is incompatible with protocol "SupportsFloat"
-         "Equality" is incompatible with protocol "SupportsIndex"
+         "Relational" is incompatible with protocol "SupportsIndex"
+     Attribute "gammasimp" is unknown (reportAttributeAccessIssue)
+   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_wester.py:2074:56 - error: Cannot access attribute "gammasimp" for class "Relational"
+     Attribute "gammasimp" is unknown (reportAttributeAccessIssue)
+   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_wester.py:2074:56 - error: Cannot access attribute "gammasimp" for class "Unequality"
-   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_wester.py:2973:37 - error: Operator "*" not supported for types "Literal[2]" and "Equality | Unknown | NaN | Sum"
+   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_wester.py:2973:37 - error: Operator "*" not supported for types "Literal[2]" and "Equality | Unknown | Relational | Unequality | Integer | NaN | Sum"
+     Operator "*" not supported for types "Literal[2]" and "Equality"
+     Operator "*" not supported for types "Literal[2]" and "Relational"
-     Operator "*" not supported for types "Literal[2]" and "Equality" (reportOperatorIssue)
+     Operator "*" not supported for types "Literal[2]" and "Unequality" (reportOperatorIssue)
-   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_wester.py:2978:17 - error: Operator "*" not supported for types "Literal[2]" and "Equality | Unknown | NaN | Sum"
+   /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_wester.py:2978:17 - error: Operator "*" not supported for types "Literal[2]" and "Equality | Unknown | Relational | Unequality | Integer | NaN | Sum"
+     Operator "*" not supported for types "Literal[2]" and "Equality"
+     Operator "*" not supported for types "Literal[2]" and "Relational"
-     Operator "*" not supported for types "Literal[2]" and "Equality" (reportOperatorIssue)
+     Operator "*" not supported for types "Literal[2]" and "Unequality" (reportOperatorIssue)
-   /tmp/mypy_primer/projects/sympy/sympy/vector/dyadic.py:233:7 - error: Base classes for class "DyadicMul" define method "as_content_primitive" in incompatible way
-     Return type mismatch: base method returns type "tuple[One, Self@Expr]", override returns type "tuple[One | NegativeOne | Zero | Integer | Rational | NaN | ComplexInfinity | Half | Unknown, Self@Mul]"
-       "tuple[One | NegativeOne | Zero | Integer | Rational | NaN | ComplexInfinity | Half | Unknown, Self@Mul]" is not assignable to "tuple[One, Self@Expr]"
-         Tuple entry 1 is incorrect type
-           Type "One | NegativeOne | Zero | Integer | Rational | NaN | ComplexInfinity | Half | Unknown" is not assignable to type "One"
-             "ComplexInfinity" is not assignable to "One" (reportIncompatibleMethodOverride)

... (truncated 85 lines) ...

antidote (https://github.com/Finistere/antidote)
+   /tmp/mypy_primer/projects/antidote/src/antidote/core/_catalog.py:302:27 - error: Argument type is partially unknown
+     Argument corresponds to parameter "obj" in function "callable"
+     Argument type is "Any | type[Unknown]" (reportUnknownArgumentType)
- 742 errors, 77 warnings, 0 informations 
+ 743 errors, 77 warnings, 0 informations 

discord.py (https://github.com/Rapptz/discord.py)
-   /tmp/mypy_primer/projects/discord.py/discord/app_commands/transformers.py:785:27 - error: Cannot access attribute "__args__" for class "type[object]"
-     Attribute "__args__" is unknown (reportAttributeAccessIssue)
-   /tmp/mypy_primer/projects/discord.py/discord/app_commands/transformers.py:789:26 - error: Cannot access attribute "__args__" for class "type[object]"
-     Attribute "__args__" is unknown (reportAttributeAccessIssue)
-   /tmp/mypy_primer/projects/discord.py/discord/app_commands/transformers.py:797:23 - error: Cannot access attribute "__args__" for class "type[object]"
-     Attribute "__args__" is unknown (reportAttributeAccessIssue)
-   /tmp/mypy_primer/projects/discord.py/discord/ext/commands/converter.py:1335:26 - error: Expected 0 positional arguments (reportCallIssue)
- 108 errors, 96 warnings, 0 informations 
+ 104 errors, 96 warnings, 0 informations 

pydantic (https://github.com/pydantic/pydantic)
+   /tmp/mypy_primer/projects/pydantic/pydantic/_internal/_generate_schema.py:1067:43 - error: Argument of type "DataclassInstance | type[DataclassInstance]" cannot be assigned to parameter "dataclass" of type "type[DataclassInstance]" in function "_dataclass_schema"
+     Type "DataclassInstance | type[DataclassInstance]" is not assignable to type "type[DataclassInstance]"
+       Type "DataclassInstance" is not assignable to type "type[DataclassInstance]" (reportArgumentType)
- 265 errors, 45 warnings, 0 informations 
+ 266 errors, 45 warnings, 0 informations 

trio (https://github.com/python-trio/trio)
+   /tmp/mypy_primer/projects/trio/src/trio/_tests/test_exports.py:580:35 - error: Argument type is partially unknown
+     Argument corresponds to parameter "cls" in function "class_is_final"
+     Argument type is "type[Unknown]" (reportUnknownArgumentType)
- 3962 errors, 55 warnings, 0 informations 
+ 3963 errors, 55 warnings, 0 informations 

mitmproxy (https://github.com/mitmproxy/mitmproxy)
-   /tmp/mypy_primer/projects/mitmproxy/mitmproxy/proxy/events.py:97:9 - error: Argument of type "Command | Any | type[object] | type[Command]" cannot be assigned to parameter "key" of type "Command" in function "__setitem__"
+   /tmp/mypy_primer/projects/mitmproxy/mitmproxy/proxy/events.py:97:9 - error: Argument of type "Command | Any | type[Unknown] | type[Command]" cannot be assigned to parameter "key" of type "Command" in function "__setitem__"
-     Type "Command | Any | type[object] | type[Command]" is not assignable to type "Command"
+     Type "Command | Any | type[Unknown] | type[Command]" is not assignable to type "Command"
-       Type "type[object]" is not assignable to type "Command" (reportArgumentType)
+       "type[Unknown]" is not assignable to "Command" (reportArgumentType)

websockets (https://github.com/aaugustin/websockets)
-   /tmp/mypy_primer/projects/websockets/tests/extensions/test_permessage_deflate.py:575:51 - error: Argument of type "Unknown | type[object]" cannot be assigned to parameter "remote_no_context_takeover" of type "bool" in function "__init__"
+   /tmp/mypy_primer/projects/websockets/tests/extensions/test_permessage_deflate.py:575:51 - error: Argument of type "Unknown | type[Unknown]" cannot be assigned to parameter "remote_no_context_takeover" of type "bool" in function "__init__"
-     Type "Unknown | type[object]" is not assignable to type "bool"
+     Type "Unknown | type[Unknown]" is not assignable to type "bool"
-       Type "type[object]" is not assignable to type "bool" (reportArgumentType)
+       "type[Unknown]" is not assignable to "bool" (reportArgumentType)
-   /tmp/mypy_primer/projects/websockets/tests/extensions/test_permessage_deflate.py:575:51 - error: Argument of type "Unknown | type[object]" cannot be assigned to parameter "local_no_context_takeover" of type "bool" in function "__init__"
+   /tmp/mypy_primer/projects/websockets/tests/extensions/test_permessage_deflate.py:575:51 - error: Argument of type "Unknown | type[Unknown]" cannot be assigned to parameter "local_no_context_takeover" of type "bool" in function "__init__"
-     Type "Unknown | type[object]" is not assignable to type "bool"
+     Type "Unknown | type[Unknown]" is not assignable to type "bool"
-       Type "type[object]" is not assignable to type "bool" (reportArgumentType)
+       "type[Unknown]" is not assignable to "bool" (reportArgumentType)
-   /tmp/mypy_primer/projects/websockets/tests/extensions/test_permessage_deflate.py:575:51 - error: Argument of type "Unknown | type[object]" cannot be assigned to parameter "remote_max_window_bits" of type "int" in function "__init__"
+   /tmp/mypy_primer/projects/websockets/tests/extensions/test_permessage_deflate.py:575:51 - error: Argument of type "Unknown | type[Unknown]" cannot be assigned to parameter "remote_max_window_bits" of type "int" in function "__init__"
-     Type "Unknown | type[object]" is not assignable to type "int"
+     Type "Unknown | type[Unknown]" is not assignable to type "int"
-       Type "type[object]" is not assignable to type "int" (reportArgumentType)
+       "type[Unknown]" is not assignable to "int" (reportArgumentType)
-   /tmp/mypy_primer/projects/websockets/tests/extensions/test_permessage_deflate.py:575:51 - error: Argument of type "Unknown | type[object]" cannot be assigned to parameter "local_max_window_bits" of type "int" in function "__init__"
+   /tmp/mypy_primer/projects/websockets/tests/extensions/test_permessage_deflate.py:575:51 - error: Argument of type "Unknown | type[Unknown]" cannot be assigned to parameter "local_max_window_bits" of type "int" in function "__init__"
-     Type "Unknown | type[object]" is not assignable to type "int"
+     Type "Unknown | type[Unknown]" is not assignable to type "int"
-       Type "type[object]" is not assignable to type "int" (reportArgumentType)
+       "type[Unknown]" is not assignable to "int" (reportArgumentType)
-   /tmp/mypy_primer/projects/websockets/tests/extensions/test_permessage_deflate.py:575:51 - error: Argument of type "Unknown | type[object]" cannot be assigned to parameter "compress_settings" of type "dict[Any, Any] | None" in function "__init__"
+   /tmp/mypy_primer/projects/websockets/tests/extensions/test_permessage_deflate.py:575:51 - error: Argument of type "Unknown | type[Unknown]" cannot be assigned to parameter "compress_settings" of type "dict[Any, Any] | None" in function "__init__"
-     Type "Unknown | type[object]" is not assignable to type "dict[Any, Any] | None"
+     Type "Unknown | type[Unknown]" is not assignable to type "dict[Any, Any] | None"
-       Type "type[object]" is not assignable to type "dict[Any, Any] | None"
+       Type "type[Unknown]" is not assignable to type "dict[Any, Any] | None"
-         Type "type[object]" is not assignable to type "dict[Any, Any]"
+         "type[Unknown]" is not assignable to "dict[Any, Any]"
-         Type is not assignable to "None" (reportArgumentType)
+         "type[Unknown]" is not assignable to "None" (reportArgumentType)
-   /tmp/mypy_primer/projects/websockets/tests/extensions/test_permessage_deflate.py:933:51 - error: Argument of type "Unknown | type[object]" cannot be assigned to parameter "remote_no_context_takeover" of type "bool" in function "__init__"
+   /tmp/mypy_primer/projects/websockets/tests/extensions/test_permessage_deflate.py:933:51 - error: Argument of type "Unknown | type[Unknown]" cannot be assigned to parameter "remote_no_context_takeover" of type "bool" in function "__init__"
-     Type "Unknown | type[object]" is not assignable to type "bool"
+     Type "Unknown | type[Unknown]" is not assignable to type "bool"
-       Type "type[object]" is not assignable to type "bool" (reportArgumentType)
+       "type[Unknown]" is not assignable to "bool" (reportArgumentType)
-   /tmp/mypy_primer/projects/websockets/tests/extensions/test_permessage_deflate.py:933:51 - error: Argument of type "Unknown | type[object]" cannot be assigned to parameter "local_no_context_takeover" of type "bool" in function "__init__"
+   /tmp/mypy_primer/projects/websockets/tests/extensions/test_permessage_deflate.py:933:51 - error: Argument of type "Unknown | type[Unknown]" cannot be assigned to parameter "local_no_context_takeover" of type "bool" in function "__init__"
-     Type "Unknown | type[object]" is not assignable to type "bool"
+     Type "Unknown | type[Unknown]" is not assignable to type "bool"
-       Type "type[object]" is not assignable to type "bool" (reportArgumentType)
+       "type[Unknown]" is not assignable to "bool" (reportArgumentType)
-   /tmp/mypy_primer/projects/websockets/tests/extensions/test_permessage_deflate.py:933:51 - error: Argument of type "Unknown | type[object]" cannot be assigned to parameter "remote_max_window_bits" of type "int" in function "__init__"
+   /tmp/mypy_primer/projects/websockets/tests/extensions/test_permessage_deflate.py:933:51 - error: Argument of type "Unknown | type[Unknown]" cannot be assigned to parameter "remote_max_window_bits" of type "int" in function "__init__"
-     Type "Unknown | type[object]" is not assignable to type "int"
+     Type "Unknown | type[Unknown]" is not assignable to type "int"
-       Type "type[object]" is not assignable to type "int" (reportArgumentType)
+       "type[Unknown]" is not assignable to "int" (reportArgumentType)
-   /tmp/mypy_primer/projects/websockets/tests/extensions/test_permessage_deflate.py:933:51 - error: Argument of type "Unknown | type[object]" cannot be assigned to parameter "local_max_window_bits" of type "int" in function "__init__"
+   /tmp/mypy_primer/projects/websockets/tests/extensions/test_permessage_deflate.py:933:51 - error: Argument of type "Unknown | type[Unknown]" cannot be assigned to parameter "local_max_window_bits" of type "int" in function "__init__"
-     Type "Unknown | type[object]" is not assignable to type "int"
+     Type "Unknown | type[Unknown]" is not assignable to type "int"
-       Type "type[object]" is not assignable to type "int" (reportArgumentType)
+       "type[Unknown]" is not assignable to "int" (reportArgumentType)
-   /tmp/mypy_primer/projects/websockets/tests/extensions/test_permessage_deflate.py:933:51 - error: Argument of type "Unknown | type[object]" cannot be assigned to parameter "compress_settings" of type "dict[Any, Any] | None" in function "__init__"
+   /tmp/mypy_primer/projects/websockets/tests/extensions/test_permessage_deflate.py:933:51 - error: Argument of type "Unknown | type[Unknown]" cannot be assigned to parameter "compress_settings" of type "dict[Any, Any] | None" in function "__init__"
-     Type "Unknown | type[object]" is not assignable to type "dict[Any, Any] | None"
+     Type "Unknown | type[Unknown]" is not assignable to type "dict[Any, Any] | None"
-       Type "type[object]" is not assignable to type "dict[Any, Any] | None"
+       Type "type[Unknown]" is not assignable to type "dict[Any, Any] | None"
-         Type "type[object]" is not assignable to type "dict[Any, Any]"
+         "type[Unknown]" is not assignable to "dict[Any, Any]"
-         Type is not assignable to "None" (reportArgumentType)
+         "type[Unknown]" is not assignable to "None" (reportArgumentType)

@erictraut erictraut merged commit 575c639 into main Jan 14, 2025
18 checks passed
@erictraut erictraut deleted the issue-9656 branch January 14, 2025 20:05
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