Warum Python -Typ in den Fehler für Dekorateur in den Typ Python -TypPython

Python-Programme
Anonymous
 Warum Python -Typ in den Fehler für Dekorateur in den Typ Python -Typ

Post by Anonymous »

Type infer error for decorator
  • class with __get__ not properly identified
  • type with Generic lose Type[]
The following are the comparisons of several Szenarien. < /p>

Code: Select all

from typing import TypeVar, Callable, Generic, Type
GetterReturnType = TypeVar("GetterReturnType")
class ClassPropertyDescriptor(Generic[GetterReturnType]):

def __init__(self, func: Callable[..., GetterReturnType]):
if isinstance(func, (classmethod, staticmethod)):
fget = func
else:
fget = classmethod(func)
self.fget = fget

def __get__(self, obj, klass=None):
if klass is None:
klass = type(obj)
return self.fget.__get__(obj, klass)()
def classproperty(func: Callable[..., GetterReturnType]):
return ClassPropertyDescriptor(func)
T = TypeVar('T')
class B(Generic[T]):
pass
class A:
@classproperty
def a(self):
return [123]

@ClassPropertyDescriptor[list[int]]
def a2(self):
return [123]

@ClassPropertyDescriptor[Type[B]]
def b(self):
return B

@ClassPropertyDescriptor[Type[B[int]]]
def b2(self):
return B[int]

@ClassPropertyDescriptor[B[int]]
def b3(self):
return B[int]()

# should be list[int]
x = A.a
x2 = A.a2
y = A.b
# should be Type[B[int]]
y2 = A.b2
y3 = A.b3
Hier ist ein Screenshot von Pycharm:

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post