Skip to content

fix(analyzer): prevent metaclass base members from leaking onto class instances - #11690

Open
Henry Su (hsusul) wants to merge 1 commit into
microsoft:mainfrom
hsusul:fix/metaclass-instance-member-access
Open

fix(analyzer): prevent metaclass base members from leaking onto class instances#11690
Henry Su (hsusul) wants to merge 1 commit into
microsoft:mainfrom
hsusul:fix/metaclass-instance-member-access

Conversation

@hsusul

Copy link
Copy Markdown
Contributor

Closes #11548.

Summary of Changes

  1. In typeUtils.ts:
    • Updated getClassIterator so that SkipTypeBaseClass and SkipObjectBaseClass checks use isClass(specializedMroClass) (enabling them to apply when traversing class instances as well as instantiables).
    • In lookUpClassMember, forwarded SkipTypeBaseClass when searching metaclass for class instances (isClassInstance(classType)).
  2. In typeEvaluator.ts:
    • When searching metaclass for class instances in getTypeOfClassMemberName, included MemberAccessFlags.SkipBaseClasses alongside SkipTypeBaseClass.
  3. Added sample test metaclass12.py and test suite entry in typeEvaluator1.test.ts verifying that:
    • a.__dict__ on an instance a: A (where class A(metaclass=Meta)) resolves to dict[str, Any].
    • Access to metaclass methods on instance a.meta_method() generates an attribute error.
    • Access via class A.__dict__ and A.meta_method() continues to resolve via the metaclass.

… instances

Class instances with custom metaclasses should not inherit members
from 'type' (such as type.__dict__ with MappingProxyType). Metaclass
base classes like 'type' should be skipped when resolving members on
class instances.
@bschnurr

Bill Schnurr (bschnurr) commented Aug 31, 2026

Copy link
Copy Markdown
Member

🔒 Automated review in progress — Bill Schnurr (@bschnurr) is auto-reviewing this PR.

@bschnurr

Copy link
Copy Markdown
Member

Verification: The relevant tests could not be fully run in the isolated environment; this review is not fully verified.

Result: could-not-verify

Summary: Verification could not run because no trusted sandbox image is configured for `microsoft/pyright`, and local execution was not authorized. The PR adds one targeted `Metaclass12` regression test covering instance and class metaclass-member resolution. No test result is available, so confidence is limited.

Test runs: 1 not run

  • ⚠️ Not run | Targeted test discovery and dependency preflight | git status --short && git diff --name-status upstream/main...HEAD && git diff --unified=20 upstream/main...HEAD -- packages/pyright-internal/src/tests/typeEvaluator1.test.ts packages/pyright-internal/src/tests/samples/metaclass12.py packages/pyright-internal/src/analyzer/typeUtils.ts packages/pyright-internal/src/analyzer/typeEvaluator.ts && if exist packages\pyright-internal\node_modules\jest\bin\jest.js (echo JEST_PRESENT) else (echo JEST_MISSING) && node --version && pnpm --version
⚠️ Targeted test discovery and dependency preflight diagnostic output
Container verification could not start: no trusted sandbox image is configured for microsoft/pyright. Local execution was not authorized for this PR HEAD.

@bschnurr Bill Schnurr (bschnurr) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approved via Review Center.

@bschnurr Bill Schnurr (bschnurr) added the review-auto:approved Automated review: no blocking findings (approval posted). label Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review-auto:approved Automated review: no blocking findings (approval posted).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1.1.408 regression: class instances with a custom metaclass are treated as types

2 participants