Skip to content

Cover common isinstance calling patterns with some overloads#15954

Open
jonathandung wants to merge 3 commits into
python:mainfrom
jonathandung:patch-4
Open

Cover common isinstance calling patterns with some overloads#15954
jonathandung wants to merge 3 commits into
python:mainfrom
jonathandung:patch-4

Conversation

@jonathandung

Copy link
Copy Markdown
Contributor

Closes #15844.

@github-actions

Copy link
Copy Markdown
Contributor

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

mypy (https://github.com/python/mypy)
+ mypy/types.py:4277: error: Argument 2 to "isinstance" has incompatible type "tuple[type[TypeAliasType], type[UnionType]]"; expected "type[Never] | tuple[type[Never]]"  [arg-type]
+ mypy/types.py:4277: note: Error code "arg-type" not covered by "type: ignore[misc]" comment
+ mypy/memprofile.py:39: error: Argument 2 to "isinstance" has incompatible type "tuple[type[Node], type[Type]]"; expected "type[Never] | tuple[type[Never]]"  [arg-type]
+ mypy/memprofile.py:39: note: Error code "arg-type" not covered by "type: ignore[misc]" comment

artigraph (https://github.com/artigraph/artigraph)
- src/arti/internal/type_hints.py:139: error: Argument 2 has incompatible type "type[T@get_item_from_annotated]"; expected "T@lenient_issubclass"  [arg-type]
+ src/arti/internal/type_hints.py:139: error: Cannot call function of unknown type  [operator]

pydantic (https://github.com/pydantic/pydantic)
+ pydantic/_internal/_typing_extra.py:192: error: Argument 2 to "isinstance" has incompatible type "tuple[type[GenericAlias], Any]"; expected "type[Never] | tuple[type[Never]]"  [arg-type]

sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/util/inspect.py: note: In function "isgenericalias":
+ sphinx/util/inspect.py:451:28: error: Argument 2 to "isinstance" has incompatible type "tuple[type[GenericAlias], Any]"; expected "type[Never] | tuple[type[Never]]"  [arg-type]
+ sphinx/util/inspect.py:451:28: note: Error code "arg-type" not covered by "type: ignore[attr-defined]" comment

mongo-python-driver (https://github.com/mongodb/mongo-python-driver)
+ pymongo/synchronous/pool.py:381: error: No overload variant of "isinstance" matches argument types "MutableMapping[str, Any]", "Sequence[type[Any]]"  [call-overload]
+ pymongo/synchronous/pool.py:381: note: Error code "call-overload" not covered by "type: ignore[arg-type]" comment
+ pymongo/synchronous/pool.py:381: note: Possible overload variants:
+ pymongo/synchronous/pool.py:381: note:     def [_T] isinstance(object, type[_T] | tuple[type[_T]], /) -> TypeIs[_T]
+ pymongo/synchronous/pool.py:381: note:     def [_T, _T1] isinstance(object, tuple[type[_T], type[_T1]], /) -> TypeIs[_T | _T1]
+ pymongo/synchronous/pool.py:381: note:     def [_T, _T1, _T2] isinstance(object, tuple[type[_T], type[_T1], type[_T2]], /) -> TypeIs[_T | _T1 | _T2]
+ pymongo/synchronous/pool.py:381: note:     def [_T, _T1, _T2, _T3] isinstance(object, tuple[type[_T], type[_T1], type[_T2], type[_T3]], /) -> TypeIs[_T | _T1 | _T2 | _T3]
+ pymongo/synchronous/pool.py:381: note:     def [_T, _T1, _T2, _T3, _T4] isinstance(object, tuple[type[_T], type[_T1], type[_T2], type[_T3], type[_T4]], /) -> TypeIs[_T | _T1 | _T2 | _T3 | _T4]
+ pymongo/synchronous/pool.py:381: note:     def [_T, _T1, _T2, _T3, _T4, _T5] isinstance(object, tuple[type[_T], type[_T1], type[_T2], type[_T3], type[_T4], type[_T5]], /) -> TypeIs[_T | _T1 | _T2 | _T3 | _T4 | _T5]
+ pymongo/synchronous/pool.py:381: note:     def isinstance(object, _ClassInfo, /) -> bool
+ pymongo/asynchronous/pool.py:381: error: No overload variant of "isinstance" matches argument types "MutableMapping[str, Any]", "Sequence[type[Any]]"  [call-overload]
+ pymongo/asynchronous/pool.py:381: note: Error code "call-overload" not covered by "type: ignore[arg-type]" comment
+ pymongo/asynchronous/pool.py:381: note: Possible overload variants:
+ pymongo/asynchronous/pool.py:381: note:     def [_T] isinstance(object, type[_T] | tuple[type[_T]], /) -> TypeIs[_T]
+ pymongo/asynchronous/pool.py:381: note:     def [_T, _T1] isinstance(object, tuple[type[_T], type[_T1]], /) -> TypeIs[_T | _T1]
+ pymongo/asynchronous/pool.py:381: note:     def [_T, _T1, _T2] isinstance(object, tuple[type[_T], type[_T1], type[_T2]], /) -> TypeIs[_T | _T1 | _T2]
+ pymongo/asynchronous/pool.py:381: note:     def [_T, _T1, _T2, _T3] isinstance(object, tuple[type[_T], type[_T1], type[_T2], type[_T3]], /) -> TypeIs[_T | _T1 | _T2 | _T3]
+ pymongo/asynchronous/pool.py:381: note:     def [_T, _T1, _T2, _T3, _T4] isinstance(object, tuple[type[_T], type[_T1], type[_T2], type[_T3], type[_T4]], /) -> TypeIs[_T | _T1 | _T2 | _T3 | _T4]
+ pymongo/asynchronous/pool.py:381: note:     def [_T, _T1, _T2, _T3, _T4, _T5] isinstance(object, tuple[type[_T], type[_T1], type[_T2], type[_T3], type[_T4], type[_T5]], /) -> TypeIs[_T | _T1 | _T2 | _T3 | _T4 | _T5]
+ pymongo/asynchronous/pool.py:381: note:     def isinstance(object, _ClassInfo, /) -> bool

@jonathandung jonathandung marked this pull request as ready for review June 27, 2026 10:05
@srittau

srittau commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

Unfortunately, it seems that mypy doesn't like the tuple overloads, for unknown reasons. Maybe the easiest first step is to just support the non-tuple version and leave all tuple variants as bool.

@OutSquareCapital

Copy link
Copy Markdown

Unfortunately, it seems that mypy doesn't like the tuple overloads, for unknown reasons. Maybe the easiest first step is to just support the non-tuple version and leave all tuple variants as bool.

Unfortunate indeed. But if I can at least have the most common case covered, I'm happy😄

@jonathandung

jonathandung commented Jun 30, 2026

Copy link
Copy Markdown
Contributor Author

The mongo-python-driver diff is irrelevant (just an error code change).

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.

TypeIs for isinstance

3 participants