@zzzen/pyright-internal 1.2.0-dev.20260719 → 1.2.0-dev.20260726

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1864,6 +1864,39 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
1864
1864
  }
1865
1865
  return false;
1866
1866
  }
1867
+ // Given a member symbol and the context in which it was accessed, computes
1868
+ // the declared type of the member (applying descriptor setter types, partial
1869
+ // specialization, and function binding as appropriate). Returns undefined if
1870
+ // the symbol has no declared type.
1871
+ function resolveDeclaredMemberType(symbol, classOrObjectBase, memberAccessClass, useDescriptorSetterType, bindFunction, selfType) {
1872
+ let declaredType = getDeclaredTypeOfSymbol(symbol)?.type;
1873
+ if (!declaredType) {
1874
+ return undefined;
1875
+ }
1876
+ // If it's a descriptor, we need to get the setter type.
1877
+ if (useDescriptorSetterType && (0, types_1.isClassInstance)(declaredType)) {
1878
+ const setter = getBoundMagicMethod(declaredType, '__set__');
1879
+ if (setter && (0, types_1.isFunction)(setter) && setter.shared.parameters.length >= 2) {
1880
+ declaredType = types_1.FunctionType.getParamType(setter, 1);
1881
+ if ((0, types_1.isAnyOrUnknown)(declaredType)) {
1882
+ return undefined;
1883
+ }
1884
+ }
1885
+ }
1886
+ if (classOrObjectBase) {
1887
+ if (memberAccessClass && (0, types_1.isInstantiableClass)(memberAccessClass)) {
1888
+ declaredType = (0, typeUtils_1.partiallySpecializeType)(declaredType, memberAccessClass, getTypeClassType(), selfType);
1889
+ }
1890
+ if ((0, types_1.isFunctionOrOverloaded)(declaredType)) {
1891
+ if (bindFunction) {
1892
+ declaredType = bindFunctionToClassOrObject(classOrObjectBase, declaredType,
1893
+ /* memberClass */ undefined,
1894
+ /* treatConstructorAsClassMethod */ undefined, selfType);
1895
+ }
1896
+ }
1897
+ }
1898
+ return declaredType;
1899
+ }
1867
1900
  // Determines whether the specified expression is a symbol with a declared type.
1868
1901
  function getDeclaredTypeForExpression(expression, usage) {
1869
1902
  let symbol;
@@ -1903,10 +1936,37 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
1903
1936
  const baseType = getTypeOfExpression(expression.d.leftExpr, 2 /* EvalFlags.MemberAccessBaseDefaults */).type;
1904
1937
  const baseTypeConcrete = makeTopLevelTypeVarsConcrete(baseType);
1905
1938
  const memberName = expression.d.member.d.value;
1906
- // Normally, baseTypeConcrete will not be a composite type (a union),
1907
- // but this can occur. In this case, it's not clear how to handle this
1908
- // correctly. For now, we'll just loop through the subtypes and
1909
- // use one of them. We'll sort the subtypes for determinism.
1939
+ if ((0, types_1.isTypeVar)(baseType)) {
1940
+ selfType = baseType;
1941
+ }
1942
+ // Normally, baseType will not be a composite type (a union), but
1943
+ // this can occur. In this case, we compute the declared type of the
1944
+ // member for each subtype. If the subtypes declare the member with
1945
+ // the same generic class but different (invariant) type arguments
1946
+ // (e.g. "list[int]" vs. "list[str]"), there is no single declared
1947
+ // type that can serve as the expected type for bidirectional
1948
+ // inference of an assigned value. Committing to one subtype's
1949
+ // declared type produces a false positive when assigning a value
1950
+ // (such as an empty container) that is compatible with every
1951
+ // subtype, so in that case we return undefined and let the value be
1952
+ // evaluated without an expected type. We sort the subtypes for
1953
+ // determinism.
1954
+ //
1955
+ // This is deliberately limited to the "same class, differing type
1956
+ // arguments" case. When the subtypes declare the member with
1957
+ // unrelated types, we retain the previous behavior (use one
1958
+ // subtype's declared type) so that genuine assignment errors are
1959
+ // still reported at the same location and downstream inference is
1960
+ // unchanged.
1961
+ //
1962
+ // This handling is further limited to cases where the declared base
1963
+ // type is itself a union. We intentionally don't apply it when the
1964
+ // base is a type variable that merely concretizes to a union.
1965
+ const isUnionBase = (0, types_1.isUnion)(baseType);
1966
+ let firstMemberDeclaredType;
1967
+ let sawMemberDeclaredType = false;
1968
+ let hasDivergentMemberDeclaredTypes = false;
1969
+ let divergesOnlyByTypeArgs = true;
1910
1970
  (0, typeUtils_1.doForEachSubtype)(baseTypeConcrete, (baseSubtype) => {
1911
1971
  if ((0, types_1.isClassInstance)(baseSubtype)) {
1912
1972
  const classMemberInfo = (0, typeUtils_1.lookUpObjectMember)(baseSubtype, memberName, 64 /* MemberAccessFlags.DeclaredTypesOnly */);
@@ -1937,10 +1997,38 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
1937
1997
  useDescriptorSetterType = false;
1938
1998
  bindFunction = false;
1939
1999
  }
2000
+ // If the base is a union, verify that the subtypes agree on a
2001
+ // single declared type for the member.
2002
+ if (isUnionBase) {
2003
+ const subtypeDeclaredType = symbol
2004
+ ? resolveDeclaredMemberType(symbol, classOrObjectBase, memberAccessClass, useDescriptorSetterType, bindFunction, selfType)
2005
+ : undefined;
2006
+ if (subtypeDeclaredType) {
2007
+ if (!sawMemberDeclaredType) {
2008
+ firstMemberDeclaredType = subtypeDeclaredType;
2009
+ sawMemberDeclaredType = true;
2010
+ }
2011
+ else if (!firstMemberDeclaredType ||
2012
+ !(0, types_1.isTypeSame)(firstMemberDeclaredType, subtypeDeclaredType)) {
2013
+ hasDivergentMemberDeclaredTypes = true;
2014
+ // The false positive we're addressing is specific
2015
+ // to invariant type arguments of the same generic
2016
+ // class. If the divergent types aren't instances of
2017
+ // the same generic class, don't treat this as an
2018
+ // ambiguous declared type.
2019
+ if (!firstMemberDeclaredType ||
2020
+ !(0, types_1.isClassInstance)(firstMemberDeclaredType) ||
2021
+ !(0, types_1.isClassInstance)(subtypeDeclaredType) ||
2022
+ !types_1.ClassType.isSameGenericClass(types_1.ClassType.cloneAsInstantiable(firstMemberDeclaredType), types_1.ClassType.cloneAsInstantiable(subtypeDeclaredType))) {
2023
+ divergesOnlyByTypeArgs = false;
2024
+ }
2025
+ }
2026
+ }
2027
+ }
1940
2028
  },
1941
2029
  /* sortSubtypes */ true);
1942
- if ((0, types_1.isTypeVar)(baseType)) {
1943
- selfType = baseType;
2030
+ if (hasDivergentMemberDeclaredTypes && divergesOnlyByTypeArgs) {
2031
+ return undefined;
1944
2032
  }
1945
2033
  break;
1946
2034
  }
@@ -2009,32 +2097,7 @@ function createTypeEvaluator(importLookup, evaluatorOptions, wrapWithLogger) {
2009
2097
  }
2010
2098
  }
2011
2099
  if (symbol) {
2012
- let declaredType = getDeclaredTypeOfSymbol(symbol)?.type;
2013
- if (declaredType) {
2014
- // If it's a descriptor, we need to get the setter type.
2015
- if (useDescriptorSetterType && (0, types_1.isClassInstance)(declaredType)) {
2016
- const setter = getBoundMagicMethod(declaredType, '__set__');
2017
- if (setter && (0, types_1.isFunction)(setter) && setter.shared.parameters.length >= 2) {
2018
- declaredType = types_1.FunctionType.getParamType(setter, 1);
2019
- if ((0, types_1.isAnyOrUnknown)(declaredType)) {
2020
- return undefined;
2021
- }
2022
- }
2023
- }
2024
- if (classOrObjectBase) {
2025
- if (memberAccessClass && (0, types_1.isInstantiableClass)(memberAccessClass)) {
2026
- declaredType = (0, typeUtils_1.partiallySpecializeType)(declaredType, memberAccessClass, getTypeClassType(), selfType);
2027
- }
2028
- if ((0, types_1.isFunctionOrOverloaded)(declaredType)) {
2029
- if (bindFunction) {
2030
- declaredType = bindFunctionToClassOrObject(classOrObjectBase, declaredType,
2031
- /* memberClass */ undefined,
2032
- /* treatConstructorAsClassMethod */ undefined, selfType);
2033
- }
2034
- }
2035
- }
2036
- return declaredType;
2037
- }
2100
+ return resolveDeclaredMemberType(symbol, classOrObjectBase, memberAccessClass, useDescriptorSetterType, bindFunction, selfType);
2038
2101
  }
2039
2102
  return undefined;
2040
2103
  }