Add protocol type BaseVersion#400
Conversation
|
IMO a better approach would be to provide a public |
|
I'm open to the suggestion, but a couple questions: Using What do you see as the advantage over using a simple base class? IIUC, code that needs to support non-PEP-440 could inherit from |
|
I believe it’d work putting it behind Once |
This allows mypy to know about all available properties when APIs return any Version type. For example, the pip project defines functions that return a _BaseVersion instance. One example is: https://github.com/pypa/pip/blob/bbf8466088655d22cd46b286c8f0b8150754c1d9/src/pip/_internal/metadata/base.py#L24 Downstream projects, such as pip-tools, use mypy to help verify correct API use. pip-tools then calls methods on the _BaseVersion instance which reports: "_BaseVersion" has no attribute "is_prerelease" By defining the protocol, pip and other libraries can use use its typing information and it will be more useful to more projects.
|
Sounds good. Thanks for explaining. I implemented |
| if TYPE_CHECKING: | ||
| from typing import Protocol | ||
|
|
||
| class BaseVersion(Protocol): |
There was a problem hiding this comment.
Do we need to type __init__() here to make sure implementations don't accidentally cause Liskov violations?
|
Is this actually worth the effort? The deprecation for So I would actually say we should not take this PR and instead remove |
|
I don't think we need this now, assuming we do accept #407. :) |
|
(converted to draft, so that we don't merge this accidentally) |
|
I think #407 is the way to go. I'm closing this. Thanks for opening the PR @pradyunsg ! |
This allows mypy to know about all available properties when APIs return
any Version type.
For example, the pip project defines functions that return a
_BaseVersion instance. One example is:
https://github.com/pypa/pip/blob/bbf8466088655d22cd46b286c8f0b8150754c1d9/src/pip/_internal/metadata/base.py#L24
Downstream projects, such as pip-tools, use mypy to help verify correct
API use. pip-tools then calls methods on the _BaseVersion instance which
reports:
By defining the protocol, pip and other libraries can use use its typing
information and it will be more useful to more projects.