Skip to content

Optional attrs and methods for Protocol classes #287

Answered by leycec
tvdboom asked this question in Q&A
Discussion options

You must be logged in to vote

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):

While the idea of optional members looks interesting, it would complicate this proposal and it is not clear how useful it will be. Therefore, it is proposed to postpone this; see rejected ideas.

...

We can come up with examples where it would be handy to be able to say that a method or data attribute does not need to be present in a class implementing a protocol, but if it is present, it must conform to a s…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by tvdboom
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants