Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PylanceBot] Pull Pylance with Pyright 1.1.296 #4714

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
Expand Down
85 changes: 6 additions & 79 deletions packages/pyright-internal/src/analyzer/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ import {
ParameterDeclaration,
TypeAliasDeclaration,
TypeParameterDeclaration,
UnresolvedModuleMarker,
VariableDeclaration,
} from './declaration';
import { extractParameterDocumentation } from './docStringUtils';
Expand Down Expand Up @@ -2472,7 +2473,7 @@ export class Binder extends ParseTreeWalker {
if (importInfo && importInfo.isImportFound && !importInfo.isNativeLib && importInfo.resolvedPaths.length > 0) {
pathOfLastSubmodule = importInfo.resolvedPaths[importInfo.resolvedPaths.length - 1];
} else {
pathOfLastSubmodule = '*** unresolved ***';
pathOfLastSubmodule = UnresolvedModuleMarker;
}

const isResolved =
Expand Down Expand Up @@ -2535,7 +2536,7 @@ export class Binder extends ParseTreeWalker {
const loaderActionPath =
importInfo && i < importInfo.resolvedPaths.length
? importInfo.resolvedPaths[i]
: '*** unresolved ***';
: UnresolvedModuleMarker;

// Allocate a new loader action.
loaderActions = {
Expand Down Expand Up @@ -3789,87 +3790,13 @@ export class Binder extends ParseTreeWalker {
}

private _getVariableDocString(node: ExpressionNode): string | undefined {
// Walk up the parse tree to find an assignment expression.
let curNode: ParseNode | undefined = node;
let annotationNode: TypeAnnotationNode | undefined;

while (curNode) {
if (curNode.nodeType === ParseNodeType.Assignment) {
break;
}

if (curNode.nodeType === ParseNodeType.TypeAnnotation && !annotationNode) {
annotationNode = curNode;
}

curNode = curNode.parent;
}

if (curNode?.nodeType !== ParseNodeType.Assignment) {
// Allow a simple annotation statement to have a docstring even
// though PEP 258 doesn't mention this case. This PEP pre-dated
// PEP 526, so it didn't contemplate this situation.
if (annotationNode) {
curNode = annotationNode;
} else {
return undefined;
}
}

const parentNode = curNode.parent;
if (parentNode?.nodeType !== ParseNodeType.StatementList) {
return undefined;
}

const suiteOrModule = parentNode.parent;
if (
!suiteOrModule ||
(suiteOrModule.nodeType !== ParseNodeType.Module && suiteOrModule.nodeType !== ParseNodeType.Suite)
) {
return undefined;
}

const assignmentIndex = suiteOrModule.statements.findIndex((node) => node === parentNode);
if (assignmentIndex < 0 || assignmentIndex === suiteOrModule.statements.length - 1) {
return undefined;
}

const nextStatement = suiteOrModule.statements[assignmentIndex + 1];

if (nextStatement.nodeType !== ParseNodeType.StatementList || !ParseTreeUtils.isDocString(nextStatement)) {
return undefined;
}

// See if the assignment is within one of the contexts specified in PEP 258.
let isValidContext = false;
if (parentNode?.parent?.nodeType === ParseNodeType.Module) {
// If we're at the top level of a module, the attribute docstring is valid.
isValidContext = true;
} else if (
parentNode?.parent?.nodeType === ParseNodeType.Suite &&
parentNode?.parent?.parent?.nodeType === ParseNodeType.Class
) {
// If we're at the top level of a class, the attribute docstring is valid.
isValidContext = true;
} else {
const func = ParseTreeUtils.getEnclosingFunction(parentNode);

// If we're within an __init__ method, the attribute docstring is valid.
if (
func &&
func.name.value === '__init__' &&
ParseTreeUtils.getEnclosingClass(func, /* stopAtFunction */ true)
) {
isValidContext = true;
}
}

if (!isValidContext) {
const docNode = ParseTreeUtils.getVariableDocStringNode(node);
if (!docNode) {
return undefined;
}

// A docstring can consist of multiple joined strings in a single expression.
const strings = (nextStatement.statements[0] as StringListNode).strings;
const strings = docNode.strings;
if (strings.length === 1) {
// Common case.
return strings[0].value;
Expand Down
6 changes: 6 additions & 0 deletions packages/pyright-internal/src/analyzer/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import {
YieldNode,
} from '../parser/parseNodes';

export const UnresolvedModuleMarker = '*** unresolved ***';

export const enum DeclarationType {
Intrinsic,
Variable,
Expand Down Expand Up @@ -290,3 +292,7 @@ export function isSpecialBuiltInClassDeclaration(decl: Declaration): decl is Spe
export function isIntrinsicDeclaration(decl: Declaration): decl is IntrinsicDeclaration {
return decl.type === DeclarationType.Intrinsic;
}

export function isUnresolvedAliasDeclaration(decl: Declaration): boolean {
return isAliasDeclaration(decl) && decl.path === UnresolvedModuleMarker;
}
80 changes: 80 additions & 0 deletions packages/pyright-internal/src/analyzer/parseTreeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2554,3 +2554,83 @@ function _getEndPositionIfMultipleStatementsAreOnSameLine(

return undefined;
}

export function getVariableDocStringNode(node: ExpressionNode): StringListNode | undefined {
// Walk up the parse tree to find an assignment expression.
let curNode: ParseNode | undefined = node;
let annotationNode: TypeAnnotationNode | undefined;

while (curNode) {
if (curNode.nodeType === ParseNodeType.Assignment) {
break;
}

if (curNode.nodeType === ParseNodeType.TypeAnnotation && !annotationNode) {
annotationNode = curNode;
}

curNode = curNode.parent;
}

if (curNode?.nodeType !== ParseNodeType.Assignment) {
// Allow a simple annotation statement to have a docstring even
// though PEP 258 doesn't mention this case. This PEP pre-dated
// PEP 526, so it didn't contemplate this situation.
if (annotationNode) {
curNode = annotationNode;
} else {
return undefined;
}
}

const parentNode = curNode.parent;
if (parentNode?.nodeType !== ParseNodeType.StatementList) {
return undefined;
}

const suiteOrModule = parentNode.parent;
if (
!suiteOrModule ||
(suiteOrModule.nodeType !== ParseNodeType.Module && suiteOrModule.nodeType !== ParseNodeType.Suite)
) {
return undefined;
}

const assignmentIndex = suiteOrModule.statements.findIndex((node) => node === parentNode);
if (assignmentIndex < 0 || assignmentIndex === suiteOrModule.statements.length - 1) {
return undefined;
}

const nextStatement = suiteOrModule.statements[assignmentIndex + 1];

if (nextStatement.nodeType !== ParseNodeType.StatementList || !isDocString(nextStatement)) {
return undefined;
}

// See if the assignment is within one of the contexts specified in PEP 258.
let isValidContext = false;
if (parentNode?.parent?.nodeType === ParseNodeType.Module) {
// If we're at the top level of a module, the attribute docstring is valid.
isValidContext = true;
} else if (
parentNode?.parent?.nodeType === ParseNodeType.Suite &&
parentNode?.parent?.parent?.nodeType === ParseNodeType.Class
) {
// If we're at the top level of a class, the attribute docstring is valid.
isValidContext = true;
} else {
const func = getEnclosingFunction(parentNode);

// If we're within an __init__ method, the attribute docstring is valid.
if (func && func.name.value === '__init__' && getEnclosingClass(func, /* stopAtFunction */ true)) {
isValidContext = true;
}
}

if (!isValidContext) {
return undefined;
}

// A docstring can consist of multiple joined strings in a single expression.
return nextStatement.statements[0] as StringListNode;
}
Loading