Skip to content

Commit

Permalink
Removed provisional support for [PEP 746](https://discuss.python.org/…
Browse files Browse the repository at this point in the history
…t/pep-746-typedmetadata-for-type-checking-of-pep-593-annotated/53834) because the PEP has been modified, and there is disagreement about the best approach.
  • Loading branch information
erictraut committed Oct 5, 2024
1 parent bbab701 commit 8ed69a9
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 160 deletions.
61 changes: 3 additions & 58 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15775,64 +15775,9 @@ export function createTypeEvaluator(

// Determines whether the metadata object is compatible with the base type.
function validateTypeMetadata(errorNode: ExpressionNode, baseType: Type, metaArg: TypeResultWithNode): boolean {
// This is an experimental feature because PEP 746 hasn't been accepted.
if (!AnalyzerNodeInfo.getFileInfo(errorNode).diagnosticRuleSet.enableExperimentalFeatures) {
return true;
}

if (!isClass(metaArg.type)) {
return true;
}

const supportsTypeMethod = getTypeOfBoundMember(
/* errorNode */ undefined,
metaArg.type,
'__supports_type__'
)?.type;

if (!supportsTypeMethod) {
return true;
}

// "Call" the __supports_type__ method to determine if the type is supported.
const callResult = useSpeculativeMode(errorNode, () =>
validateCallArgs(
errorNode,
[
{
argCategory: ArgCategory.Simple,
typeResult: { type: convertToInstance(baseType) },
},
],
{ type: supportsTypeMethod },
/* constraints */ undefined,
/* skipUnknownArgCheck */ true,
/* inferenceContext */ undefined
)
);

if (!callResult.returnType) {
return true;
}

// If there are no errors and the return type is potentially truthy,
// we know that the type is supported by this metadata object.
if (!callResult.argumentErrors && canBeTruthy(callResult.returnType)) {
return true;
}

if (!callResult.isTypeIncomplete) {
addDiagnostic(
DiagnosticRule.reportInvalidTypeArguments,
LocMessage.annotatedMetadataInconsistent().format({
metadataType: printType(metaArg.type),
type: printType(convertToInstance(baseType)),
}),
metaArg.node
);
}

return false;
// This function was added for draft PEP 746, but the functionality
// has been removed for now while the PEP is being revised.
return true;
}

// Creates one of several "special" types that are defined in typing.pyi
Expand Down
94 changes: 0 additions & 94 deletions packages/pyright-internal/src/tests/samples/annotatedMetadata1.py

This file was deleted.

8 changes: 0 additions & 8 deletions packages/pyright-internal/src/tests/typeEvaluator7.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -863,14 +863,6 @@ test('Annotated2', () => {
TestUtils.validateResults(analysisResults, 0);
});

test('AnnotatedMetadata1', () => {
const configOptions = new ConfigOptions(Uri.empty());
configOptions.diagnosticRuleSet.enableExperimentalFeatures = true;

const analysisResults = TestUtils.typeAnalyzeSampleFiles(['annotatedMetadata1.py'], configOptions);
TestUtils.validateResults(analysisResults, 6);
});

test('Circular1', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['circular1.py']);

Expand Down

0 comments on commit 8ed69a9

Please sign in to comment.