Skip to content

Commit

Permalink
error on unknown config options
Browse files Browse the repository at this point in the history
  • Loading branch information
DetachHead committed Jan 27, 2024
1 parent 692e066 commit 34f1276
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
19 changes: 19 additions & 0 deletions packages/pyright-internal/src/common/configOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,20 @@ export class ConfigOptions {
const console = serviceProvider.tryGet(ServiceKeys.console) ?? new NullConsole();
const errors = [];

// keep track of which options have been parsed so we can raise an error on unknown options later
// this is pretty cringe and we should just replace all this with some sort of schema validator thingy
// https://github.com/DetachHead/basedpyright/issues/64
const readOptions: (string | symbol)[] = [];
configObj = new Proxy(configObj, {
get: (target, name) => {
const result = target[name];
if (!readOptions.includes(name)) {
readOptions.push(name);
}
return result;
},
});

// Read the entries that should be an array of relative file paths
for (const key of ['include', 'exclude', 'ignore', 'strict'] as const) {
const configValue = configObj[key];
Expand Down Expand Up @@ -1479,6 +1493,11 @@ export class ConfigOptions {
}
}
}
for (const key of Object.keys(configObj)) {
if (!readOptions.includes(key)) {
errors.push(`unknown config option: ${key}`);
}
}
return errors;
}

Expand Down
15 changes: 13 additions & 2 deletions packages/pyright-internal/src/tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { TestFileSystem } from './harness/vfs/filesystem';
import { cloneDeep } from 'lodash';
import { deserialize, serialize } from '../backgroundThreadBase';
import { AnalysisResults } from '../analyzer/analysis';
import { existsSync } from 'fs';

function createAnalyzer(console?: ConsoleInterface) {
const cons = console ?? new NullConsole();
Expand Down Expand Up @@ -305,6 +306,7 @@ const setupPyprojectToml = (
projectPath: string
): { configOptions: ConfigOptions; analysisResult: AnalysisResults | undefined } => {
const cwd = normalizePath(combinePaths(process.cwd(), projectPath));
assert(existsSync(cwd));
const service = createAnalyzer();
let analysisResult = undefined as AnalysisResults | undefined;
service.setCompletionCallback((result) => (analysisResult = result));
Expand Down Expand Up @@ -343,15 +345,24 @@ test('both pyright and basedpyright in pyproject.toml', () => {
assert(!analysisResult.fatalErrorOccurred);
});

test('invalid setting in pyproject.toml', () => {
test('invalid option value in pyproject.toml', () => {
const { configOptions, analysisResult } = setupPyprojectToml(
'src/tests/samples/project_with_invalid_setting_in_pyproject_toml'
'src/tests/samples/project_with_invalid_option_value_in_pyproject_toml'
);
assert.strictEqual(configOptions.typeCheckingMode, undefined);
assert(analysisResult?.configParseErrorOccurred);
assert(!analysisResult.fatalErrorOccurred);
});

test('unknown option name in pyproject.toml', () => {
const { configOptions, analysisResult } = setupPyprojectToml(
'src/tests/samples/project_with_invalid_option_name_in_pyproject_toml'
);
assert(!('asdf' in configOptions));
assert(analysisResult?.configParseErrorOccurred);
assert(!analysisResult.fatalErrorOccurred);
});

test('FindFilesInMemoryOnly', () => {
const cwd = normalizePath(process.cwd());
const service = createAnalyzer();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[tool.basedpyright]
typeCheckingMode = "strict"
asdf = "asdf"

0 comments on commit 34f1276

Please sign in to comment.