Skip to content

Commit

Permalink
Merge branch 'main' into pull-pylance-with-pyright-1.1.383
Browse files Browse the repository at this point in the history
  • Loading branch information
debonte authored Oct 2, 2024
2 parents 8c7f9ba + 5e57e0a commit eb7f331
Show file tree
Hide file tree
Showing 48 changed files with 1,026 additions and 678 deletions.
9 changes: 6 additions & 3 deletions build/azuredevops/azure-pipelines-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ extends:
template: azure-pipelines/MicroBuild.1ES.Official.yml@MicroBuildTemplate
parameters:
sdl:
codeSignValidation:
enabled: true
additionalTargetsGlobPattern: -|**/gdn-vscode-pyright.vsix/**
codeql:
compiled:
enabled: false
Expand All @@ -45,7 +48,7 @@ extends:
outputs:
- output: pipelineArtifact
displayName: 'publish vsix artifact'
targetPath: build_output
targetPath: 'build_output'
artifactName: $(ARTIFACT_NAME_VSIX)
steps:
- checkout: self
Expand Down Expand Up @@ -80,7 +83,7 @@ extends:
displayName: 'Copy vsix to: build_output'
inputs:
SourceFolder: packages/vscode-pyright
Contents: '*.vsix'
Contents: '$(VSIX_NAME)'
TargetFolder: build_output

- script: |
Expand Down Expand Up @@ -169,7 +172,7 @@ extends:
inputs:
notifyUsers: 'plseng@microsoft.com,eric@traut.com'
instructions: 'In the next 2 hours please test the latest draft release of Pyright, then Publish the release in GitHub.'
onTimeout: 'resume'
onTimeout: 'reject' # require sign-off

- stage: PublishExtension
dependsOn:
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"packages": [
"packages/*"
],
"version": "1.1.382",
"version": "1.1.383",
"command": {
"version": {
"push": false,
Expand Down
4 changes: 2 additions & 2 deletions packages/pyright-internal/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/pyright-internal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "pyright-internal",
"displayName": "pyright",
"description": "Type checker for the Python language",
"version": "1.1.382",
"version": "1.1.383",
"license": "MIT",
"private": true,
"files": [
Expand Down
24 changes: 24 additions & 0 deletions packages/pyright-internal/src/analyzer/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ export class Binder extends ParseTreeWalker {
// binding the builtins module itself.
const isBuiltInModule = this._fileInfo.builtinsScope === undefined;

this._addTypingImportAliasesFromBuiltinsScope();

this._createNewScope(
isBuiltInModule ? ScopeType.Builtin : ScopeType.Module,
this._fileInfo.builtinsScope,
Expand Down Expand Up @@ -1863,6 +1865,10 @@ export class Binder extends ParseTreeWalker {
}
}
}

if (isTypingImport) {
localSymbol.setTypingSymbolAlias(name);
}
}
});
}
Expand Down Expand Up @@ -1971,6 +1977,10 @@ export class Binder extends ParseTreeWalker {
if (isTypingImport) {
if (typingSymbolsOfInterest.some((s) => s === importSymbolNode.d.name.d.value)) {
this._typingSymbolAliases.set(nameNode.d.value, importSymbolNode.d.name.d.value);

if (isTypingImport) {
symbol.setTypingSymbolAlias(nameNode.d.value);
}
}
}

Expand Down Expand Up @@ -2352,6 +2362,20 @@ export class Binder extends ParseTreeWalker {
return true;
}

private _addTypingImportAliasesFromBuiltinsScope() {
if (!this._fileInfo.builtinsScope) {
return;
}

const symbolTable = this._fileInfo.builtinsScope.symbolTable;
symbolTable.forEach((symbol, name) => {
const typingImportAlias = symbol.getTypingSymbolAlias();
if (typingImportAlias && !symbol.isExternallyHidden()) {
this._typingSymbolAliases.set(name, typingImportAlias);
}
});
}

private _formatModuleName(node: ModuleNameNode): string {
return '.'.repeat(node.d.leadingDots) + node.d.nameParts.map((part) => part.d.value).join('.');
}
Expand Down
19 changes: 9 additions & 10 deletions packages/pyright-internal/src/analyzer/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ export class Checker extends ParseTreeWalker {

const enclosingFunctionNode = ParseTreeUtils.getEnclosingFunction(node);
let declaredReturnType = enclosingFunctionNode
? this._evaluator.getFunctionDeclaredReturnType(enclosingFunctionNode)
? this._evaluator.getDeclaredReturnType(enclosingFunctionNode)
: undefined;

if (node.d.expr) {
Expand Down Expand Up @@ -2333,7 +2333,7 @@ export class Checker extends ParseTreeWalker {
// the TypeVar multiple times.
const baseType = this._evaluator.getType(baseExpression);
const aliasInfo = baseType?.props?.typeAliasInfo;
if (aliasInfo?.typeParams && subscriptIndex < aliasInfo.typeParams.length) {
if (aliasInfo?.shared.typeParams && subscriptIndex < aliasInfo.shared.typeParams.length) {
isExempt = true;
}
}
Expand Down Expand Up @@ -2744,12 +2744,11 @@ export class Checker extends ParseTreeWalker {

// Now check the return types.
const overloadReturnType = this._evaluator.solveAndApplyConstraints(
FunctionType.getEffectiveReturnType(overloadBound) ??
this._evaluator.getFunctionInferredReturnType(overloadBound),
FunctionType.getEffectiveReturnType(overloadBound) ?? this._evaluator.getInferredReturnType(overloadBound),
constraints
);
const implReturnType = this._evaluator.solveAndApplyConstraints(
FunctionType.getEffectiveReturnType(implBound) ?? this._evaluator.getFunctionInferredReturnType(implBound),
FunctionType.getEffectiveReturnType(implBound) ?? this._evaluator.getInferredReturnType(implBound),
constraints
);

Expand Down Expand Up @@ -3453,7 +3452,7 @@ export class Checker extends ParseTreeWalker {

// If the return type has not yet been inferred, do so now.
if (primaryType && isFunction(primaryType)) {
this._evaluator.getFunctionInferredReturnType(primaryType);
this._evaluator.getInferredReturnType(primaryType);
}

const otherType = this._evaluator.getTypeForDeclaration(otherDecl)?.type;
Expand All @@ -3468,7 +3467,7 @@ export class Checker extends ParseTreeWalker {

// If the return type has not yet been inferred, do so now.
if (otherType && isFunction(otherType)) {
this._evaluator.getFunctionInferredReturnType(otherType);
this._evaluator.getInferredReturnType(otherType);
}

// If both declarations are functions, it's OK if they
Expand Down Expand Up @@ -4238,7 +4237,7 @@ export class Checker extends ParseTreeWalker {
if (deprecatedForm) {
if (
(isInstantiableClass(type) && type.shared.fullName === deprecatedForm.fullName) ||
type.props?.typeAliasInfo?.fullName === deprecatedForm.fullName
type.props?.typeAliasInfo?.shared.fullName === deprecatedForm.fullName
) {
if (
PythonVersion.isGreaterOrEqualTo(
Expand Down Expand Up @@ -4670,7 +4669,7 @@ export class Checker extends ParseTreeWalker {
);
}
} else {
const inferredReturnType = this._evaluator.getFunctionInferredReturnType(functionType);
const inferredReturnType = this._evaluator.getInferredReturnType(functionType);
if (
!isNever(inferredReturnType) &&
!isNoneInstance(inferredReturnType) &&
Expand Down Expand Up @@ -4758,7 +4757,7 @@ export class Checker extends ParseTreeWalker {
}
}
} else {
const inferredReturnType = this._evaluator.getFunctionInferredReturnType(functionType);
const inferredReturnType = this._evaluator.getInferredReturnType(functionType);
this._reportUnknownReturnResult(node, inferredReturnType);
this._validateReturnTypeIsNotContravariant(inferredReturnType, node.d.name);
}
Expand Down
3 changes: 1 addition & 2 deletions packages/pyright-internal/src/analyzer/codeFlowEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ import {
} from './types';
import {
cleanIncompleteUnknown,
convertNodeToArg,
derivesFromStdlibClass,
doForEachSubtype,
isIncompleteUnknown,
Expand Down Expand Up @@ -1811,7 +1810,7 @@ export function getCodeFlowEngine(
// the applicable overload returns a NoReturn.
const callResult = evaluator.validateOverloadedArgTypes(
node,
node.d.args.map((arg) => convertNodeToArg(arg)),
node.d.args.map((arg) => evaluator.convertNodeToArg(arg)),
{ type: callSubtype, isIncomplete: callTypeResult.isIncomplete },
/* constraints */ undefined,
/* skipUnknownArgCheck */ false,
Expand Down
8 changes: 0 additions & 8 deletions packages/pyright-internal/src/analyzer/constraintTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,6 @@ export class ConstraintTracker {
}
}

