Skip to content

[ty] Fix false negative when using a non-runtime-checkable protocol in a match class pattern#22836

Merged
AlexWaygood merged 6 commits intoastral-sh:mainfrom
denyszhak:fix/false-negative-when-matching-a-protocol
Jan 25, 2026
Merged

[ty] Fix false negative when using a non-runtime-checkable protocol in a match class pattern#22836
AlexWaygood merged 6 commits intoastral-sh:mainfrom
denyszhak:fix/false-negative-when-matching-a-protocol

Conversation

@denyszhak
Copy link
Contributor

@denyszhak denyszhak commented Jan 24, 2026

Summary

Closes astral-sh/ty#2581

Emit invalid-argument-type when a non-runtime-checkable Protocol is used in a match class pattern (case Proto():), matching the existing behavior for isinstance() checks.

Test Plan

Added test cases in protocols.md covering the change:

  • Top-level class patterns with non-runtime-checkable protocols
  • Runtime-checkable protocols (no error)
  • Nested class patterns

@astral-sh-bot
Copy link

astral-sh-bot bot commented Jan 24, 2026

Typing conformance results

No changes detected ✅

@astral-sh-bot
Copy link

astral-sh-bot bot commented Jan 24, 2026

mypy_primer results

Changes were detected when running on open source projects
scikit-build-core (https://github.com/scikit-build/scikit-build-core)
- src/scikit_build_core/build/wheel.py:99:20: error[no-matching-overload] No overload of bound method `__init__` matches arguments
- Found 47 diagnostics
+ Found 46 diagnostics

rotki (https://github.com/rotki/rotki)
+ rotkehlchen/chain/decoding/tools.py:96:44: warning[unused-ignore-comment] Unused blanket `type: ignore` directive
- rotkehlchen/chain/decoding/tools.py:97:13: error[invalid-argument-type] Argument to function `decode_transfer_direction` is incorrect: Expected `BTCAddress | ChecksumAddress | SubstrateAddress | SolanaAddress`, found `A@BaseDecoderTools`
+ rotkehlchen/chain/decoding/tools.py:99:13: error[invalid-argument-type] Argument to function `decode_transfer_direction` is incorrect: Expected `Sequence[A@BaseDecoderTools]`, found `Unknown | tuple[BTCAddress, ...] | tuple[ChecksumAddress, ...] | tuple[SubstrateAddress, ...] | tuple[SolanaAddress, ...]`
- rotkehlchen/chain/decoding/tools.py:98:13: error[invalid-argument-type] Argument to function `decode_transfer_direction` is incorrect: Expected `BTCAddress | ChecksumAddress | SubstrateAddress | SolanaAddress | None`, found `A@BaseDecoderTools | None`
+ rotkehlchen/chain/decoding/tools.py:100:62: warning[unused-ignore-comment] Unused blanket `type: ignore` directive
- Found 2050 diagnostics
+ Found 2051 diagnostics

No memory usage changes detected ✅

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, thank you!

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Jan 24, 2026
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent! Just a few docs nits:

@AlexWaygood
Copy link
Member

(Oh, and you'll need to run cargo dev generate-all locally to regenerate the docs)

@denyszhak denyszhak force-pushed the fix/false-negative-when-matching-a-protocol branch from 248b0ee to 90f48d1 Compare January 25, 2026 11:45
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brilliant, thanks again!

@AlexWaygood AlexWaygood enabled auto-merge (squash) January 25, 2026 11:49
@AlexWaygood AlexWaygood disabled auto-merge January 25, 2026 11:49
@AlexWaygood AlexWaygood changed the title [ty] false negative when matching a protocol [ty] Fix false negative when using a non-runtime-checkable protocol in a match class pattern Jan 25, 2026
@AlexWaygood AlexWaygood merged commit e8e8235 into astral-sh:main Jan 25, 2026
44 checks passed
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.

False negative when matching a Protocol

2 participants