-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Overloading an abstract attribute with a Final one #14863
Comments
If it's final, it means it cannot be changed
What are you trying to achieve with |
@ikonst you're right, |
This doesn't require all subclasses to have final strings (I'm not sure how you could ensure that) but you can make your original example work with a readonly property! from typing import Final
from abc import ABC, abstractmethod
class Base(ABC):
@property
@abstractmethod
def toto(self) -> str:
...
def access_toto(self) -> str:
return self.toto
class CmdA(Base):
toto: Final[str] = "toto" |
Should we close this, or do we consider this a usability problem with the current error "Cannot override writable attribute "{name}" with a final one'"? For example, do we need treatment similar to this? Lines 1194 to 1205 in 267d376
|
My original point was more about:
@A5rocks yes this answers point 1, though I prefer avoiding
For point 2 I still don't have any idea. I'd think the final annotation in superclass without an assignment could have a use here, it would just declare an "abstract attribute" -- assigning @ikonst: yes I'd tend to think similar treatment would help, but it's not the whole of this ticket ;) |
Hm I just re-read this and I think typing-wise a property setter that has a NoReturn return type might work for point 2, but I'm not sure if mypy supports that and I'm on mobile so can't test. |
In the following code it would be useful to mark
toto
as final to make sure no code modifies it by error. However this triggersCannot override writable attribute "toto" with a final one
In fact I tend to agree with that error, in that what I'd really like to do is to annotate
toto
as "abstract final", which is not possible either as the use oftoto: Final[str]
triggersFinal name must be initialized with a value
, andtoto: Final[str] = NotImplemented
still gets theCannot override final attribute "toto" (previously declared in base class "Base")
the previous attempt also showed.The text was updated successfully, but these errors were encountered: