Repository navigation
Apply metaclass data descriptors when metaclass is type - #12036
crypt0lith wants to merge 9 commits into
Conversation
|
Reviewed One pre-existing gap, not a regression in this read fix: when the class body defines |
|
The write and delete gap isn't specific to class Meta(type):
@property
def x(cls) -> int:
return 0
class C(metaclass=Meta):
x = "class body"
reveal_type(C.x)
C.x = "other"
del C.xThe read already honors On the cost of the new lookup: I measured it before opening the PR, comparing ff00b03 against 73a7b69. Each run was
Runs 8 to 12 were slowed by other load for both builds. The means differ by 0.06 s, and the fastest runs by 0.3 s (1.7%), so the difference is within noise. |
🔴 Pyright CLI QA —
|
|
I replayed the new QA cases against base
Validation: both revisions build with |
Class-level writes to converter fields are still supported. The problem is with how assignment is modeled. A class is an instance of its metaclass, so assignments on a class go through the metaclass's That only shows up for converter fields because pyright models them with a synthesized descriptor ( |
synthesizeDataClassMethods replaces a converter field's symbol with a synthesized descriptor, so class-object assignment, which no longer invokes it, is checked against the descriptor type. The descriptor is created only while synthesizing __init__. Keep the declared symbol, store the converter's input type on the dataclass entry, and check assignment through an instance against it, whether or not __init__ is synthesized.
|
07076bd fixes the three findings from the QA bot run on 18df53e: an empty container assigned to a converter field, through a union of descriptors, or to a class attribute shadowed by a metaclass data descriptor was inferred against the declared type rather than the type it's checked against. The I'll post on DPO for a consensus. I wouldn't treat the |
Posted here: https://discuss.python.org/t/clarifying-classvar-on-metaclasses/110326 |
Summary
python/typeshed#16674 declares
type's read-only attributes as getter-only properties, reverting theFinaldeclarations that 4975f45 brought in. Under those stubs,type.__mro__evaluates toproperty, andC.__mro__evaluates toC's own__mro__when its class body defines one.Member access on a class object doesn't follow the descriptor protocol:
typeis never applied.__get__returns something other than the descriptor through the class, or when it defines__delete__but not__set__.__set__instead of the descriptor's type.__set__is checked against the descriptor's own type, and a deletion through one that lacks__delete__isn't reported.__set__'s value type.An assignment through an instance to a dataclass field with a converter is checked against the converter's input type only when pyright synthesizes
__init__for the class.Root cause
getTypeOfBoundMemberskips the metaclass data descriptor lookup when the metaclass istype, and when the metaclass is the class itself, which onlytypeis. The comment on the check saystypehas no attributes that are descriptors, butobject.__class__is a property intype's MRO, and so is each oftype's read-only attributes whenever the stub declares it as one. The lookup also runs only for reads, and it tests the metaclass member after applying its__get__(typeEvaluator.ts:1811-1830), requiring a__set__. CPython treats a descriptor that defines__set__or__delete__as a data descriptor, and for assignment and deletion it doesn't require__get__. A union counts only if every subtype is a data descriptor (typeUtils.ts:1406-1412).The lookup on the class itself applies a class member's descriptor (
typeEvaluator.ts:4805-4816) on assignment and deletion through the class object too, though CPython consults only the metaclass for those. When the descriptor has no__set__or__delete__, the error is reported only for properties (typeEvaluator.ts:5007-5020). The declared type for an assignment (typeEvaluator.ts:2177-2186) is__set__'s value type only when the declared type is a single class instance, and the descriptor's own type when__set__is missing.Dataclass synthesis replaces a converter field's symbol (
dataClasses.ts:709-728) with an instance of a synthesized descriptor class whose__set__takes the converter's input type, and does so only while synthesizing__init__(dataClasses.ts:686-689).Fix
For member access on a class object:
__set__or__delete__, and for__get__on reads.__set__.For assignment and deletion through a descriptor:
__set__or__delete__.__set__'s value type for each subtype of the declared type, with no expected type when a data descriptor lacks__set__, or when the value types of several descriptors are the same class with different type arguments.For a dataclass field with a converter:
Update the bundled typeshed
stdlib/builtins.pyito python/typeshed@8024194 for the stub change.Test
Cases added to
memberAccess23.pyClassB.__mro__typewins over class attributeClassB().__mro__type.__mro__typeas its own metaclassClassC1.getClassC1.getset__get__doesn't return the descriptorClassC1.getdelete__get__and__delete__onlyClassC1.getsetdelete__get__returns the descriptorfunc1typefunc2typefunc3__set__func4func5__set__func6__set__func7__delete__func8__delete__func9__delete__func10__delete__func11__set__ClassD.attr1func12__set__in shadowed metaclass unionfunc13__delete__func14__set__ClassE.attr1__set__/__delete__yields to class attributefunc15__set__/__delete__func16__set__, not class attributefunc17__set__, list literalCases added and changed in
dataclassConverter2.pyDC1.asymmetric = "2"DC1.asymmetric = 2func1init=Falsefieldfunc2init=Falsefieldfunc3__init__func4__init__func5func6Cases added to
descriptor8.pyfunc1__set__func2__delete__func3__delete__func4__set__func5func6func7__set__value type, not descriptor typefunc8__set__reports missing__set__func9__set__in annotated union reports missing__set__func10__set__value types differ in type argumentfunc11__set__in union of descriptorsCases changed in
memberAccess10.pyandproperty11.pyX.number_cls = "hi"__set__of class-valued attributeClass1.prop1 = "hi"