Skip to content

Commit

Permalink
Default python version not being picked up from the environment (#9735)
Browse files Browse the repository at this point in the history
* Remove unnecessary if check

* Add tests for default python version
  • Loading branch information
rchiodo authored Jan 22, 2025
1 parent c2ca796 commit ce4e143
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
7 changes: 2 additions & 5 deletions packages/pyright-internal/src/analyzer/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -856,11 +856,8 @@ export class AnalyzerService {
configOptions.verboseOutput = commandLineOptions.configSettings.verboseOutput;
}

// Ensure default python version and platform. A default should only be picked if
// there is a python path however.
if (configOptions.pythonPath) {
configOptions.ensureDefaultPythonVersion(host, this._console);
}
// Ensure default python version and platform.
configOptions.ensureDefaultPythonVersion(host, this._console);
configOptions.ensureDefaultPythonPlatform(host, this._console);
}

Expand Down
30 changes: 28 additions & 2 deletions packages/pyright-internal/src/tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { ConfigOptions, ExecutionEnvironment, getStandardDiagnosticRuleSet } fro
import { ConsoleInterface, NullConsole } from '../common/console';
import { TaskListPriority } from '../common/diagnostic';
import { combinePaths, normalizePath, normalizeSlashes } from '../common/pathUtils';
import { pythonVersion3_9 } from '../common/pythonVersion';
import { pythonVersion3_13, pythonVersion3_9 } from '../common/pythonVersion';
import { RealTempFile, createFromRealFileSystem } from '../common/realFileSystem';
import { createServiceProvider } from '../common/serviceProviderExtensions';
import { Uri } from '../common/uri/uri';
Expand Down Expand Up @@ -577,10 +577,36 @@ describe(`config test'}`, () => {
assert.equal(options.venvPath?.pathIncludes('test_venv_path'), false);
});

test('DefaultPythonVersion no config', () => {
const cwd = normalizePath(process.cwd());
const nullConsole = new NullConsole();
const service = createAnalyzer(nullConsole);
const commandLineOptions = new CommandLineOptions(cwd, /* fromLanguageServer */ false);
commandLineOptions.configFilePath = 'src/tests/samples/package1';
service.setOptions(commandLineOptions);

const config = service.test_getConfigOptions(commandLineOptions);
assert.deepStrictEqual(config.defaultPythonVersion, pythonVersion3_13);
});

test('DefaultPythonVersion with config', () => {
const cwd = normalizePath(process.cwd());
const nullConsole = new NullConsole();
const service = createAnalyzer(nullConsole);
const commandLineOptions = new CommandLineOptions(cwd, /* fromLanguageServer */ false);
commandLineOptions.configFilePath = 'src/tests/samples/project1';
service.setOptions(commandLineOptions);

const config = service.test_getConfigOptions(commandLineOptions);
assert.deepStrictEqual(config.defaultPythonVersion, pythonVersion3_13);
});

function createAnalyzer(console?: ConsoleInterface) {
const cons = console ?? new NullConsole();
const fs = createFromRealFileSystem(tempFile, cons);
const serviceProvider = createServiceProvider(fs, cons, tempFile);
return new AnalyzerService('<default>', serviceProvider, { console: cons });
const host = new TestAccessHost();
host.getPythonVersion = () => pythonVersion3_13;
return new AnalyzerService('<default>', serviceProvider, { console: cons, hostFactory: () => host });
}
});

0 comments on commit ce4e143

Please sign in to comment.