DetachHead / basedpyright

pyright fork with various type checking improvements, improved vscode support and pylance features built into the language server
https://docs.basedpyright.com
Other
967 stars 18 forks source link

option to ban abstract methods with implementations #630

Open DetachHead opened 3 weeks ago

DetachHead commented 3 weeks ago
class Foo(ABC):
    @abstractmethod
    def foo(self):
        print(1)

apparently this is intentionally allowed but i think it's stupid so there should be an option to ban it

KotlinIsland commented 3 weeks ago

Note Unlike Java abstract methods, these abstract methods may have an implementation. This implementation can be called via the super() mechanism from the class that overrides it. This could be useful as an end-point for a super-call in a framework that uses cooperative multiple-inheritance.

mahyarmirrashed commented 1 week ago

That's got to be a peak level of Python idiocy. I fully support this. Ban it 🔨

KotlinIsland commented 1 week ago

alternativly, could we require that it explicitly raises an exception:

from abc import ABC, abstractmethod
from typing import override

class A(ABC):
    @abstractmethod
    def foo(self) -> int: ...

class D:
    def foo(self) -> int:
        return 1

class C(D):
    @override
    def foo(self) -> int:
        return super().foo()
# mro = B, C, A, D
class B(C, A, D): pass

_ = B().foo() + 1  # no error, None at runtime

playground

maybe no implementation, or exception, your choice.

mahyarmirrashed commented 1 week ago

I vote for no implementation. It's the most idiomatic choice IMO. Abstract methods are supposed to be abstract lol. I don't know if there are any other programming languages that allow this (they shouldn't).

KotlinIsland commented 1 week ago

other programming languages tend to throw an error if you somehow end up calling an abstract method, python just returns None