6 Mar
2023
6 Mar
'23
1:47 a.m.
I already know those packages, but thanks. Now I know how to use `__get__` overload to annotate class and instance variables with distinct types. Thanks, Eric. ```python from typing import Generic, Self, Type, TypeVar, overload TClassVar = TypeVar('TClassVar') TInstanceVar = TypeVar('TInstanceVar') class VariableMember(Generic[TClassVar, TInstanceVar]): @overload def __get__(self: 'VariableMember', obj: Self, obj_type: Type[Self]) -> TInstanceVar: ... @overload def __get__(self: 'VariableMember', obj: None, obj_type: Type[Self]) -> TClassVar: ... class Foo: a: VariableMember[int, str] = 5 # typechecker OK Foo.a = 10 # typechecker NG Foo.a = '10' # typechecker OK Foo().a = '10' # typechecker NG Foo().a = 10 ```