Skip to content

[ty] Detect invalid isinstance() and issubclass() calls against TypedDict classes#22887

Merged
AlexWaygood merged 1 commit intomainfrom
alex/typeddict-isinstance
Jan 27, 2026
Merged

[ty] Detect invalid isinstance() and issubclass() calls against TypedDict classes#22887
AlexWaygood merged 1 commit intomainfrom
alex/typeddict-isinstance

Conversation

@AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Jan 27, 2026

Summary

These fail at runtime, and there are tests in the spec that validate that type checkers catch the runtime error.

Test Plan

mdtests

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Jan 27, 2026
@AlexWaygood AlexWaygood force-pushed the alex/typeddict-isinstance branch from aded5bf to 59f74eb Compare January 27, 2026 15:13
@astral-sh-bot
Copy link

astral-sh-bot bot commented Jan 27, 2026

Typing conformance results improved 🎉

The percentage of diagnostics emitted that were expected errors increased from 78.80% to 78.82%. The percentage of expected errors that received a diagnostic increased from 69.23% to 69.32%.

Summary

Metric Old New Diff Outcome
True Positives 747 748 +1 ⏫ (✅)
False Positives 201 201 +0
False Negatives 332 331 -1 ⏬ (✅)
Total Diagnostics 948 949 +1
Precision 78.80% 78.82% +0.02% ⏫ (✅)
Recall 69.23% 69.32% +0.09% ⏫ (✅)

True positives added

Details
Location Name Message
typeddicts_usage.py:35:4 isinstance-against-typed-dict TypedDict class Movie cannot be used as the second argument to isinstance: This call will raise TypeError at runtime

@astral-sh-bot
Copy link

astral-sh-bot bot commented Jan 27, 2026

mypy_primer results

Changes were detected when running on open source projects
prefect (https://github.com/PrefectHQ/prefect)
- src/prefect/deployments/runner.py:997:70: warning[possibly-missing-attribute] Attribute `__name__` may be missing on object of type `Unknown | (((...) -> Any) & ((*args: object, **kwargs: object) -> object))`
+ src/prefect/deployments/runner.py:997:70: warning[possibly-missing-attribute] Attribute `__name__` may be missing on object of type `Unknown | ((...) -> Any)`
+ src/prefect/flow_engine.py:989:32: error[invalid-await] `Unknown | R@FlowRunEngine | Coroutine[Any, Any, R@FlowRunEngine]` is not awaitable
+ src/prefect/flow_engine.py:1580:24: error[invalid-await] `Unknown | R@AsyncFlowRunEngine | Coroutine[Any, Any, R@AsyncFlowRunEngine]` is not awaitable
+ src/prefect/flow_engine.py:1661:43: error[invalid-argument-type] Argument to function `next` is incorrect: Expected `SupportsNext[Unknown]`, found `Unknown | R@run_generator_flow_sync`
+ src/prefect/flow_engine.py:1669:21: warning[possibly-missing-attribute] Attribute `throw` may be missing on object of type `Unknown | R@run_generator_flow_sync`
+ src/prefect/flow_engine.py:1703:44: warning[possibly-missing-attribute] Attribute `__anext__` may be missing on object of type `Unknown | R@run_generator_flow_async`
+ src/prefect/flow_engine.py:1710:25: warning[possibly-missing-attribute] Attribute `throw` may be missing on object of type `Unknown | R@run_generator_flow_async`
- src/prefect/flows.py:285:34: error[unresolved-attribute] Object of type `((**P@Flow) -> R@Flow) & ((*args: object, **kwargs: object) -> object)` has no attribute `__name__`
+ src/prefect/flows.py:285:34: error[unresolved-attribute] Object of type `(**P@Flow) -> R@Flow` has no attribute `__name__`
- src/prefect/flows.py:403:68: error[unresolved-attribute] Object of type `((**P@Flow) -> R@Flow) & ((*args: object, **kwargs: object) -> object)` has no attribute `__name__`
+ src/prefect/flows.py:403:68: error[unresolved-attribute] Object of type `(**P@Flow) -> R@Flow` has no attribute `__name__`
- src/prefect/flows.py:1877:53: warning[unused-ignore-comment] Unused blanket `type: ignore` directive
+ src/prefect/flows.py:1937:21: error[no-matching-overload] No overload of function `run_coro_as_sync` matches arguments
- Found 5362 diagnostics
+ Found 5368 diagnostics

pandas-stubs (https://github.com/pandas-dev/pandas-stubs)
- tests/frame/test_groupby.py:229:15: error[type-assertion-failure] Type `Series[Any]` does not match asserted type `Series[str | bytes | int | ... omitted 12 union elements]`
- tests/frame/test_groupby.py:625:15: error[type-assertion-failure] Type `Series[Any]` does not match asserted type `Series[str | bytes | int | ... omitted 12 union elements]`
- Found 4413 diagnostics
+ Found 4411 diagnostics

No memory usage changes detected ✅

@AlexWaygood AlexWaygood marked this pull request as ready for review January 27, 2026 15:20
@AlexWaygood AlexWaygood force-pushed the alex/typeddict-isinstance branch from 59f74eb to d970b69 Compare January 27, 2026 15:24
@AlexWaygood AlexWaygood merged commit 2d76a61 into main Jan 27, 2026
49 checks passed
@AlexWaygood AlexWaygood deleted the alex/typeddict-isinstance branch January 27, 2026 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants