Skip to content

Commit

Permalink
Removed support for transform_descriptor_types parameter in `datacl…
Browse files Browse the repository at this point in the history
…ass_transform`, a feature that was determined to be not necessary. Added support on normal dataclass handling for field types that are custom descriptor objects.
  • Loading branch information
msfterictraut committed Mar 27, 2022
1 parent 9558239 commit 368dc5e
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 98 deletions.
22 changes: 3 additions & 19 deletions packages/pyright-internal/src/analyzer/dataClasses.ts
Expand Up @@ -401,9 +401,9 @@ export function synthesizeDataClassMethods(
effectiveType = applySolvedTypeVars(effectiveType, typeVarMap);
}

if (classType.details.dataClassBehaviors?.transformDescriptorTypes) {
effectiveType = transformDescriptorType(evaluator, effectiveType);
}
// Is the field type a descriptor object? If so, we need to extract the corresponding
// type of the __init__ method parameter from the __set__ method.
effectiveType = transformDescriptorType(evaluator, effectiveType);

const functionParam: FunctionParameter = {
category: ParameterCategory.Simple,
Expand Down Expand Up @@ -631,7 +631,6 @@ export function validateDataClassTransformDecorator(
keywordOnlyParams: false,
generateEq: true,
generateOrder: false,
transformDescriptorTypes: false,
fieldDescriptorNames: [],
};

Expand Down Expand Up @@ -687,20 +686,6 @@ export function validateDataClassTransformDecorator(
break;
}

case 'transform_descriptor_types': {
const value = evaluateStaticBoolExpression(arg.valueExpression, fileInfo.executionEnvironment);
if (value === undefined) {
evaluator.addError(
Localizer.Diagnostic.dataClassTransformExpectedBoolLiteral(),
arg.valueExpression
);
return;
}

behaviors.transformDescriptorTypes = value;
break;
}

case 'field_descriptors': {
const valueType = evaluator.getTypeOfExpression(arg.valueExpression).type;
if (
Expand Down Expand Up @@ -770,7 +755,6 @@ export function getDataclassDecoratorBehaviors(type: Type): DataClassBehaviors |
keywordOnlyParams: false,
generateEq: true,
generateOrder: false,
transformDescriptorTypes: false,
fieldDescriptorNames: ['dataclasses.field', 'dataclasses.Field'],
};
}
Expand Down
1 change: 0 additions & 1 deletion packages/pyright-internal/src/analyzer/types.ts
Expand Up @@ -416,7 +416,6 @@ export interface DataClassBehaviors {
keywordOnlyParams: boolean;
generateEq: boolean;
generateOrder: boolean;
transformDescriptorTypes: boolean;
fieldDescriptorNames: string[];
}

Expand Down
44 changes: 44 additions & 0 deletions packages/pyright-internal/src/tests/samples/dataclass19.py
@@ -0,0 +1,44 @@
# This sample tests the handling of dataclass fields that use
# descriptor objects.

from dataclasses import dataclass
from typing import Any, cast, overload


class MyDescriptor:
@overload
def __get__(self, __obj: None, __owner: Any) -> "MyDescriptor":
...

@overload
def __get__(self, __obj: object, __owner: Any) -> int:
...

def __get__(self, __obj: object | None, __owner: Any) -> "int | MyDescriptor":
if __obj is None:
return self
return cast(Any, __obj)._x

def __set__(self, __obj: object, __value: int) -> None:
if __obj is not None:
cast(Any, __obj)._x = __value


@dataclass
class Foo:
y: MyDescriptor = MyDescriptor()


f1 = Foo(3)

reveal_type(f1.y, expected_text="int")
reveal_type(Foo.y, expected_text="MyDescriptor")


# This should generate an error.
f2 = Foo("hi")


f3 = Foo()
reveal_type(f3.y, expected_text="int")

72 changes: 0 additions & 72 deletions packages/pyright-internal/src/tests/samples/dataclassTransform5.py

This file was deleted.

6 changes: 0 additions & 6 deletions packages/pyright-internal/src/tests/typeEvaluator3.test.ts
Expand Up @@ -1191,12 +1191,6 @@ test('DataclassTransform4', () => {
TestUtils.validateResults(analysisResults, 1);
});

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

TestUtils.validateResults(analysisResults, 1);
});

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

Expand Down
6 changes: 6 additions & 0 deletions packages/pyright-internal/src/tests/typeEvaluator4.test.ts
Expand Up @@ -561,6 +561,12 @@ test('DataClass18', () => {
TestUtils.validateResults(analysisResults, 2);
});

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

TestUtils.validateResults(analysisResults, 1);
});

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

Expand Down

0 comments on commit 368dc5e

Please sign in to comment.