Skip to content
Snippets Groups Projects
Unverified Commit 2a2d1989 authored by Todd Gamblin's avatar Todd Gamblin Committed by GitHub
Browse files

`version_types`: clean up type hierarchy and add annotations (#47781)


In preparation for adding `when=` to `version()`, I'm cleaning up the types in
`version_types` and making sure the methods here pass `mypy` checks. This started as an
attempt to use `ConcreteVersion` outside of `spack.version` and grew into a larger type
refactor.

The hierarchy now looks like this:

* `VersionType`
  * `ConcreteVersion`
    * `StandardVersion`
    * `GitVersion`
  * `ClosedOpenRange`
  * `VersionList`

Note that the top-level thing can't easily be `Version` as that is a method and it
returns only `ConcreteVersion` right now. I *could* do something fancy with `__new__` to
make `Version` a synonym for the `ConcreteVersion` constructor, which would allow it to
be used as a type. I could also do something similar with `VersionRange` but not sure if
it's worth it just to make these into types.

There are still some places where I think `GitVersion` might not be handled properly,
but I have not attempted to fix those here.

- [x] Add a top-level `VersionType` class that all version types extend from
- [x] Define and document common methods and rich comparisons on `VersionType`
- [x] Replace complicated `Union` types with `VersionType` and `ConcreteVersion` as needed
- [x] Annotate most methods (skipping `__getitem__` and friends as the typing is a pain)
- [x] Fix up the `VersionList` constructor a bit
- [x] Add cases to methods that weren't handling all `VersionType`s
- [x] Rework some places to clarify typing for `mypy`
- [x] Simplify / optimize _next_version
- [x] Make StandardVersion.string a property to enable lazy comparison

Signed-off-by: default avatarTodd Gamblin <tgamblin@llnl.gov>
parent c6e292f5
Branches
No related tags found
No related merge requests found
...@@ -607,6 +607,9 @@ def test_stringify_version(version_str): ...@@ -607,6 +607,9 @@ def test_stringify_version(version_str):
v.string = None v.string = None
assert str(v) == version_str assert str(v) == version_str
v.string = None
assert v.string == version_str
def test_len(): def test_len():
a = Version("1.2.3.4") a = Version("1.2.3.4")
......
...@@ -25,11 +25,13 @@ ...@@ -25,11 +25,13 @@
) )
from .version_types import ( from .version_types import (
ClosedOpenRange, ClosedOpenRange,
ConcreteVersion,
GitVersion, GitVersion,
StandardVersion, StandardVersion,
Version, Version,
VersionList, VersionList,
VersionRange, VersionRange,
VersionType,
_next_version, _next_version,
_prev_version, _prev_version,
from_string, from_string,
...@@ -40,21 +42,23 @@ ...@@ -40,21 +42,23 @@
any_version: VersionList = VersionList([":"]) any_version: VersionList = VersionList([":"])
__all__ = [ __all__ = [
"Version",
"VersionRange",
"ver",
"from_string",
"is_git_version",
"infinity_versions",
"_prev_version",
"_next_version",
"VersionList",
"ClosedOpenRange", "ClosedOpenRange",
"StandardVersion", "ConcreteVersion",
"EmptyRangeError",
"GitVersion", "GitVersion",
"VersionError", "StandardVersion",
"Version",
"VersionChecksumError", "VersionChecksumError",
"VersionError",
"VersionList",
"VersionLookupError", "VersionLookupError",
"EmptyRangeError", "VersionRange",
"VersionType",
"_next_version",
"_prev_version",
"any_version", "any_version",
"from_string",
"infinity_versions",
"is_git_version",
"ver",
] ]
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment