Skip to content

Commit

Permalink
report explicit usages of the Any type
Browse files Browse the repository at this point in the history
  • Loading branch information
DetachHead committed Nov 14, 2024
1 parent cb30c33 commit bc635fd
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/pyright-internal/src/analyzer/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,10 @@ export class Checker extends ParseTreeWalker {
const type = this._evaluator.getType(node);
this._reportDeprecatedUseForType(node, type);

if (type && isAny(type) && type.props?.specialForm) {
this._evaluator.addDiagnostic(DiagnosticRule.reportAny, LocMessage.explicitAny(), node);
}

return true;
}

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 @@ -1241,6 +1241,7 @@ export namespace Localizer {
new ParameterizedString<{ classes: string }>(getRawString('Diagnostic.multipleInheritance'));
export const unannotatedClassAttribute = () =>
new ParameterizedString<{ name: string }>(getRawString('Diagnostic.unannotatedClassAttribute'));
export const explicitAny = () => getRawString('Diagnostic.explicitAny');
}

export namespace DiagnosticAddendum {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1730,7 +1730,8 @@
"implicitRelativeImport": "Import from `{importName}` is implicitly relative and will not work if this file is imported as a module",
"invalidCast": "Conversion of type `{fromType}` to type `{toType}` may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to `object` first.",
"multipleInheritance": "Multiple inheritance is not allowed because the following base classes contain `__init__` or `__new__` methods that may not get called: {classes}",
"unannotatedClassAttribute": "Type annotation for attribute `{name}` is required because this class is not decorated with `@final`"
"unannotatedClassAttribute": "Type annotation for attribute `{name}` is required because this class is not decorated with `@final`",
"explicitAny": "The `Any` type is not allowed"
},
"DiagnosticAddendum": {
"annotatedNotAllowed": {
Expand Down
4 changes: 4 additions & 0 deletions packages/pyright-internal/src/tests/reportAny.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ test('reportAny', () => {
validateResultsButBased(analysisResults, {
errors: [
{ line: 3, code: DiagnosticRule.reportAny, message: LocMessage.returnTypeAny() },
{ line: 3, code: DiagnosticRule.reportAny, message: LocMessage.explicitAny() },
{ line: 3, code: DiagnosticRule.reportAny, message: LocMessage.explicitAny() },
{
line: 3,
code: DiagnosticRule.reportAny,
Expand All @@ -20,6 +22,7 @@ test('reportAny', () => {
{ line: 4, code: DiagnosticRule.reportAny },
{ line: 5, code: DiagnosticRule.reportAny, message: LocMessage.returnTypeAny() },
{ line: 7, code: DiagnosticRule.reportAny, message: LocMessage.typeAny().format({ name: 'bar' }) },
{ line: 7, code: DiagnosticRule.reportAny, message: LocMessage.explicitAny() },
{
line: 9,
code: DiagnosticRule.reportAny,
Expand All @@ -40,6 +43,7 @@ test('reportAny', () => {
code: DiagnosticRule.reportAny,
message: LocMessage.lambdaReturnTypeAny(),
},
{ line: 15, code: DiagnosticRule.reportAny, message: LocMessage.explicitAny() },
{
line: 18,
code: DiagnosticRule.reportAny,
Expand Down

0 comments on commit bc635fd

Please sign in to comment.