fix: add __instancecheck__ and __subclasscheck__ to _DeprecatedProxy#126
Merged
Conversation
isinstance(x, proxy) and issubclass(Sub, proxy) raised TypeError when a deprecated_class() proxy was used as the second argument, because _DeprecatedProxy is an instance, not a type. Add __instancecheck__ and __subclasscheck__ instance methods that delegate to the active class (target if set, otherwise wrapped obj). Neither method emits a deprecation warning — type checks are structural and must not consume the warning budget or produce noise in loops. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Contributor
|
Documentation Updates 1 document(s) were updated by changes in this PR: READMEView Changes@@ -563,6 +563,9 @@
access and forwards attribute, item, and call operations to the replacement class.
Use `args_mapping` to rename or drop kwargs when the deprecated class is called.
+> [!NOTE]
+> Type checks with `isinstance()` and `issubclass()` work transparently with `@deprecated_class` proxies and do not emit deprecation warnings, as these are structural checks rather than actual usage of the deprecated API.
+
```python
from enum import Enum
from dataclasses import dataclass |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes isinstance(x, proxy) / issubclass(Sub, proxy) behavior when deprecated_class() returns an _DeprecatedProxy (an instance), by adding type-protocol hooks intended to delegate the checks to the active class without emitting deprecation warnings.
Changes:
- Add
__instancecheck__and__subclasscheck__to_DeprecatedProxyto supportisinstance/issubclasswhen a proxy is used as the RHS. - Add unit tests covering
isinstance/issubclassdelegation and the “no warnings during type checks” expectation (partially).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/deprecate/proxy.py |
Implements __instancecheck__ / __subclasscheck__ delegation for proxies. |
tests/unittests/test_proxy.py |
Adds new unit tests for the type-protocol behavior. |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
for more information, see https://pre-commit.ci
Borda
pushed a commit
that referenced
this pull request
Mar 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Before submitting
What does this PR do?
isinstance(x, proxy) and issubclass(Sub, proxy) raised TypeError when a deprecated_class() proxy was used as the second argument, because _DeprecatedProxy is an instance, not a type.
Add instancecheck and subclasscheck instance methods that delegate to the active class (target if set, otherwise wrapped obj). Neither method emits a deprecation warning — type checks are structural and must not consume the warning budget or produce noise in loops.
Part of #109
PR review
Anyone in the community is free to review the PR once the tests have passed.
If we didn't discuss your PR in Github issues there's a high chance it will not be merged.
Did you have fun?
Make sure you had fun coding 🙃