Skip to content

Commit

Permalink
rename improvedGenericNarrowing to be consistent with other similar…
Browse files Browse the repository at this point in the history
… options
  • Loading branch information
DetachHead committed Nov 23, 2024
1 parent c0caebc commit 886cf5a
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ The following settings determine how different types should be evaluated.

### basedpyright exclusive settings

- <a name="improvedGenericNarrowing"></a> **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`.
- <a name="strictGenericNarrowing"></a> **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

Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/analyzer/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3797,7 +3797,7 @@ export class Checker extends ParseTreeWalker {
this._evaluator,
arg1Type,
arg0Type,
this._fileInfo.diagnosticRuleSet.improvedGenericNarrowing
this._fileInfo.diagnosticRuleSet.strictGenericNarrowing
);
if (!classTypeList) {
return;
Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/analyzer/patternMatching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
6 changes: 3 additions & 3 deletions packages/pyright-internal/src/analyzer/typeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)[] = [];
Expand All @@ -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);
}
Expand Down
14 changes: 7 additions & 7 deletions packages/pyright-internal/src/analyzer/typeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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.
Expand All @@ -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;
Expand All @@ -1131,7 +1131,7 @@ export function specializeWithUnknownTypeArgs(
!!type.priv.includeSubclasses
);
}
if (objectTypeForImprovedGenericNarrowing) {
if (objectTypeForstrictGenericNarrowing) {
const result = UnionType.create();
const constraintCombinations = new Array<Type[]>();

Expand All @@ -1146,7 +1146,7 @@ export function specializeWithUnknownTypeArgs(
}
} else {
currentConstraints.push(
getUnknownForTypeVar(typeParam, tupleClassType, objectTypeForImprovedGenericNarrowing)
getUnknownForTypeVar(typeParam, tupleClassType, objectTypeForstrictGenericNarrowing)
);
}
}
Expand Down
16 changes: 8 additions & 8 deletions packages/pyright-internal/src/common/configOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -442,7 +442,7 @@ export function getBooleanDiagnosticRules(includeNonOverridable = false) {
DiagnosticRule.enableExperimentalFeatures,
DiagnosticRule.deprecateTypingAliases,
DiagnosticRule.disableBytesTypePromotions,
DiagnosticRule.improvedGenericNarrowing,
DiagnosticRule.strictGenericNarrowing,
];

if (includeNonOverridable) {
Expand Down Expand Up @@ -675,7 +675,7 @@ export function getOffDiagnosticRuleSet(): DiagnosticRuleSet {
reportShadowedImports: 'none',
reportImplicitOverride: 'none',
failOnWarnings: false,
improvedGenericNarrowing: false,
strictGenericNarrowing: false,
reportUnreachable: 'hint',
reportAny: 'none',
reportExplicitAny: 'none',
Expand Down Expand Up @@ -791,7 +791,7 @@ export function getBasicDiagnosticRuleSet(): DiagnosticRuleSet {
reportShadowedImports: 'none',
reportImplicitOverride: 'none',
failOnWarnings: false,
improvedGenericNarrowing: false,
strictGenericNarrowing: false,
reportUnreachable: 'hint',
reportAny: 'none',
reportExplicitAny: 'none',
Expand Down Expand Up @@ -907,7 +907,7 @@ export function getStandardDiagnosticRuleSet(): DiagnosticRuleSet {
reportShadowedImports: 'none',
reportImplicitOverride: 'none',
failOnWarnings: false,
improvedGenericNarrowing: false,
strictGenericNarrowing: false,
reportUnreachable: 'hint',
reportAny: 'none',
reportExplicitAny: 'none',
Expand Down Expand Up @@ -1022,7 +1022,7 @@ export const getRecommendedDiagnosticRuleSet = (): DiagnosticRuleSet => ({
reportShadowedImports: 'warning',
reportImplicitOverride: 'warning',
failOnWarnings: true,
improvedGenericNarrowing: true,
strictGenericNarrowing: true,
reportUnreachable: 'warning',
reportAny: 'warning',
reportExplicitAny: 'warning',
Expand Down Expand Up @@ -1134,7 +1134,7 @@ export const getAllDiagnosticRuleSet = (): DiagnosticRuleSet => ({
reportShadowedImports: 'error',
reportImplicitOverride: 'error',
failOnWarnings: true,
improvedGenericNarrowing: true,
strictGenericNarrowing: true,
reportUnreachable: 'error',
reportAny: 'error',
reportExplicitAny: 'error',
Expand Down Expand Up @@ -1247,7 +1247,7 @@ export function getStrictDiagnosticRuleSet(): DiagnosticRuleSet {
reportShadowedImports: 'none',
reportImplicitOverride: 'none',
failOnWarnings: false,
improvedGenericNarrowing: false,
strictGenericNarrowing: false,
reportUnreachable: 'hint',
reportAny: 'none',
reportExplicitAny: 'none',
Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/common/diagnosticRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export enum DiagnosticRule {

// basedpyright options:
failOnWarnings = 'failOnWarnings',
improvedGenericNarrowing = 'improvedGenericNarrowing',
strictGenericNarrowing = 'strictGenericNarrowing',
reportUnreachable = 'reportUnreachable',
reportAny = 'reportAny',
reportExplicitAny = 'reportExplicitAny',
Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/tests/typeEvaluator1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand All @@ -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: [],
Expand Down
10 changes: 5 additions & 5 deletions packages/vscode-pyright/schemas/pyrightconfig.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -681,8 +681,8 @@
"deprecateTypingAliases": {
"$ref": "#/definitions/deprecateTypingAliases"
},
"improvedGenericNarrowing": {
"$ref": "#/definitions/improvedGenericNarrowing"
"strictGenericNarrowing": {
"$ref": "#/definitions/strictGenericNarrowing"
},
"reportGeneralTypeIssues": {
"$ref": "#/definitions/reportGeneralTypeIssues"
Expand Down Expand Up @@ -1035,8 +1035,8 @@
"deprecateTypingAliases": {
"$ref": "#/definitions/deprecateTypingAliases"
},
"improvedGenericNarrowing": {
"$ref": "#/definitions/improvedGenericNarrowing"
"strictGenericNarrowing": {
"$ref": "#/definitions/strictGenericNarrowing"
},
"reportGeneralTypeIssues": {
"$ref": "#/definitions/reportGeneralTypeIssues"
Expand Down

0 comments on commit 886cf5a

Please sign in to comment.