mirror of
https://github.com/microsoft/pyright.git
synced 2024-09-11 07:55:56 +03:00
Removed recent change in dataclass_transform
handling. Dataclass properties should not be inherited from base classes after all. See https://discuss.python.org/t/dataclass-transform-inheriting-class-doesnt-recognize-kw-only-true-from-parent-class/55734/2.
This commit is contained in:
parent
cbd189a29f
commit
a03eb8492e
@ -17071,42 +17071,19 @@ export function createTypeEvaluator(
|
|||||||
// Determine whether this class derives from (or has a metaclass) that imbues
|
// Determine whether this class derives from (or has a metaclass) that imbues
|
||||||
// it with dataclass-like behaviors. If so, we'll apply those here.
|
// it with dataclass-like behaviors. If so, we'll apply those here.
|
||||||
let dataClassBehaviors: DataClassBehaviors | undefined;
|
let dataClassBehaviors: DataClassBehaviors | undefined;
|
||||||
|
if (isInstantiableClass(effectiveMetaclass) && effectiveMetaclass.details.classDataClassTransform) {
|
||||||
|
dataClassBehaviors = effectiveMetaclass.details.classDataClassTransform;
|
||||||
|
} else {
|
||||||
|
const baseClassDataTransform = classType.details.mro.find((mroClass) => {
|
||||||
|
return (
|
||||||
|
isClass(mroClass) &&
|
||||||
|
mroClass.details.classDataClassTransform !== undefined &&
|
||||||
|
!ClassType.isSameGenericClass(mroClass, classType)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
// If this class has not already received its dataclass behaviors from a
|
if (baseClassDataTransform) {
|
||||||
// decorator and is inheriting from a class that has dataclass behaviors,
|
dataClassBehaviors = (baseClassDataTransform as ClassType).details.classDataClassTransform;
|
||||||
// apply those inherited behaviors to this class.
|
|
||||||
if (!classType.details.dataClassBehaviors) {
|
|
||||||
for (const mroClass of classType.details.mro) {
|
|
||||||
if (!isClass(mroClass)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ClassType.isSameGenericClass(mroClass, classType)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mroClass.details.dataClassBehaviors) {
|
|
||||||
dataClassBehaviors = mroClass.details.dataClassBehaviors;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!dataClassBehaviors) {
|
|
||||||
if (isInstantiableClass(effectiveMetaclass) && effectiveMetaclass.details.classDataClassTransform) {
|
|
||||||
dataClassBehaviors = effectiveMetaclass.details.classDataClassTransform;
|
|
||||||
} else {
|
|
||||||
const baseClassDataTransform = classType.details.mro.find((mroClass) => {
|
|
||||||
return (
|
|
||||||
isClass(mroClass) &&
|
|
||||||
mroClass.details.classDataClassTransform !== undefined &&
|
|
||||||
!ClassType.isSameGenericClass(mroClass, classType)
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (baseClassDataTransform) {
|
|
||||||
dataClassBehaviors = (baseClassDataTransform as ClassType).details.classDataClassTransform;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,29 +0,0 @@
|
|||||||
# This sample tests the case where a dataclass inherits a dataclass
|
|
||||||
# behavior (like kw_only) from its parent class.
|
|
||||||
|
|
||||||
from typing import dataclass_transform
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass_transform()
|
|
||||||
class ModelMeta(type):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class ModelBase(metaclass=ModelMeta):
|
|
||||||
def __init_subclass__(cls, kw_only: bool = False) -> None:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class Base(ModelBase, kw_only=True):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class Model(Base):
|
|
||||||
a: str | None = None
|
|
||||||
b: int
|
|
||||||
|
|
||||||
|
|
||||||
Model(b=0)
|
|
||||||
|
|
||||||
# This should generate an error because of kw_only.
|
|
||||||
Model("", 1)
|
|
@ -416,12 +416,6 @@ test('DataclassTransform4', () => {
|
|||||||
TestUtils.validateResults(analysisResults, 2);
|
TestUtils.validateResults(analysisResults, 2);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('DataclassTransform5', () => {
|
|
||||||
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['dataclassTransform5.py']);
|
|
||||||
|
|
||||||
TestUtils.validateResults(analysisResults, 1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Async1', () => {
|
test('Async1', () => {
|
||||||
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['async1.py']);
|
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['async1.py']);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user