Repository navigation
Fix #11453: no error on accessing __qualname__ of instance - #11503
Conversation
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]>
This comment has been minimized.
This comment has been minimized.
|
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)
|
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]>
|
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 ( Fix: inject the implicit Added regression coverage to |
This comment has been minimized.
This comment has been minimized.
|
Follow-up on the mypy_primer
Mechanism of the fix (73f5461): the implicit |
|
Solid, surgical fix for instance |
Heejae Chang (heejaechang)
left a comment
There was a problem hiding this comment.
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]>
|
Looks good — clean root-cause fix in the binder (injecting |
Heejae Chang (heejaechang)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
|
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
|
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 metaclasstype, not as an instance attribute, so instance access raisesAttributeError. Class-object access (MyClass.__qualname__) is and should remain valid.How you figured out what to do
The existing sample
classes3.pyalready documented this exact bug at theinstance.__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,_addSymbolToCurrentScopestamps it withSymbolFlags.ClassMember. Instance member-access resolution (getClassMemberIterator) treats anyisClassMember()symbol as accessible, so the error was suppressed. Confirmed via debugger that the symbol hadisClassMember()=true/isInstanceMember()=false, and via CPython thatA().__qualname__raisesAttributeError.Implementation
Threaded an optional
isClassMemberparameter (defaulttrue) through_addImplicitSymbolToCurrentScope→_addSymbolToCurrentScope. TheClassMemberflag is now only set when the scope is a Class andisClassMemberis 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 metaclasstype.__qualname__: str.__doc__/__module__are unchanged (they are legitimately instance-accessible).Testing
classes3.py:instance.__qualname__now expected error; addedreveal_type(..., expected_text="str")guards for class-object access; added subclass, nested/inner class, andtype[_T]generic cases. BumpedClasses3expected error count 3 → 6.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.Addresses
Fixes #11453
Generated by fix_all_my_issues pipeline