-
Alright, here I come with a new issue. I was searching for a way to add optional attributes and methods to a protocol and this is everything I could find: python/typing#601. Example: from typing import NotRequired, Protocol
from sklearn.preprocessing import StandardScaler
class Transformer(Protocol):
optional_attr: NotRequired[str]
def transform(self, **params): ...
isinstance(StandardScaler(), Transformer) # false, but mypy passes! Would be nice if beartype passes as well... Using So, how to solve this? Is there a way to add optional attributes and methods to protocol classes? Would it be noption to implement something like this in beartype? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Excellent question! Does mypy really accept that? That... might be a bug. Maybe? That's certainly non-standard, which isn't great. Even if mypy accepts that, it's doubtful any other type-checkers do. To boringly quote PEP 544 – Protocols: Structural subtyping (static duck typing):
Sadly, PEP 544 intentionally chose not to do anything here. Sadly x 2, the completely unrelated PEP 655 – Marking individual TypedDict items as required or potentially-missing that standardized the What Is Even Going on Anymore!?If mypy has quietly extended Would you mind copy-pasting your fascinating Oh — and thanks so much for the continued excitement surrounding @beartype and protocols. By some divine miracle, we will eventually get all of this to work for you. You are posing thought-provoking problems and deserve Python typing that actually works. Two jaundiced thumbs way up! 👍 👍 |
Beta Was this translation helpful? Give feedback.
Excellent question! Does mypy really accept that? That... might be a bug. Maybe? That's certainly non-standard, which isn't great. Even if mypy accepts that, it's doubtful any other type-checkers do. To boringly quote PEP 544 – Protocols: Structural subtyping (static duck typing):