Skip to content

Fix #11453: no error on accessing __qualname__ of instance - #11503

Merged
Rich Chiodo (rchiodo) merged 4 commits into
microsoft:mainfrom
rchiodo:fix11453
Jun 30, 2026
Merged

Rich Chiodo (rchiodo) merged 4 commits into
microsoft:mainfrom
rchiodo:fix11453

Conversation

@rchiodo

@rchiodo Rich Chiodo (rchiodo) commented Jun 15, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Pyright/Pylance failed to report the expected "cannot access attribute" diagnostic when __qualname__ was accessed on an instance (e.g. MyClass().__qualname__). At runtime, __qualname__ is exposed via the metaclass type, not as an instance attribute, so instance access raises AttributeError. Class-object access (MyClass.__qualname__) is and should remain valid.

How you figured out what to do

The existing sample classes3.py already documented this exact bug at the instance.__qualname__ line. Reproduced it (no diagnostic emitted), then traced the root cause: the binder unconditionally injects an implicit __qualname__ symbol into each class scope's symbol table (binder.ts ~L510). In a Class scope, _addSymbolToCurrentScope stamps it with SymbolFlags.ClassMember. Instance member-access resolution (getClassMemberIterator) treats any isClassMember() symbol as accessible, so the error was suppressed. Confirmed via debugger that the symbol had isClassMember()=true / isInstanceMember()=false, and via CPython that A().__qualname__ raises AttributeError.

Implementation

Threaded an optional isClassMember parameter (default true) through _addImplicitSymbolToCurrentScope → _addSymbolToCurrentScope. The ClassMember flag is now only set when the scope is a Class and isClassMember is true. The __qualname__ injection passes /* isClassMember */ false, so it stays name-resolvable inside the class body (print(__qualname__)) but is no longer exposed as a class/instance member. Class.__qualname__ still resolves via the metaclass type.__qualname__: str. __doc__/__module__ are unchanged (they are legitimately instance-accessible).

Testing

  • Updated classes3.py: instance.__qualname__ now expected error; added reveal_type(..., expected_text="str") guards for class-object access; added subclass, nested/inner class, and type[_T] generic cases. Bumped Classes3 expected error count 3 ΓåÆ 6.
  • Added completions.qualname.fourslash.ts: instance completions exclude __qualname__ (but include __doc__/__module__); class completions include __qualname__.
  • typeEvaluator1-8 + checker (9 suites / 1234 tests) pass; fourslash completions/hover (333 tests) pass.
  • Prettier + ESLint clean on changed files.

Addresses

Fixes #11453


Generated by fix_all_my_issues pipeline

Rich Chiodo (rchiodo) and others added 2 commits June 15, 2026 10:58
The binder injects an implicit __qualname__ symbol into every class scope so
it can be referenced by name within the class body (e.g. print(__qualname__)).
Because the scope is a class scope, the symbol was flagged as a class member,
so member access on an instance resolved it and skipped the expected
attribute-access error. Unlike __doc__/__module__, __qualname__ is exposed via
the metaclass (type), not as a class/instance attribute.

Thread an optional isClassMember flag through the implicit-symbol helpers and
inject __qualname__ without the ClassMember flag. It stays name-resolvable in
the class body, instance access now reports an attribute-access error, and
class-object access still resolves via type.__qualname__: str.

Fixes microsoft#11453

Co-authored-by: Copilot <[email protected]>
@github-actions

This comment has been minimized.

@rchiodo

Copy link
Copy Markdown
Collaborator Author

It looks to me like this has introduced a whole number of failures along the lines of:

dd-trace-py (https://github.com/DataDog/dd-trace-py)

  • .../projects/dd-trace-py/ddtrace/contrib/internal/asyncio/patch.py:73:32 - error: "qualname" is not a known attribute of "None" (reportOptionalMemberAccess)
  • .../projects/dd-trace-py/ddtrace/vendor/debtcollector/_utils.py:137:52 - error: Cannot access attribute "qualname" for class "MethodType"
  • Attribute "__qualname__" is unknown (reportAttributeAccessIssue)
    
  • 14755 errors, 750 warnings, 0 informations
  • 14757 errors, 750 warnings, 0 informations

pydantic (https://github.com/pydantic/pydantic)

  • .../projects/pydantic/pydantic/_internal/_validate_call.py
  • .../projects/pydantic/pydantic/_internal/_validate_call.py:26:98 - error: Cannot access attribute "qualname" for class "MethodType"
  • Attribute "__qualname__" is unknown (reportAttributeAccessIssue)
    
  • 10 errors, 0 warnings, 0 informations
  • 11 errors, 0 warnings, 0 informations

Inject the implicit __qualname__ symbol after walking the class suite so a
class that explicitly declares __qualname__ (e.g. type, function, and other
typeshed classes) keeps the class-member symbol created for that declaration.

Previously the non-class-member symbol was created up front and the explicit
declaration merged into it without restoring the class-member flag, which hid
the legitimate member and caused reportAttributeAccessIssue errors on valid
instance.__qualname__ accesses (MethodType, function, etc.).

Co-authored-by: Copilot <[email protected]>
@rchiodo

Copy link
Copy Markdown
Collaborator Author

Good catch — fixed in 73f5461.

Root cause: the implicit qualname symbol was injected into the class scope before the class suite was walked. For typeshed classes that legitimately declare qualname (function, MethodType, type, partial, etc.), that pre-created non-class-member symbol got the explicit declaration merged into it, but _bindNameValueToScope only sets the ClassMember flag when it creates a symbol — so the flag was never restored. The legitimate member was then hidden, producing the new reportAttributeAccessIssue errors on valid instance.__qualname__ accesses across the mypy_primer projects.

Fix: inject the implicit __qualname__ after walking the suite. Now an explicit declaration creates the symbol as a proper class member first, and the implicit injection only adds a non-class-member symbol for classes that don't declare one. Instance access on a plain class still errors (the original #11453 behavior), while function.__qualname__ / MethodType.__qualname__ / explicit __qualname__ members resolve again.

Added regression coverage to classes3.py for a function object's __qualname__ and a class that explicitly declares __qualname__. Classes3 and completions.qualname both pass locally.

@github-actions

This comment has been minimized.

@rchiodo

Copy link
Copy Markdown
Collaborator Author

Follow-up on the mypy_primer __qualname__ diff (the run posted after 73f5461 still lists __qualname__ errors, but they're now a different, expected set):

  • Fixed regressions (false positives that hid legitimate members): the MethodType / function / partial-via-_lru_cache_wrapper etc. cases from the original report are gone. These typeshed classes explicitly declare __qualname__: str, so instance.__qualname__ resolves again. Verified: Classes3 and completions.qualname pass, and dd-trace-py/pydantic no longer show the MethodType.__qualname__ error.

  • Remaining __qualname__ diffs are intended true positives of no error on accessing __qualname__ of instance #11453: object, str, None, and functools.partial do not declare __qualname__ as a class/instance attribute (it lives on the metaclass type). At runtime object().__qualname__, "x".__qualname__, and partial(f).__qualname__ all raise AttributeError, so flagging them (reportAttributeAccessIssue / reportOptionalMemberAccess) is the correct new behavior — these are pre-existing latent bugs in jax/streamlit/spack/dd-trace-py that the fix now surfaces.

Mechanism of the fix (73f5461): the implicit __qualname__ symbol is now injected after walking the class suite. A class that explicitly declares __qualname__ therefore creates the class-member symbol first; the implicit injection (with isClassMember=false) finds the existing symbol and leaves its ClassMember flag intact. Plain classes that don't declare it get a non-class-member symbol, so instance access still errors as intended.

@heejaechang

Copy link
Copy Markdown
Collaborator

Solid, surgical fix for instance __qualname__ access. A couple of test/metadata cleanups and one navigation behavior worth a quick verification, but nothing blocking.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approved via Review Center.

Only inject the implicit __qualname__ symbol when the class body did not
already declare it. Classes that explicitly declare __qualname__ (typeshed
type/function, or a user assignment) keep their real declaration as the
symbol's sole declaration, so hover/go-to-definition/completion resolve to
the real declaration instead of a synthetic empty-range Intrinsic decl
appended on top. Also reword the classes3.py comment to clarify this reflects
pyright's class-variable modeling rather than runtime behavior.

Co-authored-by: Copilot <[email protected]>
@heejaechang

Copy link
Copy Markdown
Collaborator

Looks good — clean root-cause fix in the binder (injecting __qualname__ after the suite walk as a non-class member, while preserving real declarations). Test coverage is solid. The WithQualname/annotation-only no-error cases diverge from CPython runtime, but that divergence is an intentional, clearly-documented consequence of pyright's general class-variable modeling rather than something this PR introduces. Approving.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approved via Review Center.

@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

sympy (https://github.com/sympy/sympy)
-   .../projects/sympy/sympy/solvers/ode/single.py:867:9 - error: Expression with type "tuple[ComplexInfinity | NaN | Rational | Zero | Infinity | NegativeInfinity | Float | NotImplementedType | Expr | Unknown | Any, list[tuple[Unknown, Unknown]] | list[Unknown]] | tuple[ComplexInfinity | NaN | Rational | Zero | Infinity | NegativeInfinity | Float | NotImplementedType | Expr | Unknown | Any, list[tuple[Unknown, Unknown]] | list[Unknown], list[tuple[Unknown, Unknown]] | list[Unknown]]" cannot be assigned to target tuple
-     Type "tuple[ComplexInfinity | NaN | Rational | Zero | Infinity | NegativeInfinity | Float | NotImplementedType | Expr | Unknown | Any, list[tuple[Unknown, Unknown]] | list[Unknown], list[tuple[Unknown, Unknown]] | list[Unknown]]" is incompatible with target tuple
+   .../projects/sympy/sympy/solvers/ode/single.py:867:9 - error: Expression with type "tuple[Unknown | Any | NaN | Expr | Infinity | NegativeInfinity | Zero | Float, list[tuple[Unknown, Unknown]] | list[Unknown]] | tuple[Unknown | Any | NaN | Expr | Infinity | NegativeInfinity | Zero | Float, list[tuple[Unknown, Unknown]] | list[Unknown], list[tuple[Unknown, Unknown]] | list[Unknown]]" cannot be assigned to target tuple
+     Type "tuple[Unknown | Any | NaN | Expr | Infinity | NegativeInfinity | Zero | Float, list[tuple[Unknown, Unknown]] | list[Unknown], list[tuple[Unknown, Unknown]] | list[Unknown]]" is incompatible with target tuple
-   .../projects/sympy/sympy/solvers/ode/single.py:1404:20 - error: Operator "/" not supported for types "Basic" and "Basic" (reportOperatorIssue)
-   .../projects/sympy/sympy/solvers/ode/single.py:1883:22 - error: "is_Rational" is not a known attribute of "None" (reportOptionalMemberAccess)
-   .../projects/sympy/sympy/solvers/ode/single.py:1886:22 - error: "is_Rational" is not a known attribute of "None" (reportOptionalMemberAccess)
-   .../projects/sympy/sympy/solvers/ode/single.py:1905:25 - error: Operator "*" not supported for types "Zero | One | Expr | Unknown | None" and "Zero | One | Expr | Unknown | None"
-     Operator "*" not supported for types "Expr" and "None"
-     Operator "*" not supported for types "None" and "Expr"
-     Operator "*" not supported for types "None" and "None" (reportOperatorIssue)
-   .../projects/sympy/sympy/solvers/ode/single.py:1905:33 - error: Operator "*" not supported for types "Zero | One | Expr | Unknown | None" and "Zero | One | Expr | Unknown | None"
-     Operator "*" not supported for types "Expr" and "None"
-     Operator "*" not supported for types "None" and "Expr"
-     Operator "*" not supported for types "None" and "None" (reportOperatorIssue)
-   .../projects/sympy/sympy/solvers/ode/single.py:1914:21 - error: Operator "*" not supported for "None" (reportOptionalOperand)
-   .../projects/sympy/sympy/solvers/ode/single.py:1914:29 - error: Operator "*" not supported for "None" (reportOptionalOperand)
-   .../projects/sympy/sympy/solvers/ode/single.py:1914:44 - error: Operator "*" not supported for "None" (reportOptionalOperand)
-   .../projects/sympy/sympy/solvers/ode/single.py:1914:52 - error: Operator "*" not supported for "None" (reportOptionalOperand)
+   .../projects/sympy/sympy/solvers/ode/single.py:2646:27 - error: Argument of type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Expr | Unknown | int" cannot be assigned to parameter "stop" of type "SupportsIndex" in function "__new__"
+     Type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Expr | Unknown | int" is not assignable to type "SupportsIndex"
+       "Expr" is incompatible with protocol "SupportsIndex"
+         "__index__" is not present (reportArgumentType)
-   .../projects/sympy/sympy/solvers/ode/single.py:2652:9 - error: Operator "+=" not supported for types "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Expr | Any | Unknown" and "Basic"
+   .../projects/sympy/sympy/solvers/ode/single.py:2652:9 - error: Operator "+=" not supported for types "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Expr | Any | Unknown" and "Basic | Any | Unknown"
-   .../projects/sympy/sympy/solvers/ode/single.py:2760:30 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+   .../projects/sympy/sympy/solvers/tests/test_solveset.py:445:30 - error: Cannot access attribute "limit_denominator" for class "NaN"
+     Attribute "limit_denominator" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/solvers/tests/test_solveset.py:445:30 - error: Cannot access attribute "limit_denominator" for class "ComplexInfinity"
+     Attribute "limit_denominator" is unknown (reportAttributeAccessIssue)
-   .../projects/sympy/sympy/stats/joint_rv_types.py:576:27 - error: Argument of type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Unknown | Infinity | NegativeInfinity | Float | Number | Expr" cannot be assigned to parameter "stop" of type "SupportsIndex" in function "__new__"
+   .../projects/sympy/sympy/stats/joint_rv_types.py:576:27 - error: Argument of type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Expr" cannot be assigned to parameter "stop" of type "SupportsIndex" in function "__new__"
-     Type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Unknown | Infinity | NegativeInfinity | Float | Number | Expr" is not assignable to type "SupportsIndex"
+     Type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Expr" is not assignable to type "SupportsIndex"
-   .../projects/sympy/sympy/stats/rv_interface.py:451:42 - error: Operator "-" not supported for types "Unknown | Basic | Any" and "Rational | Unknown"
+   .../projects/sympy/sympy/stats/rv_interface.py:451:42 - error: Operator "-" not supported for types "Unknown | Basic | Any" and "Rational | NaN | ComplexInfinity"
+     Operator "-" not supported for types "Basic" and "Rational"
+     Operator "-" not supported for types "Basic" and "NaN"
-     Operator "-" not supported for types "Basic" and "Rational" (reportOperatorIssue)
+     Operator "-" not supported for types "Basic" and "ComplexInfinity" (reportOperatorIssue)
-   .../projects/sympy/sympy/stats/tests/test_finite_rv.py:248:19 - error: Argument of type "Rational | Unknown" cannot be assigned to parameter "p" of type "Half" in function "Coin"
+   .../projects/sympy/sympy/stats/tests/test_finite_rv.py:248:19 - error: Argument of type "Rational | NaN | ComplexInfinity" cannot be assigned to parameter "p" of type "Half" in function "Coin"
-     Type "Rational | Unknown" is not assignable to type "Half"
+     Type "Rational | NaN | ComplexInfinity" is not assignable to type "Half"
-       "Rational" is not assignable to "Half" (reportArgumentType)
+       "ComplexInfinity" is not assignable to "Half" (reportArgumentType)
+   .../projects/sympy/sympy/utilities/tests/test_wester.py:402:38 - error: Cannot access attribute "limit_denominator" for class "NaN"
+     Attribute "limit_denominator" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/utilities/tests/test_wester.py:402:38 - error: Cannot access attribute "limit_denominator" for class "ComplexInfinity"
+     Attribute "limit_denominator" is unknown (reportAttributeAccessIssue)
-     Return type mismatch: base method returns type "tuple[One, Self@Expr]", override returns type "tuple[One | NegativeOne | Zero | Integer | Unknown | Rational, Expr | Unknown | Self@Add]"
+     Return type mismatch: base method returns type "tuple[One, Self@Expr]", override returns type "tuple[One | NegativeOne | Zero | Integer | Expr | Unknown | Rational | NaN | ComplexInfinity, Expr | Unknown | Self@Add]"
-       "tuple[One | NegativeOne | Zero | Integer | Unknown | Rational, Expr | Unknown | Self@Add]" is not assignable to "tuple[One, Self@Expr]"
+       "tuple[One | NegativeOne | Zero | Integer | Expr | Unknown | Rational | NaN | ComplexInfinity, Expr | Unknown | Self@Add]" is not assignable to "tuple[One, Self@Expr]"
-           Type "One | NegativeOne | Zero | Integer | Unknown | Rational" is not assignable to type "One"
+           Type "One | NegativeOne | Zero | Integer | Expr | Unknown | Rational | NaN | ComplexInfinity" is not assignable to type "One"
-             "Rational" is not assignable to "One" (reportIncompatibleMethodOverride)
+             "Expr" is not assignable to "One" (reportIncompatibleMethodOverride)
+   .../projects/sympy/sympy/vector/dyadic.py:253:7 - error: Base classes for class "DyadicAdd" define method "primitive" in incompatible way
+     Return type mismatch: base method returns type "tuple[Number, Expr]", override returns type "tuple[One, Self@Add] | tuple[Rational | NaN | ComplexInfinity, Unknown | Self@Add]"
+       Type "tuple[One, Self@Add] | tuple[Rational | NaN | ComplexInfinity, Unknown | Self@Add]" is not assignable to type "tuple[Number, Expr]"
+         "tuple[Rational | NaN | ComplexInfinity, Unknown | Self@Add]" is not assignable to "tuple[Number, Expr]"
+           Tuple entry 1 is incorrect type
+             Type "Rational | NaN | ComplexInfinity" is not assignable to type "Number" (reportIncompatibleMethodOverride)
-   .../projects/sympy/sympy/vector/implicitregion.py:188:18 - error: Operator "/" not supported for types "int | Unknown" and "Alg | Unknown | GaussianRational | MPQ | GaussianInteger | MPZ | Expr | Rational | One | NegativeOne | Zero | Integer | NaN | Infinity | NegativeInfinity | Float | Number | Poly"
-     Operator "/" not supported for types "int" and "MPZ" (reportOperatorIssue)

... (truncated 59 lines) ...

streamlit (https://github.com/streamlit/streamlit)
+   .../projects/streamlit/lib/streamlit/type_util.py:131:56 - error: Cannot access attribute "__qualname__" for class "object"
+     Attribute "__qualname__" is unknown (reportAttributeAccessIssue)
+   .../projects/streamlit/lib/streamlit/type_util.py:132:20 - error: Cannot access attribute "__qualname__" for class "object"
+     Attribute "__qualname__" is unknown (reportAttributeAccessIssue)
- 5725 errors, 116 warnings, 0 informations
+ 5727 errors, 116 warnings, 0 informations

dd-trace-py (https://github.com/DataDog/dd-trace-py)
+   .../projects/dd-trace-py/ddtrace/contrib/internal/asyncio/patch.py:73:32 - error: "__qualname__" is not a known attribute of "None" (reportOptionalMemberAccess)
- 15112 errors, 770 warnings, 0 informations
+ 15113 errors, 770 warnings, 0 informations

trio (https://github.com/python-trio/trio)
-   .../projects/trio/src/trio/_core/_run.py:1974:68 - error: Unnecessary "# type: ignore" comment (reportUnnecessaryTypeIgnoreComment)
+   .../projects/trio/src/trio/_deprecate.py:51:38 - error: Type of "__qualname__" is unknown (reportUnknownMemberType)
+   .../projects/trio/src/trio/_deprecate.py:51:44 - error: Cannot access attribute "__qualname__" for class "object"
+     Attribute "__qualname__" is unknown (reportAttributeAccessIssue)
+   .../projects/trio/src/trio/_util.py:221:25 - error: Cannot assign to attribute "__qualname__" for class "object"
+     Attribute "__qualname__" is unknown (reportAttributeAccessIssue)
- 1688 errors, 13 warnings, 0 informations
+ 1690 errors, 13 warnings, 0 informations

spack (https://github.com/spack/spack)
+   .../projects/spack/lib/spack/spack/build_environment.py:1517:37 - error: Cannot access attribute "__qualname__" for class "str"
+     Attribute "__qualname__" is unknown (reportAttributeAccessIssue)
- 2127 errors, 26 warnings, 0 informations
+ 2128 errors, 26 warnings, 0 informations

jax (https://github.com/google/jax)
+   .../projects/jax/jax/_src/api.py:1983:51 - error: Cannot access attribute "__qualname__" for class "partial[Unknown]"
+     Attribute "__qualname__" is unknown (reportAttributeAccessIssue)
+   .../projects/jax/jax/_src/util.py:551:11 - error: Cannot assign to attribute "__qualname__" for class "object*"
+     Attribute "__qualname__" is unknown (reportAttributeAccessIssue)
- 3441 errors, 90 warnings, 0 informations
+ 3443 errors, 90 warnings, 0 informations

@rchiodo
Rich Chiodo (rchiodo) merged commit 433e84d into microsoft:main Jun 30, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

no error on accessing __qualname__ of instance

2 participants