0

Lets say I'm using this implementation of classproperty @staticmethod with @property :

class classproperty(property):
    def __get__(self, cls, owner):
        return classmethod(self.fget).__get__(None, owner)()
  • this is useful for instance-less classes (like Java Interfaces) when you still want to use the property decorator but you don't have a self , just a cls

How could you satisfy linters and cast the classmethods such that when overrides occur of them, you won't get linter errors that type Callable[[Type], IntendedType] is not IntendedType ?

Here is an example:

Class A:
   @classproperty
   def foo(cls) -> str:
      return 'bar'

class B(A):
   foo = 'not bar'

when I run mypy on this code, I get the following error:

error: Incompatible types in assignment (expression has type "str", base class "A" defined the type as "Callable[[A], str]")  [assignment]

It looks like the way to deal with this would be to:

Class A:
   @classproperty
   def foo(cls) -> str:
      return 'bar'
   foo = cast(str, foo)

But that just feels wrong (Also it doesn't work).

Is there some way to achieve the same result by modifying some parts of classproperty ?

other than adding some comment at the beginning of the file to entirely ignore linters checks? (which I am not interested at, not at the beginning of the file nor per specific lines either)

Alex Waygood
  • 6,304
  • 3
  • 24
  • 46
AmazingMiki
  • 254
  • 2
  • 10

0 Answers0