Skip to content

fix: add __instancecheck__ and __subclasscheck__ to _DeprecatedProxy#126

Merged
Borda merged 4 commits into
mainfrom
fix/proxy-isinstance
Mar 3, 2026
Merged

fix: add __instancecheck__ and __subclasscheck__ to _DeprecatedProxy#126
Borda merged 4 commits into
mainfrom
fix/proxy-isinstance

Conversation

@Borda

@Borda Borda commented Mar 3, 2026

Copy link
Copy Markdown
Owner
Before submitting
  • Was this discussed/approved via a Github issue? (no need for typos and docs improvements)
  • Did you make sure to update the docs?
  • Did you write any new necessary tests?

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 🙃

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>
Copilot AI review requested due to automatic review settings March 3, 2026 15:50
@dosubot

dosubot Bot commented Mar 3, 2026

Copy link
Copy Markdown
Contributor

Documentation Updates

1 document(s) were updated by changes in this PR:

README
View 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

How did I do? Any feedback?  Join Discord

@dosubot dosubot Bot added the bug / fix Something isn't working label Mar 3, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 _DeprecatedProxy to support isinstance/issubclass when a proxy is used as the RHS.
  • Add unit tests covering isinstance / issubclass delegation 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.

Comment thread src/deprecate/proxy.py
Comment thread src/deprecate/proxy.py Outdated
Comment thread tests/unittests/test_proxy.py
Comment thread tests/unittests/test_proxy.py
@Borda Borda merged commit d39def9 into main Mar 3, 2026
34 checks passed
@Borda Borda deleted the fix/proxy-isinstance branch March 3, 2026 16:20
dosubot Bot added a commit that referenced this pull request Mar 3, 2026
@dosubot dosubot Bot mentioned this pull request Mar 3, 2026
Borda pushed a commit that referenced this pull request Mar 3, 2026
Co-authored-by: dosubot[bot] <131922026+dosubot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug / fix Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants