From 886cf5ab830e717c44bb7b9f3568433c0926f709 Mon Sep 17 00:00:00 2001 From: detachhead Date: Sat, 23 Nov 2024 00:54:35 +1000 Subject: [PATCH] rename `improvedGenericNarrowing` to be consistent with other similar options --- docs/configuration.md | 2 +- .../pyright-internal/src/analyzer/checker.ts | 2 +- .../src/analyzer/patternMatching.ts | 2 +- .../pyright-internal/src/analyzer/typeGuards.ts | 6 +++--- .../pyright-internal/src/analyzer/typeUtils.ts | 14 +++++++------- .../pyright-internal/src/common/configOptions.ts | 16 ++++++++-------- .../src/common/diagnosticRules.ts | 2 +- .../src/tests/typeEvaluator1.test.ts | 2 +- .../src/tests/typeEvaluatorBased.test.ts | 4 ++-- .../schemas/pyrightconfig.schema.json | 10 +++++----- 10 files changed, 30 insertions(+), 30 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index e3f6e3908b..4e92663f56 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -75,7 +75,7 @@ The following settings determine how different types should be evaluated. ### basedpyright exclusive settings -- **improvedGenericNarrowing** [boolean]: When a type is narrowed in such a way that its type parameters are not known (eg. using an `isinstance` check), basedpyright will resolve the type parameter to the generic's bound or constraint instead of `Any`. +- **strictGenericNarrowing** [boolean]: When a type is narrowed in such a way that its type parameters are not known (eg. using an `isinstance` check), basedpyright will resolve the type parameter to the generic's bound or constraint instead of `Any`. ## Diagnostic Categories diff --git a/packages/pyright-internal/src/analyzer/checker.ts b/packages/pyright-internal/src/analyzer/checker.ts index c449f22437..c0189ca0a9 100644 --- a/packages/pyright-internal/src/analyzer/checker.ts +++ b/packages/pyright-internal/src/analyzer/checker.ts @@ -3797,7 +3797,7 @@ export class Checker extends ParseTreeWalker { this._evaluator, arg1Type, arg0Type, - this._fileInfo.diagnosticRuleSet.improvedGenericNarrowing + this._fileInfo.diagnosticRuleSet.strictGenericNarrowing ); if (!classTypeList) { return; diff --git a/packages/pyright-internal/src/analyzer/patternMatching.ts b/packages/pyright-internal/src/analyzer/patternMatching.ts index 616fca0985..03587543af 100644 --- a/packages/pyright-internal/src/analyzer/patternMatching.ts +++ b/packages/pyright-internal/src/analyzer/patternMatching.ts @@ -780,7 +780,7 @@ function narrowTypeBasedOnClassPattern( exprType = specializeWithUnknownTypeArgs( exprType, evaluator.getTupleClassType(), - shouldUseVarianceForSpecialization(type, getFileInfo(pattern).diagnosticRuleSet.improvedGenericNarrowing) + shouldUseVarianceForSpecialization(type, getFileInfo(pattern).diagnosticRuleSet.strictGenericNarrowing) ? evaluator.getObjectType() : undefined ); diff --git a/packages/pyright-internal/src/analyzer/typeGuards.ts b/packages/pyright-internal/src/analyzer/typeGuards.ts index 137c9dec27..3ebde159a6 100644 --- a/packages/pyright-internal/src/analyzer/typeGuards.ts +++ b/packages/pyright-internal/src/analyzer/typeGuards.ts @@ -628,7 +628,7 @@ export function getTypeNarrowingCallback( evaluator, arg1Type, evaluator.getTypeOfExpression(arg0Expr).type, - getFileInfo(testExpression).diagnosticRuleSet.improvedGenericNarrowing + getFileInfo(testExpression).diagnosticRuleSet.strictGenericNarrowing ); const isIncomplete = !!callTypeResult.isIncomplete || !!arg1TypeResult.isIncomplete; @@ -1133,7 +1133,7 @@ export function getIsInstanceClassTypes( evaluator: TypeEvaluator, argType: Type, typeToNarrow: Type, - improvedGenericNarrowing: boolean + strictGenericNarrowing: boolean ): (ClassType | TypeVarType | FunctionType)[] | undefined { let foundNonClassType = false; const classTypeList: (ClassType | TypeVarType | FunctionType)[] = []; @@ -1143,7 +1143,7 @@ export function getIsInstanceClassTypes( types.forEach((type) => { const subtypes: Type[] = []; if (isClass(type)) { - const useVariance = shouldUseVarianceForSpecialization(typeToNarrow, improvedGenericNarrowing); + const useVariance = shouldUseVarianceForSpecialization(typeToNarrow, strictGenericNarrowing); if (useVariance) { evaluator.inferVarianceForClass(type); } diff --git a/packages/pyright-internal/src/analyzer/typeUtils.ts b/packages/pyright-internal/src/analyzer/typeUtils.ts index ff43b805d1..c61d434fc6 100644 --- a/packages/pyright-internal/src/analyzer/typeUtils.ts +++ b/packages/pyright-internal/src/analyzer/typeUtils.ts @@ -1083,8 +1083,8 @@ export function getTypeVarScopeIds(type: Type): TypeVarScopeId[] { * If the type we're narrowing already has type parameters, * there's no need to use variance for specialization. */ -export const shouldUseVarianceForSpecialization = (typeToNarrow: Type, improvedGenericNarrowing: boolean) => { - if (!improvedGenericNarrowing) { +export const shouldUseVarianceForSpecialization = (typeToNarrow: Type, strictGenericNarrowing: boolean) => { + if (!strictGenericNarrowing) { return false; } return allSubtypes( @@ -1098,12 +1098,12 @@ export const shouldUseVarianceForSpecialization = (typeToNarrow: Type, improvedG /** * Specializes the class with "Unknown" type args (or the equivalent for ParamSpecs or TypeVarTuples), or its - * widest possible type if its variance is known and {@link objectTypeForImprovedGenericNarrowing} is provided (`object` if + * widest possible type if its variance is known and {@link objectTypeForstrictGenericNarrowing} is provided (`object` if * the bound if covariant, `Never` if contravariant). see docstring on {@link getUnknownForTypeVar} for more info * * @param tupleClassType the builtin `tuple` type for special-casing tuples. needs to be passed so that this * module doesn't depend on `typeEvaluator.ts` - * @param objectTypeForImprovedGenericNarrowing + * @param objectTypeForstrictGenericNarrowing * the builtin `object` type to be returned if the type var is covariant. * needs to be passed so that this module doesn't depend on `typeEvaluator.ts`. note that * `evaluator.inferVarianceForClass` needs to be called on {@link type} first if passing this parameter. @@ -1115,7 +1115,7 @@ export const shouldUseVarianceForSpecialization = (typeToNarrow: Type, improvedG export function specializeWithUnknownTypeArgs( type: ClassType, tupleClassType?: ClassType, - objectTypeForImprovedGenericNarrowing?: Type + objectTypeForstrictGenericNarrowing?: Type ): ClassType | UnionType { if (type.shared.typeParams.length === 0) { return type; @@ -1131,7 +1131,7 @@ export function specializeWithUnknownTypeArgs( !!type.priv.includeSubclasses ); } - if (objectTypeForImprovedGenericNarrowing) { + if (objectTypeForstrictGenericNarrowing) { const result = UnionType.create(); const constraintCombinations = new Array(); @@ -1146,7 +1146,7 @@ export function specializeWithUnknownTypeArgs( } } else { currentConstraints.push( - getUnknownForTypeVar(typeParam, tupleClassType, objectTypeForImprovedGenericNarrowing) + getUnknownForTypeVar(typeParam, tupleClassType, objectTypeForstrictGenericNarrowing) ); } } diff --git a/packages/pyright-internal/src/common/configOptions.ts b/packages/pyright-internal/src/common/configOptions.ts index cdb5a0ecaa..9b755a537e 100644 --- a/packages/pyright-internal/src/common/configOptions.ts +++ b/packages/pyright-internal/src/common/configOptions.ts @@ -411,7 +411,7 @@ export interface DiagnosticRuleSet { * @see https://github.com/DetachHead/basedpyright/issues/603#issuecomment-2303297625 */ failOnWarnings: boolean; - improvedGenericNarrowing: boolean; + strictGenericNarrowing: boolean; reportUnreachable: DiagnosticLevel; reportAny: DiagnosticLevel; reportExplicitAny: DiagnosticLevel; @@ -442,7 +442,7 @@ export function getBooleanDiagnosticRules(includeNonOverridable = false) { DiagnosticRule.enableExperimentalFeatures, DiagnosticRule.deprecateTypingAliases, DiagnosticRule.disableBytesTypePromotions, - DiagnosticRule.improvedGenericNarrowing, + DiagnosticRule.strictGenericNarrowing, ]; if (includeNonOverridable) { @@ -675,7 +675,7 @@ export function getOffDiagnosticRuleSet(): DiagnosticRuleSet { reportShadowedImports: 'none', reportImplicitOverride: 'none', failOnWarnings: false, - improvedGenericNarrowing: false, + strictGenericNarrowing: false, reportUnreachable: 'hint', reportAny: 'none', reportExplicitAny: 'none', @@ -791,7 +791,7 @@ export function getBasicDiagnosticRuleSet(): DiagnosticRuleSet { reportShadowedImports: 'none', reportImplicitOverride: 'none', failOnWarnings: false, - improvedGenericNarrowing: false, + strictGenericNarrowing: false, reportUnreachable: 'hint', reportAny: 'none', reportExplicitAny: 'none', @@ -907,7 +907,7 @@ export function getStandardDiagnosticRuleSet(): DiagnosticRuleSet { reportShadowedImports: 'none', reportImplicitOverride: 'none', failOnWarnings: false, - improvedGenericNarrowing: false, + strictGenericNarrowing: false, reportUnreachable: 'hint', reportAny: 'none', reportExplicitAny: 'none', @@ -1022,7 +1022,7 @@ export const getRecommendedDiagnosticRuleSet = (): DiagnosticRuleSet => ({ reportShadowedImports: 'warning', reportImplicitOverride: 'warning', failOnWarnings: true, - improvedGenericNarrowing: true, + strictGenericNarrowing: true, reportUnreachable: 'warning', reportAny: 'warning', reportExplicitAny: 'warning', @@ -1134,7 +1134,7 @@ export const getAllDiagnosticRuleSet = (): DiagnosticRuleSet => ({ reportShadowedImports: 'error', reportImplicitOverride: 'error', failOnWarnings: true, - improvedGenericNarrowing: true, + strictGenericNarrowing: true, reportUnreachable: 'error', reportAny: 'error', reportExplicitAny: 'error', @@ -1247,7 +1247,7 @@ export function getStrictDiagnosticRuleSet(): DiagnosticRuleSet { reportShadowedImports: 'none', reportImplicitOverride: 'none', failOnWarnings: false, - improvedGenericNarrowing: false, + strictGenericNarrowing: false, reportUnreachable: 'hint', reportAny: 'none', reportExplicitAny: 'none', diff --git a/packages/pyright-internal/src/common/diagnosticRules.ts b/packages/pyright-internal/src/common/diagnosticRules.ts index b681e5c8ef..a6d564d523 100644 --- a/packages/pyright-internal/src/common/diagnosticRules.ts +++ b/packages/pyright-internal/src/common/diagnosticRules.ts @@ -106,7 +106,7 @@ export enum DiagnosticRule { // basedpyright options: failOnWarnings = 'failOnWarnings', - improvedGenericNarrowing = 'improvedGenericNarrowing', + strictGenericNarrowing = 'strictGenericNarrowing', reportUnreachable = 'reportUnreachable', reportAny = 'reportAny', reportExplicitAny = 'reportExplicitAny', diff --git a/packages/pyright-internal/src/tests/typeEvaluator1.test.ts b/packages/pyright-internal/src/tests/typeEvaluator1.test.ts index 0939e41390..935f2a6590 100644 --- a/packages/pyright-internal/src/tests/typeEvaluator1.test.ts +++ b/packages/pyright-internal/src/tests/typeEvaluator1.test.ts @@ -435,7 +435,7 @@ test('TypeNarrowingIsinstance12', () => { test('TypeNarrowingIsinstance13.py', () => { const configOptions = new ConfigOptions(Uri.empty()); - configOptions.diagnosticRuleSet.improvedGenericNarrowing = true; + configOptions.diagnosticRuleSet.strictGenericNarrowing = true; const analysisResults = TestUtils.typeAnalyzeSampleFiles(['typeNarrowingIsinstance13.py'], configOptions); TestUtils.validateResults(analysisResults, 0); diff --git a/packages/pyright-internal/src/tests/typeEvaluatorBased.test.ts b/packages/pyright-internal/src/tests/typeEvaluatorBased.test.ts index 815409418b..c6e9efc1bc 100644 --- a/packages/pyright-internal/src/tests/typeEvaluatorBased.test.ts +++ b/packages/pyright-internal/src/tests/typeEvaluatorBased.test.ts @@ -122,7 +122,7 @@ describe('narrowing type vars using their bounds', () => { test('enabled', () => { const configOptions = new ConfigOptions(Uri.empty()); configOptions.diagnosticRuleSet.reportUnusedParameter = 'none'; - configOptions.diagnosticRuleSet.improvedGenericNarrowing = true; + configOptions.diagnosticRuleSet.strictGenericNarrowing = true; const analysisResults = typeAnalyzeSampleFiles(['typeNarrowingUsingBounds.py'], configOptions); validateResultsButBased(analysisResults, { errors: [], @@ -131,7 +131,7 @@ describe('narrowing type vars using their bounds', () => { test('disabled', () => { const configOptions = new ConfigOptions(Uri.empty()); configOptions.diagnosticRuleSet.reportUnusedParameter = 'none'; - configOptions.diagnosticRuleSet.improvedGenericNarrowing = false; + configOptions.diagnosticRuleSet.strictGenericNarrowing = false; const analysisResults = typeAnalyzeSampleFiles(['typeNarrowingUsingBoundsDisabled.py'], configOptions); validateResultsButBased(analysisResults, { errors: [], diff --git a/packages/vscode-pyright/schemas/pyrightconfig.schema.json b/packages/vscode-pyright/schemas/pyrightconfig.schema.json index 7eba572172..9ef06bff3e 100644 --- a/packages/vscode-pyright/schemas/pyrightconfig.schema.json +++ b/packages/vscode-pyright/schemas/pyrightconfig.schema.json @@ -100,7 +100,7 @@ "title": "Treat typing-specific aliases to standard types as deprecated", "default": false }, - "improvedGenericNarrowing": { + "strictGenericNarrowing": { "type": "boolean", "title": "Use generic bounds instead of `Any` when narrowing", "default": false @@ -681,8 +681,8 @@ "deprecateTypingAliases": { "$ref": "#/definitions/deprecateTypingAliases" }, - "improvedGenericNarrowing": { - "$ref": "#/definitions/improvedGenericNarrowing" + "strictGenericNarrowing": { + "$ref": "#/definitions/strictGenericNarrowing" }, "reportGeneralTypeIssues": { "$ref": "#/definitions/reportGeneralTypeIssues" @@ -1035,8 +1035,8 @@ "deprecateTypingAliases": { "$ref": "#/definitions/deprecateTypingAliases" }, - "improvedGenericNarrowing": { - "$ref": "#/definitions/improvedGenericNarrowing" + "strictGenericNarrowing": { + "$ref": "#/definitions/strictGenericNarrowing" }, "reportGeneralTypeIssues": { "$ref": "#/definitions/reportGeneralTypeIssues"