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

Added check for overwriting a method within a class. This addresses #… #9443

Closed
wants to merge 1 commit 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
5 changes: 5 additions & 0 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6225,6 +6225,11 @@ export function createTypeEvaluator(
isDescriptorError = true;
}

if (memberInfo?.isMethod && usage.method === 'set') {
diag?.addMessage(LocAddendum.assignMethod());
isDescriptorError = true;
}

return resultType;
});

Expand Down
14 changes: 14 additions & 0 deletions packages/pyright-internal/src/analyzer/typeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ export interface ClassMember {
// or frozen dataclasses.
isReadOnly: boolean;

// True if the member is a method -- a class variable declared
// with a "def" statement.
isMethod: boolean;

// True if member has declared type, false if inferred
isTypeDeclared: boolean;

Expand Down Expand Up @@ -1631,6 +1635,7 @@ export function getProtocolSymbolsRecursive(
isClassMember: symbol.isClassMember(),
isClassVar: isEffectivelyClassVar(symbol, /* isDataclass */ false),
isReadOnly: false,
isMethod: false,
isTypeDeclared: symbol.hasTypedDeclarations(),
skippedUndeclaredType: false,
});
Expand Down Expand Up @@ -1772,6 +1777,7 @@ export function* getClassMemberIterator(
classType,
unspecializedClassType: classType,
isReadOnly: false,
isMethod: false,
isTypeDeclared: false,
skippedUndeclaredType: false,
};
Expand Down Expand Up @@ -1800,6 +1806,7 @@ export function* getClassMemberIterator(
classType: specializedMroClass,
unspecializedClassType: mroClass,
isReadOnly: isMemberReadOnly(specializedMroClass, memberName),
isMethod: false,
isTypeDeclared: hasDeclaredType,
skippedUndeclaredType,
};
Expand Down Expand Up @@ -1847,6 +1854,10 @@ export function* getClassMemberIterator(
symbol = Symbol.createWithType(SymbolFlags.ClassMember, classType.priv.partialCallType);
}

const isMethod = symbol
.getTypedDeclarations()
.some((decl) => decl.type === DeclarationType.Function);

const cm: ClassMember = {
symbol,
isInstanceMember,
Expand All @@ -1855,6 +1866,7 @@ export function* getClassMemberIterator(
classType: specializedMroClass,
unspecializedClassType: mroClass,
isReadOnly: false,
isMethod,
isTypeDeclared: hasDeclaredType,
skippedUndeclaredType,
};
Expand All @@ -1876,6 +1888,7 @@ export function* getClassMemberIterator(
classType,
unspecializedClassType: classType,
isReadOnly: false,
isMethod: false,
isTypeDeclared: false,
skippedUndeclaredType: false,
};
Expand Down Expand Up @@ -1970,6 +1983,7 @@ export function getClassFieldsRecursive(classType: ClassType): Map<string, Class
isClassMember: symbol.isClassMember(),
isClassVar: isEffectivelyClassVar(symbol, ClassType.isDataClass(specializedMroClass)),
isReadOnly: isMemberReadOnly(specializedMroClass, name),
isMethod: false,
isTypeDeclared: true,
skippedUndeclaredType: false,
});
Expand Down
1 change: 1 addition & 0 deletions packages/pyright-internal/src/localization/localize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,7 @@ export namespace Localizer {
new ParameterizedString<{ type: string }>(getRawString('DiagnosticAddendum.argumentType'));
export const argumentTypes = () =>
new ParameterizedString<{ types: string }>(getRawString('DiagnosticAddendum.argumentTypes'));
export const assignMethod = () => getRawString('DiagnosticAddendum.assignMethod');
export const assignToNone = () => getRawString('DiagnosticAddendum.assignToNone');
export const asyncHelp = () => getRawString('DiagnosticAddendum.asyncHelp');
export const baseClassIncompatible = () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,7 @@
"argsPositionOnly": "Position-only parameter mismatch; expected {expected} but received {received}",
"argumentType": "Argument type is \"{type}\"",
"argumentTypes": "Argument types: ({types})",
"assignMethod": "Method cannot be reassigned",
"assignToNone": {
"message": "Type is not assignable to \"None\"",
"comment": "{Locked='None'}"
Expand Down
Loading