Fix false positives on property setters during variance inference by crypt0lith · Pull Request #11840 · microsoft/pyright · GitHub
Skip to content

Fix false positives on property setters during variance inference - #11840

Open
crypt0lith wants to merge 1 commit into
microsoft:mainfrom
crypt0lith:fix/incomplete-decorator-caching
Open

crypt0lith wants to merge 1 commit into
microsoft:mainfrom
crypt0lith:fix/incomplete-decorator-caching

Conversation

@crypt0lith

Copy link
Copy Markdown
Contributor

Summary

Fixes #11835

Since #9102 (1.1.383), specializing a generic type alias infers its variance. When a property getter's return annotation specializes an alias that refers back to a class with inferred variance, pyright reports the setter's decorator as an unknown attribute of a function and the getter as obscured.

Root cause

Inferring the class's variance evaluates the declared type of each member. For a property, that is the setter, whose decorator refers to the getter while the getter is still being decorated, so the decorator's base type is an incomplete function.

  • getTypeOfMemberAccessWithBaseType looks up an attribute of a function on the function or MethodType class, but passes that lookup a base type without isIncomplete, so it reports the missing attribute.
  • getTypeOfFunction caches the decorated type as final even when a decorator's type was incomplete. The setter's decorated type stays Any after the getter is complete, and the redeclaration check reports the getter as obscured.

Fix

The function member lookup keeps the base type's incompleteness, and a decorated type built from an incomplete decorator is cached as incomplete, so it is evaluated again once the getter is complete.

Test

diagnostics.propertySetterVarianceInference.fourslash.ts, a fourslash test because the sample-file harness evaluates the getter before the checker reaches the annotation:

  • test1.py: a getter and setter whose return annotation specializes an alias referring to the class. Must stay clean.
  • test2.py: the same with a misspelled setter attribute. Must still flag it as an unknown attribute of property, and the getter as obscured.

When a decorator's type is incomplete, getTypeOfFunction caches the
decorated type as final, and member access on a function drops the
base type's incompleteness before reporting a missing attribute.

Cache the decorated type as incomplete when any decorator is, and
keep the base type's incompleteness in the function member lookup.

Fixes microsoft#11835
@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

This branch had an error being deployed

1 failed deployment
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.

Property in a PEP 695 generic class is reported as redeclared when its type is an alias that refers back to the class

1 participant