isSame(other: ConstraintTracker) {
if (other._constraintSets.length !== this._constraintSets.length) {
return false;
}

return this._constraintSets.every((set, index) => set.isSame(other._constraintSets[index]));
}

isEmpty() {
return this._constraintSets.every((set) => set.isEmpty());
}
Expand Down
7 changes: 5 additions & 2 deletions packages/pyright-internal/src/analyzer/constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,12 @@ export function validateConstructorArgs(
// If this is an unspecialized generic type alias, specialize it now
// using default type argument values.
const aliasInfo = type.props?.typeAliasInfo;
if (aliasInfo?.typeParams && !aliasInfo.typeArgs) {
if (aliasInfo?.shared.typeParams && !aliasInfo.typeArgs) {
type = applySolvedTypeVars(type, new ConstraintSolution(), {
replaceUnsolved: { scopeIds: [aliasInfo.typeVarScopeId], tupleClassType: evaluator.getTupleClassType() },
replaceUnsolved: {
scopeIds: [aliasInfo.shared.typeVarScopeId],
tupleClassType: evaluator.getTupleClassType(),
},
}) as ClassType;
}

Expand Down
7 changes: 3 additions & 4 deletions packages/pyright-internal/src/analyzer/dataClasses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ import {
applySolvedTypeVars,
buildSolutionFromSpecializedClass,
computeMroLinearization,
convertNodeToArg,
convertToInstance,
doForEachSignature,
getTypeVarScopeId,
Expand Down Expand Up @@ -746,7 +745,7 @@ function getDefaultArgValueForFieldSpecifier(
callTarget = evaluator.getBestOverloadForArgs(
callNode,
{ type: callType, isIncomplete: callTypeResult.isIncomplete },
callNode.d.args.map((arg) => convertNodeToArg(arg))
callNode.d.args.map((arg) => evaluator.convertNodeToArg(arg))
);
} else if (isInstantiableClass(callType)) {
const initMethodResult = getBoundInitMethod(evaluator, callNode, callType);
Expand All @@ -757,7 +756,7 @@ function getDefaultArgValueForFieldSpecifier(
callTarget = evaluator.getBestOverloadForArgs(
callNode,
{ type: initMethodResult.type },
callNode.d.args.map((arg) => convertNodeToArg(arg))
callNode.d.args.map((arg) => evaluator.convertNodeToArg(arg))
);
}
}
Expand Down Expand Up @@ -1464,7 +1463,7 @@ export function applyDataClassDecorator(
evaluator,
errorNode,
classType,
(callNode?.d.args ?? []).map((arg) => convertNodeToArg(arg)),
(callNode?.d.args ?? []).map((arg) => evaluator.convertNodeToArg(arg)),
defaultBehaviors
);
}
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/analyzer/importResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2660,7 +2660,7 @@ export class ImportResolver {
): boolean {
let isNativeLib = false;

if (this.dirExistsCached(fileDirectory)) {
if (!execEnv.skipNativeLibraries && this.dirExistsCached(fileDirectory)) {
const filesInDir = this._getFilesInDirectory(fileDirectory);
const dirName = dirPath.fileName;
const nativeLibPath = filesInDir.find((f) => this._isNativeModuleFileName(dirName, f));
Expand Down
79 changes: 47 additions & 32 deletions packages/pyright-internal/src/analyzer/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,37 +683,7 @@ export function getTypeOfUnaryOperation(
// is incomplete because we may be evaluating an expression within
// a loop, so the literal value may change each time.
if (!exprTypeResult.isIncomplete) {
const operator = node.d.operator;
const literalClassName = getLiteralTypeClassName(exprType);

if (literalClassName === 'int') {
if (operator === OperatorType.Add) {
type = exprType;
} else if (operator === OperatorType.Subtract) {
type = mapSubtypes(exprType, (subtype) => {
const classSubtype = subtype as ClassType;
return ClassType.cloneWithLiteral(
classSubtype,
-(classSubtype.priv.literalValue as number | bigint)
);
});
} else if (operator === OperatorType.BitwiseInvert) {
type = mapSubtypes(exprType, (subtype) => {
const classSubtype = subtype as ClassType;
return ClassType.cloneWithLiteral(
classSubtype,
~(classSubtype.priv.literalValue as number | bigint)
);
});
}
} else if (literalClassName === 'bool') {
if (operator === OperatorType.Not) {
type = mapSubtypes(exprType, (subtype) => {
const classSubtype = subtype as ClassType;
return ClassType.cloneWithLiteral(classSubtype, !(classSubtype.priv.literalValue as boolean));
});
}
}
type = calcLiteralForUnaryOp(node.d.operator, exprType);
}

if (!type) {
Expand Down Expand Up @@ -936,17 +906,62 @@ function createUnionType(
return { type: newUnion };
}

// Attempts to apply "literal math" for a literal operands.
function calcLiteralForUnaryOp(operator: OperatorType, operandType: Type): Type | undefined {
let type: Type | undefined;

if (getUnionSubtypeCount(operandType) >= maxLiteralMathSubtypeCount) {
return undefined;
}

if (!!getTypeCondition(operandType) || someSubtypes(operandType, (subtype) => !!getTypeCondition(subtype))) {
return undefined;
}

const literalClassName = getLiteralTypeClassName(operandType);

if (literalClassName === 'int') {
if (operator === OperatorType.Add) {
type = operandType;
} else if (operator === OperatorType.Subtract) {
type = mapSubtypes(operandType, (subtype) => {
const classSubtype = subtype as ClassType;
return ClassType.cloneWithLiteral(classSubtype, -(classSubtype.priv.literalValue as number | bigint));
});
} else if (operator === OperatorType.BitwiseInvert) {
type = mapSubtypes(operandType, (subtype) => {
const classSubtype = subtype as ClassType;
return ClassType.cloneWithLiteral(classSubtype, ~(classSubtype.priv.literalValue as number | bigint));
});
}
} else if (literalClassName === 'bool') {
if (operator === OperatorType.Not) {
type = mapSubtypes(operandType, (subtype) => {
const classSubtype = subtype as ClassType;
return ClassType.cloneWithLiteral(classSubtype, !(classSubtype.priv.literalValue as boolean));
});
}
}

return type;
}

// Attempts to apply "literal math" for two literal operands.
function calcLiteralForBinaryOp(operator: OperatorType, leftType: Type, rightType: Type): Type | undefined {
const leftLiteralClassName = getLiteralTypeClassName(leftType);
if (!leftLiteralClassName || getTypeCondition(leftType)) {
if (
!leftLiteralClassName ||
getTypeCondition(leftType) ||
someSubtypes(leftType, (subtype) => !!getTypeCondition(subtype))
) {
return undefined;
}

const rightLiteralClassName = getLiteralTypeClassName(rightType);
if (
leftLiteralClassName !== rightLiteralClassName ||
getTypeCondition(rightType) ||
someSubtypes(rightType, (subtype) => !!getTypeCondition(subtype)) ||
getUnionSubtypeCount(leftType) * getUnionSubtypeCount(rightType) >= maxLiteralMathSubtypeCount
) {
return undefined;
Expand Down
Loading

0 comments on commit eb7f331

Please sign in to comment.